Topic links
/* Test instructions is a traditional maze plus a condition that the walls will disappear at the full multiples of k, so the shortest time to reach the exit is obtained.
The key point is that a point is taken up to K times, labeled Vis[x][y][time%k]. */#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <
Algorithm> using namespace std;
const int maxn=100+5;
int vis[maxn][maxn][15];
int dir[4][2]={0,1,1,0,0,-1,-1,0};
Char MAZE[MAXN][MAXN];
int Sx,sy,ex,ey;
int n,m,k;
void Init () {memset (vis,0,sizeof (Vis));} struct node {int x;
int y;
int time;
};
int BFS () {node now;
NOW.X=SX;
Now.y=sy;
now.time=0;
queue<node>que;
Que.push (now);
while (!que.empty ()) {Now=que.front ();
Que.pop ();
if (Now.x==ex&&now.y==ey) return now.time;
for (int i=0;i<4;i++) {int x=now.x+dir[i][0];
int y=now.y+dir[i][1];
int t=now.time+1;
int mod=t%k; if (x>=1&&x<=n&&y>=1&&y<=m) {if (maze[x]
[y]!= ' # ' &&vis[x][y][mod]==0) {vis[x][y][mod]=t;
Node next= (node) {x,y,t};
Que.push (next);
} else if (maze[x][y]== ' # ' &&mod==0&&vis[x][y][mod]==0) {
vis[x][y][mod]=t;
Node next= (node) {x,y,t};
Que.push (next);
}}}} return-1;
} int main () {int t;scanf ("%d", &t);
while (t--) {scanf ("%d%d%d", &n,&m,&k);
Init ();
for (int i=1;i<=n;i++) {scanf ("%s", maze[i]+1);
for (int j=1;j<=m;j++) if (maze[i][j]== ' Y ') sx=i,sy=j;
else if (maze[i][j]== ' G ') ex=i,ey=j;
} int Ans=bfs ();
if (ans==-1) printf ("Please give me another chance!\n"); else PrinTF ("%d\n", ans);
} return 0; }