Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 802 Accepted Submission(s): 195
1 6 6 2 ...Y.. ...#.. .#.... ...#.. ...#.. ..#G#.
7
#include <iostream> #include <stdio.h> #include <memory.h> #include <queue> using namespace std; int xx[4] = {1, -1, 0, 0}; int yy[4] = {0, 0, -1, 1}; char a[105][105]; int map[105][105][15]; //开3维数组保存该点步数 int x1, y1, x2, y2; int N, M, K; bool flag; struct node { int x, y, step; }n, m; int main() { int i, j, k, t; scanf("%d", &t); while(t--) { scanf("%d %d %d", &N, &M, &K); for(i = 0; i < N; i++) { getchar(); for(j = 0; j < M; j++) { scanf("%c", &a[i][j]); if(a[i][j] == 'Y') x1 = i, y1 = j; //找'Y'坐标 if(a[i][j] == 'G') x2 = i, y2 = j; //找'G'坐标 } } for(i = 0; i < N; i++) for(j = 0; j < M; j++) for(k = 0; k < K; k++) map[i][j][k] = 1000000; //初始化 flag = false; map[x1][y1][0] = 0; n.x = x1; n.y = y1; n.step = 0; queue<node> Q; Q.push(n); while(!Q.empty()) { m = Q.front(); Q.pop(); if(m.x == x2 && m.y == y2) //到达目标 { flag = true; break; } for(i = 0; i < 4; i++) { n.x = m.x + xx[i]; n.y = m.y + yy[i]; n.step = m.step + 1; if(n.x>=0 && n.x<N && n.y>=0 && n.y<M) //判断是否越界 { //判断该点是否可行和该点步数是否大于该点曾经步数 if(a[n.x][n.y] == '#' && n.step%K != 0) continue; if(n.step >= map[n.x][n.y][n.step%K]) continue; map[n.x][n.y][n.step%K] = n.step; //更新该点步数 Q.push(n); } } } if(flag) printf("%d\n", m.step); else printf("Please give me another chance!\n"); } return 0; }