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

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

BFS

我的代码
 1 #include <cstdio>
2 #include <queue>
3 using namespace std;
4 const int dx[4]={0,1,0,-1};
5 const int dy[4]={1,0,-1,0};
6 char maze[2][15][15];
7 bool vis[2][15][15];
8 int n,m,t,dis[2][15][15];
9 queue<int> q;
10 int bfs(int x,int y,int z)
11 {
12 while (!q.empty()) q.pop();
13 int u=z*m*n+x*m+y,v;
14 vis[z][x][y]=1;
15 q.push(u);
16 int nx,ny,nz;
17 while (!q.empty())
18 {
19 u=q.front(); q.pop();
20 z=u/m/n; x=u%(m*n)/m; y=u%(m*n)%m;
21 if (dis[z][x][y]>=t) return 0;
22 for (int d=0;d<4;d++)
23 {
24 nx=x+dx[d]; ny=y+dy[d]; nz=z;
25 if (nx<0 || nx>=n || ny<0 || ny>=m) continue;
26 if (maze[nz][nx][ny]=='#') nz=!nz;
27 if (maze[nz][nx][ny]=='*' || vis[nz][nx][ny]) continue;
28 if (maze[nz][nx][ny]=='P') return 1;
29 vis[nz][nx][ny]=1;
30 dis[nz][nx][ny]=dis[z][x][y]+1;
31 v=nz*m*n+nx*m+ny;
32 q.push(v);
33 }
34 }
35 return 0;
36 }
37 int main()
38 {
39 int T,i,k,j;
40 scanf("%d",&T);
41 while (T--)
42 {
43 scanf("%d%d%d",&n,&m,&t);
44 for (k=0;k<2;k++)
45 for (i=0;i<n;i++) scanf("%s",maze[k][i]);
46 for (i=0;i<n;i++) for (j=0;j<m;j++)
47 if (maze[0][i][j]=='#' && maze[1][i][j]=='#') maze[0][i][j]='*';
48 memset(vis,0,sizeof(vis));
49 memset(dis,0,sizeof(dis));
50 if (bfs(0,0,0)) printf("YES\n");
51 else printf("NO\n");
52 }
53 }

 

posted on 2011-12-16 11:49  Qiuqiqiu  阅读(154)  评论(0编辑  收藏  举报