0%

算法竞赛入门经典(第2版) 6-4UVa439 6-5UVa1600

比较忙比较累,只贴代码了。


题目:6-4 UVa439 - Knight Moves

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
//UVa439 - Knight Moves
//Accepted 0.000s
//#define _XIENAOBAN_
#include<iostream>
#include<cstring>
#include<queue>
#define M(po) Map[po.x][po.y]
using namespace std;

struct poi {
int x, y, weight;
poi operator +(const poi &that) const {
return poi{ x + that.x, y + that.y, weight};
}
bool operator ==(const poi &that) const {
return (x == that.x) && (y == that.y);
}
} op, ed;

const poi dir[8] = { { 2,1 },{ -2,1 },{ 2,-1 },{ -2,-1 },{ 1,2 },{ -1,2 },{ 1,-2 },{ -1,-2 } };
bool Map[10][10];
char xstart, ystart, xend, yend;
int xs, ys, xe, ye;

int BFS(){
if (op == ed) return 0;
queue<poi> Q;
Q.push(op);
M(op) = true;
while (!Q.empty()) {
for (int i(0);i < 8;++i){
poi nxt(Q.front() + dir[i]);
if (nxt.x > 0 && nxt.y > 0 && nxt.x < 9 && nxt.y < 9 && !M(nxt)) {
++nxt.weight;
if (nxt == ed) return nxt.weight;
M(nxt) = true;
Q.push(nxt);
}
}
Q.pop();
}
return -1;
}


int main()
{
#ifdef _XIENAOBAN_
#define gets(T) gets_s(T, 129)
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif

while (scanf("%c%c %c%c", &xstart, &ystart, &xend, &yend) == 4) {
memset(Map, 0, sizeof(Map));
op.x = xstart - 96, op.y = ystart - 48, op.weight = 0;
ed.x = xend - 96, ed.y = yend - 48, ed.weight = 0;
printf("To get from %c%c to %c%c takes %d knight moves.\n", xstart, ystart, xend, yend, BFS());
while (getchar() != '\n');
}
return 0;
}

题目: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_
#include<iostream>
#include<cstring>
#include<queue>
#define DONE 2333333
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()
{
#ifdef _XIENAOBAN_
#define gets(T) gets_s(T, 129)
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif

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;
}