forked from improper4/uva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa00321_TheNewVilla.java
151 lines (127 loc) · 3.79 KB
/
UVa00321_TheNewVilla.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 257 (321 - The New Villa) */
/* SUBMISSION: 10905767 */
/* SUBMISSION TIME: 2012-11-19 01:01:53 */
/* LANGUAGE: 2 */
import java.util.*;
import java.io.*;
public class UVa00321_TheNewVilla {
static int r;
static boolean[][] adjRooms;
static boolean[][] adjSwitch;
static class QueueItem {
int u, mask;
public QueueItem(int u, int mask) {
this.u = u; this.mask = mask;
}
}
static void bfs() {
Queue<QueueItem> Q = new LinkedList<QueueItem>();
int[][] dist = new int[r][1050];
boolean[][] visited = new boolean[r][1050];
QueueItem[][] p = new QueueItem[r][1050];
for (int i = 0; i < r; ++i) {
Arrays.fill(dist[i], -1);
Arrays.fill(p[i], null);
}
QueueItem s = new QueueItem(0, 1); // First node, only first light switched on
Q.offer(s);
dist[0][1] = 0;
visited[0][1] = true;
while (!Q.isEmpty()) {
QueueItem x = Q.poll();
int u = x.u;
int mask = x.mask;
// Move to another room with light on
for (int v = 0; v < r; ++v) {
if (adjRooms[u][v] && (mask & (1 << v)) != 0 &&
!visited[v][mask]) {
QueueItem next = new QueueItem(v, mask);
Q.offer(next);
dist[v][mask] = dist[u][mask] + 1;
visited[v][mask] = true;
p[v][mask] = x;
}
}
// Switch on or off a light, being careful not to turn off current room's light
for (int v = 0; v < r; ++v) {
if (adjSwitch[u][v] && u != v &&
!visited[u][mask ^ (1 << v)]) {
QueueItem next = new QueueItem(u, mask ^ (1 << v));
Q.offer(next);
dist[u][mask ^ (1 << v)] = dist[u][mask] + 1;
visited[u][mask ^ (1 << v)] = true;
p[u][mask ^ (1 << v)] = x;
}
}
}
if (dist[r - 1][1 << (r - 1)] == -1)
System.out.println("The problem cannot be solved.");
else {
System.out.println("The problem can be solved in " + dist[r - 1][1 << (r - 1)] + " steps:");
int tnode = r - 1;
int tmask = 1 << (r - 1);
Stack<String> path = new Stack<String>();
while (true) {
QueueItem parent = p[tnode][tmask];
if (parent == null)
break;
if (parent.u != tnode)
path.push("- Move to room " + (tnode + 1) + ".");
else {
int sw = 0;
for (int i = 0; i < r; ++i)
if ((parent.mask & (1 << i)) != (tmask & (1 << i))) {
sw = i;
break;
}
if (tmask < parent.mask)
path.push("- Switch off light in room " + (sw + 1) + ".");
else
path.push("- Switch on light in room " + (sw + 1) + ".");
}
tnode = parent.u;
tmask = parent.mask;
}
while (!path.isEmpty())
System.out.println(path.pop());
}
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
boolean first = true;
int t = 1;
while (true) {
if (first)
first = false;
else
in.readLine();
String[] parts = in.readLine().split("[ ]+");
r = Integer.parseInt(parts[0]);
int d = Integer.parseInt(parts[1]);
int s = Integer.parseInt(parts[2]);
if (r == 0 && d == 0 && s == 0) break;
System.out.println("Villa #" + t);
adjRooms = new boolean[r][r];
adjSwitch = new boolean[r][r];
for (int i = 0; i < d; ++i) {
parts = in.readLine().split("[ ]+");
int u = Integer.parseInt(parts[0]) - 1;
int v = Integer.parseInt(parts[1]) - 1;
adjRooms[u][v] = adjRooms[v][u] = true;
}
for (int i = 0; i < s; ++i) {
parts = in.readLine().split("[ ]+");
int u = Integer.parseInt(parts[0]) - 1;
int v = Integer.parseInt(parts[1]) - 1;
adjSwitch[u][v] = true;
}
bfs();
System.out.println();
++t;
}
in.close();
System.exit(0);
}
}