4 4 10 .... .... .... S**P 0 0 0
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 |
#include<stdio.h> #include<string.h> #define WALL -1 #define RODE 1000000 int
nx[50000],ny[50000],movex[4]={1,-1,0,0},movey[4]={0,0,1,-1},map[100][100]; void
bfs( int
sx, int
sy) { int
front = 0,rear = 1; nx[0] = sx; ny[0] = sy; map[sx][sy] = 0; while (front < rear) { int
i; for (i = 0;i < 4;i++) { int
newx = nx[front] + movex[i],newy = ny[front] + movey[i]; if (map[newx][newy] != WALL && map[nx[front]][ny[front]] + 1 < map[newx][newy]) { map[newx][newy] = map[nx[front]][ny[front]] + 1; nx[rear] = newx; ny[rear] = newy; rear++; } } front++; } } int
main() { //freopen("data.in","r",stdin); int
px,py,sx,sy,n,m,t,i,j; while ( scanf ( "%d%d%d" ,&n,&m,&t) && n != 0) { getchar (); memset (map,WALL, sizeof (map)); for (i = 0;i < m;i++) { for (j = 0;j < n;j++) { char
temp; scanf ( "%c" ,&temp); if (temp == ‘*‘ ) map[i][j] = WALL; else
map[i][j] = RODE; if (temp == ‘S‘ ) sx = i,sy = j; if (temp == ‘P‘ ) px = i,py = j; } getchar (); } bfs(sx,sy); int
ans = map[px][py]; if (ans <= t) puts ( "YES" ); else
puts ( "NO" ); } return
0; } |
原文:http://www.cnblogs.com/ahu-shu/p/3521500.html