http://acm.hdu.edu.cn/showproblem.php?pid=2579
1 6 6 2 ...Y.. ...#.. .#.... ...#.. ...#.. ..#G#.
7
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAXN 110 using namespace std; struct node { int x,y; int time; /*friend bool operator < (node a,node b) { return a.time>b.time; }*/ }; int row,column,k; char map[MAXN][MAXN]; node s,e; int x[4]={1,-1,0,0}; int y[4]={0,0,1,-1}; bool vis[MAXN][MAXN][20]; bool judge(int x,int y,int time) { if(x>row||x<=0||y>column||y<=0||vis[x][y][time%k]) return 0; if(map[x][y]=='#') { if(time%k==0) return 1; return 0; } return 1; } int BFS() { node pos,next; memset(vis,0,sizeof(vis)); queue<node>q; vis[s.x][s.y][0] = 1; s.time = 0; q.push(s); while(!q.empty()) { pos=q.front(); q.pop(); for(int i=0;i<4;++i) { next.x=pos.x+x[i]; next.y=pos.y+y[i]; next.time = pos.time+1; if(judge(next.x,next.y,next.time)) { if(next.x==e.x&&next.y==e.y) return next.time; vis[next.x][next.y][next.time%k]=1; q.push(next); } } } return -1; } int main() { int T; int i,j; scanf("%d",&T); while(T--) { scanf("%d%d%d",&row,&column,&k); getchar(); for(i=1;i<=row;++i) { for(j=1;j<=column;++j) { scanf("%c",map[i]+j); if(map[i][j]=='G') { e.x=i;e.y=j; } else if(map[i][j]=='Y') { s.x=i,s.y=j; } } getchar(); } int res=BFS(); if(res==-1) printf("Please give me another chance!\n"); else printf("%d\n",res); } return 0; }