题意:
约会女孩
从 Y到G 问最短的路程是什么 其中 .为可走路径 #为石头 其中石头在时间 t%k==0 的时候是可以消失的 即使可以走的 如果t%k!=0 就会又出现
所以我们在原来的二维数组上新增加一个数组 变成三位数组 用来标记在 t%k这个时间Y是否走过这个点 如果这个点之前t%k时间走过 那么再走就没有意义
AC代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n, m, k, bx, by, ex, ey;
struct node
{
int x, y, t;
};
char maze[111][111];
int f[4][2] = { 0,1,0,-1,1,0,-1,0 };
int used[110][110][11];
int BFS()
{
queue<node>q;
node now, next;
now.x = bx; now.y = by; now.t = 0;
memset(used, 0, sizeof(used));
used[bx][by][0] = 1;
q.push(now);
while (!q.empty())
{
now = q.front(); q.pop();
if (now.x == ex && now.y == ey)
{
return now.t;
}
for (int i = 0; i<4; i++)
{
int xx, yy;
xx = now.x + f[i][0];
yy = now.y + f[i][1];
if (xx >= 1 && xx<=n&& yy >= 1 && yy<=m && (maze[xx][yy] != '#' || (now.t + 1) % k == 0) && !used[xx][yy][(now.t + 1) % k])
{
next.t = now.t+ 1;
next.x = xx;
next.y = yy;
used[xx][yy][(now.t + 1) % k] = 1;
q.push(next);
}
}
}
return -1;
}
int main()
{
int i, j, t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++)
{
scanf("%s", maze[i]+1);
for (int j = 1; j<= m; j++)
{
if (maze[i][j] == 'Y') { bx = i; by = j; }
else if ( maze[i][j] == 'G') { ex = i; ey = j; }
}
}
int flag=BFS();
if( flag == -1) printf("Please give me another chance!\n");
else printf("%d\n", flag);
}
return 0;
}