比较忙比较累,只贴代码了。
题目:6-4 UVa439 - Knight Moves
1 | //UVa439 - Knight Moves |
题目:6-5 UVa1600 - Patrol Robot 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//UVa1600 - Patrol Robot
//Accepted 0.000s
//#define _XIENAOBAN_
using namespace std;
struct step { int x, y, k, mov; };
int T, m, n, k;
int Map[24][24],Obst[24][24];
void judge(queue<step> &Q, step &now, int x, int y) {
if (Obst[x += now.x][y += now.y] == DONE) return;
int _k = (Obst[x][y] ? now.k + 1 : 0);
if (_k <= k) {
if (_k) {
if (Map[x][y] && Map[x][y] <= _k) return;
Map[x][y] = _k;
}
else Obst[x][y] = DONE;
Q.push(step{ x, y, _k, now.mov + 1 });
}
}
int main()
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
scanf("%d", &T);
while (T--) {
scanf("%d%d%d", &m, &n, &k);
for (int i(1);i <= m;++i) for (int j(1);j <= n;++j)
scanf("%d", &Obst[i][j]);
memset(Map, 0, sizeof(Map));
queue<step> Q;
Q.push(step{ 1,1,0,0 });
Obst[1][1] = DONE;
while (!Q.empty()) {
step &now(Q.front());
if (now.x == m && now.y == n) break;
if (now.x + 1 <= m) judge(Q, now, 1, 0);
if (now.y + 1 <= n) judge(Q, now, 0, 1);
if (now.x - 1 >= 1) judge(Q, now, -1, 0);
if (now.y - 1 >= 1) judge(Q, now, 0, -1);
Q.pop();
}
if (Q.empty()) printf("-1\n");
else printf("%d\n", Q.front().mov);
}
return 0;
}