UVa 1063 - Marble Game

contents

  1. 1. Problem
  2. 2. Sample Input
  3. 3. Sample Output
  4. 4. Solution

Problem

旋轉盤面,讓每顆球落入屬於自己的洞,請問至少需要幾次旋轉。

  • 球不能翻過牆壁、不能跨越其他球、不能飛過空洞。
  • 球不能離開盤面
  • 每一格最多只有一顆球
  • 當球落入空洞時,這個空洞相當於被填滿,其他的球可以經過這個被填滿的洞,但無法再次落入該洞。

Sample Input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
4 3 1
0 1
1 0
1 2
2 3
2 1
3 2
1 1 1 2
3 2 2
0 0
0 1
0 2
2 0
2 0 1 0
2 0 2 1
0 0 0

Sample Output

1
2
3
Case 1: 5 moves
Case 2: impossible

Solution

首先有可能落錯空洞,模擬時必須特別小心這種非法的旋轉。在一次操作中,有可能在落入空洞的瞬間,下一個球剛好可以通過這個填滿的洞。

將每個球的座標壓成狀態,進行 Bfs 搜索。

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <stdio.h>
#include <queue>
#include <map>
#include <string.h>
#include <algorithm>
using namespace std;
int N, W;
struct state {
vector< pair<int, int> > xy;
bool operator<(const state &a) const {
for (int i = 0; i < xy.size(); i++)
if (xy[i] != a.xy[i])
return xy[i] < a.xy[i];
return false;
}
int isComplete() {
int ok = 1;
for (int i = 0; i < xy.size() && ok; i++)
ok &= xy[i].first < 0;
return ok;
}
};
int g[4][4][4];
pair<int, int> goal[64];
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int isValid(int x, int y) {
return x >= 0 && y >= 0 && x < W && y < W;
}
state eraseGoal(state u) {
for (int i = 0; i < u.xy.size(); i++) {
if (u.xy[i] == goal[i])
u.xy[i] = pair<int, int>(-1, -1);
}
return u;
}
state rotateMap(state u, int dir, int& ok) {
ok = 1;
int update = 1, tx, ty, x, y;
int used[4][4] = {}, usedg[4][4] = {};
for (int i = 0; i < u.xy.size(); i++) {
x = u.xy[i].first, y = u.xy[i].second;
if (x == -1) continue;
used[x][y] = 1;
x = goal[i].first, y = goal[i].second;
usedg[x][y] = i+1;
}
do {
update = 0;
for (int i = 0; i < u.xy.size(); i++) {
while (u.xy[i].first >= 0) {
x = u.xy[i].first, y = u.xy[i].second;
tx = x + dx[dir], ty = y + dy[dir];
if (isValid(tx, ty) && !g[x][y][dir] && usedg[tx][ty] && usedg[tx][ty] != i+1)
ok = 0;
if (isValid(tx, ty) && !g[x][y][dir] && (!used[tx][ty] || goal[i] == make_pair(tx, ty))) {
used[x][y] = 0, used[tx][ty] = 1;
u.xy[i] = pair<int, int>(tx, ty);
update = 1;
if (goal[i] == pair<int, int>(tx, ty)) {
u.xy[i] = pair<int, int>(-1, -1);
used[tx][ty] = 0, usedg[tx][ty] = 0;
}
} else {
break;
}
}
}
} while (update);
return u;
}
void print(state u) {
int used[4][4] = {}, x, y;
for (int i = 0; i < u.xy.size(); i++) {
x = u.xy[i].first, y = u.xy[i].second;
if (x == -1)
continue;
used[x][y] = i+1;
}
for (int i = 0; i < W; i++) {
for (int j = 0; j < W; j++)
printf("%d ", used[i][j]);
puts("");
}
puts("--");
}
int bfs(state init) {
state u, v;
queue<state> Q;
map<state, int> R;
int f;
init = eraseGoal(init);
Q.push(init), R[init] = 0;
// print(init);
if (init.isComplete())
return 0;
while (!Q.empty()) {
u = Q.front(), Q.pop();
int step = R[u];
// print(u);
// printf("step %d\n", step);
for (int i = 0; i < 4; i++) {
v = rotateMap(u, i, f);
v = eraseGoal(v);
if (!f || R.count(v)) continue;
if (v.isComplete())
return step + 1;
R[v] = step + 1;
// print(v);
Q.push(v);
}
// puts("--------------");
// getchar();
}
return -1;
}
int main() {
int x, y, tx, ty, M;
int sx, sy, ex, ey;
int cases = 0;
while (scanf("%d %d %d", &W, &N, &M) == 3 && N+W+M) {
state init;
memset(g, 0, sizeof(g));
for (int i = 0; i < N; i++) {
scanf("%d %d", &x, &y);
init.xy.push_back(pair<int, int>(x, y));
}
for (int i = 0; i < N; i++) {
scanf("%d %d", &x, &y);
goal[i] = pair<int, int>(x, y);
}
for (int i = 0; i < M; i++) {
scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
for (int j = 0; j < 4; j++) {
tx = sx + dx[j], ty = sy + dy[j];
if (tx == ex && ty == ey)
g[sx][sy][j] = 1;
tx = ex + dx[j], ty = ey + dy[j];
if (tx == sx && ty == sy)
g[ex][ey][j] = 1;
}
}
int step = bfs(init);
printf("Case %d: ", ++cases);
if (step < 0)
puts("impossible");
else
printf("%d moves\n", step);
puts("");
}
return 0;
}
/*
3 1 5
1 2
1 0
2 1 2 2
0 0 1 0
0 1 1 1
0 2 1 2
1 1 2 1
4 3 1
0 1
1 0
1 2
2 3
2 1
3 2
1 1 1 2
3 2 2
0 0
0 1
0 2
2 0
2 0 1 0
2 0 2 1
*/