forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1148The Game.cpp
More file actions
93 lines (86 loc) · 2.45 KB
/
1148The Game.cpp
File metadata and controls
93 lines (86 loc) · 2.45 KB
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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#ifdef WIN32
#define for if(0); else for
#endif
class point {
public:
int x;
int y;
point() {
}
point(int xx, int yy) {
x = xx, y = yy;
}
point &operator = (const point& cc) {
this->x = cc.x;
this->y = cc.y;
return *this;
}
};
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int w, h, board = 0;
void bfs(vector<vector<int> >& mat, point& a, point& b) {
vector<point> queue(10000);
int head, tail, nhead, ntail, step = 0, tx, ty;
head = tail = 0;
queue[0] = a;
while (1) {
++step;
nhead = tail + 1, ntail = tail;
for (int i = head; i <= tail; ++i) {
for (int dd = 0; dd < 4; ++dd) {
tx = queue[i].x, ty = queue[i].y;
while (1) {
tx += dir[dd][0], ty += dir[dd][1];
if (tx < 0 || tx > h + 1 || ty < 0 || ty > w + 1) break;
if (tx == b.x && ty == b.y) {
cout << step << " segments." << endl;
return;
}
if (mat[tx][ty] == -1 || (mat[tx][ty] != 0 && mat[tx][ty] < step)) break;
if (mat[tx][ty] == step) continue;
mat[tx][ty] = step;
queue[++ntail] = point(tx, ty);
}
}
}
if (ntail < nhead) break;
for (int i = nhead; i <= ntail; ++i) {
queue[i - nhead] = queue[i];
}
head = 0, tail = ntail - nhead;
}
cout << "impossible." << endl;
}
void Run() {
vector<vector<int> > mat(h + 2, vector<int>(w + 2, 0));
vector<vector<int> > bak(h + 2, vector<int>(w + 2, 0));
char in[100];
cin.getline(in, 100, '\n');
for (int i = 0; i < h; ++i) {
cin.getline(in, 100, '\n');
for (int j = 0; j < strlen(in); ++j) {
if (in[j] == 'X') mat[i + 1][j + 1] = -1;
}
}
copy(mat.begin(), mat.end(), bak.begin());
cout << "Board #" << ++board << ":" << endl;
point a, b;
int pair = 0;
while (cin >> a.y >> a.x >> b.y >> b.x) {
if (a.x == 0 && a.y == 0 && b.x == 0 && b.y == 0) break;
cout << "Pair " << ++pair << ": ";
copy(bak.begin(), bak.end(), mat.begin());
bfs(mat, a, b);
}
cout << endl;
}
int main() {
while (cin >> w >> h && w + h) {
Run();
}
return 0;
}