【HDOJ】2579 Dating with girls(2)
简单BFS。
1 /* 2579 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 using namespace std; 8 9 #define MAXN 105 10 11 typedef struct node_t { 12 int x, y, t; 13 node_t() {} 14 node_t(int xx, int yy, int tt) { 15 x = xx; y = yy; t = tt; 16 } 17 } node_t; 18 19 int n, m, b; 20 bool visit[MAXN][MAXN][10]; 21 char map[MAXN][MAXN]; 22 node_t beg; 23 int dir[4][2] = { 24 -1,0,1,0,0,-1,0,1 25 }; 26 27 inline bool check(int x, int y) { 28 return x<0 || x>=n || y<0 || y>=m; 29 } 30 31 int bfs() { 32 int i, j, k; 33 int x, y, t; 34 node_t nd; 35 queue<node_t> Q; 36 37 memset(visit, false, sizeof(visit)); 38 visit[beg.x][beg.y][0] = true; 39 Q.push(beg); 40 41 while (!Q.empty()) { 42 nd = Q.front(); 43 Q.pop(); 44 t = nd.t + 1; 45 k = t%b; 46 for (i=0; i<4; ++i) { 47 x = nd.x + dir[i][0]; 48 y = nd.y + dir[i][1]; 49 if (check(x,y) || visit[x][y][k]) 50 continue; 51 if (k && map[x][y]=='#') 52 continue; 53 if (map[x][y] == 'G') 54 return t; 55 visit[x][y][k] = true; 56 Q.push(node_t(x, y, t)); 57 } 58 } 59 60 return -1; 61 } 62 63 int main() { 64 int t; 65 int i, j, k; 66 67 #ifndef ONLINE_JUDGE 68 freopen("data.in", "r", stdin); 69 freopen("data.out", "w", stdout); 70 #endif 71 72 beg.t = 0; 73 scanf("%d", &t); 74 while (t--) { 75 scanf("%d %d %d", &n,&m,&b); 76 for (i=0; i<n; ++i) { 77 scanf("%s", map[i]); 78 for (j=0; j<m; ++j) { 79 if (map[i][j] == 'Y') { 80 beg.x = i; 81 beg.y = j; 82 } 83 } 84 } 85 k = bfs(); 86 if (k < 0) 87 puts("Please give me another chance!"); 88 else 89 printf("%d\n", k); 90 } 91 92 return 0; 93 }