http://acm.hdu.edu.cn/showproblem.php?pid=2579

时间:2022-05-15 09:18:57
#include<stdio.h>
#include<string.h>
#include<queue>
#define N 110 int m, n, k, x1, x2, y1, y2;
char map[N][N];
int v[N][N][N];//当时间是k的倍数时,障碍消失,之后又重现,所以在平时使用的二维标记数组中再加一维
int d[][] = {{-, }, {, }, {, -}, {, }}; using namespace std; struct node
{
int x, y , stemp;
}; int BFS()
{
node now, next;
int i;
memset(v, , sizeof(v));
queue<node>Q;
now.x = x1;
now.y = y1;
now.stemp = ;
v[x1][y1][] = ;
Q.push(now);
while(!Q.empty())
{
now = Q.front();
Q.pop();
if(now.x == x2 && now.y ==y2)
return now.stemp;
for(i = ; i < ; i++)
{
next.x = now.x + d[i][];
next.y = now.y + d[i][];
next.stemp = now.stemp + ;
if(next.x >= && next.x < m && next.y >= && next.y < n && (map[next.x][next.y] != '#' || next.stemp % k == ) && v[next.x][next.y][next.stemp] == )
{
v[next.x][next.y][next.stemp] = ;
Q.push(next);
}
}
}
return -;
}
int main()
{
int t, i, j, ans;
scanf("%d", &t);
while(t--)
{
scanf("%d%d%d", &m, &n, &k);
for(i = ; i < m ; i++)
scanf("%s", map[i]);
for(i = ; i < m ; i++)
{
for(j = ; j < n ; j++)
{
if(map[i][j] == 'Y')
x1 = i, y1 = j;
else if(map[i][j] == 'G')
x2 = i, y2 = j;
}
}
ans = BFS();
if(ans == -)
printf("Please give me another chance!\n");
else
printf("%d\n", ans);
}
return ;
}