Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

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

BFS

View Code
 1 #include <cstdio>
2 #include <cstring>
3 #include <queue>
4 using namespace std;
5 const int N=110;
6 const int dx[4]={0,1,0,-1};
7 const int dy[4]={1,0,-1,0};
8 char maze[N][N];
9 int vis[N][N][15];
10 int n,m,k;
11 struct sta
12 {
13 int x,y,dis;
14 };
15 queue<sta> q;
16 int bfs(int x,int y)
17 {
18 while (!q.empty()) q.pop();
19 sta u={x,y,0};
20 vis[x][y][0]=1;
21 q.push(u);
22 while (!q.empty())
23 {
24 u=q.front(); q.pop();
25 int x,y,d,nx,ny,dis;
26 x=u.x; y=u.y; dis=u.dis+1;
27 for (d=0;d<4;d++)
28 {
29 nx=x+dx[d]; ny=y+dy[d];
30 int bol1=vis[nx][ny][dis%k];
31 int bol2=!maze[nx][ny];
32 int bol3=maze[nx][ny]=='#' && dis%k!=0;
33 if (bol1 || bol2 || bol3) continue;
34 vis[nx][ny][dis%k]=1;
35 sta v={nx,ny,dis};
36 q.push(v);
37 if (maze[nx][ny]=='G') return dis;
38 }
39 }
40 return -1;
41 }
42 int main()
43 {
44 int T;
45 scanf("%d",&T);
46 int i,j,x,y,ans;
47 while (T--)
48 {
49 memset(vis,0,sizeof(vis));
50 memset(maze,0,sizeof(maze));
51 scanf("%d%d%d",&n,&m,&k);
52 for (i=1;i<=n;i++) scanf("%s",maze[i]+1);
53 for (i=1;i<=n;i++)
54 for (j=1;j<=m;j++)
55 if (maze[i][j]=='Y') {x=i; y=j;}
56 ans=bfs(x,y);
57 if (ans<0) printf("Please give me another chance!\n");
58 else printf("%d\n",ans);
59 }
60 return 0;
61 }

 

posted on 2012-02-11 21:13  Qiuqiqiu  阅读(235)  评论(0编辑  收藏  举报