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

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

BFS

我的代码
 1 #include <stdio.h>
2 #include <string.h>
3 const int N=10000;
4 const int dx[4]={0,1,0,-1};
5 const int dy[4]={-1,0,1,0};
6 const char dd[5]="LDRU";
7 int maze1[10][10],maze2[10][10];
8 int vis[N],dir[N],fa[N],q[N],dir2[N];
9 int bfs(int x1,int y1,int x2,int y2)
10 {
11 memset(q,0,sizeof(0));
12 int u=x1*1000+y1*100+x2*10+y2;
13 vis[u]=1; fa[u]=0;
14 int front=0,rear=1;
15 q[1]=u;
16 while (front<rear)
17 {
18 u=q[++front];
19 x1=u/1000; y1=u/100%10;
20 x2=u/10%10; y2=u%10;
21 if (((maze1[x1][y1]&maze2[x2][y2])>>6)&1) return u;
22 int d2,d,nx1,ny1,nx2,ny2;
23 const int dd[4]={1,0,2,3};
24 for (d2=0;d2<4;d2++)
25 {
26 d=dd[d2];
27 nx1=x1; ny1=y1;
28 if (((maze1[x1][y1]>>d)&1)==0) {nx1+=dx[d]; ny1+=dy[d];}
29 nx2=x2; ny2=y2;
30 if (((maze2[x2][y2]>>d)&1)==0) {nx2+=dx[d]; ny2+=dy[d];}
31 int v=nx1*1000+ny1*100+nx2*10+ny2;
32 int w=maze1[nx1][ny1]&maze2[nx2][ny2];
33 if (vis[v] || ((w>>4)&1)==0) continue;
34 vis[v]=1; dir[v]=d; fa[v]=u;
35 q[++rear]=v;
36 }
37 }
38 return -1;
39 }
40 int main()
41 {
42 int T,i,j;
43 scanf("%d",&T);
44 memset(maze2,0,sizeof(maze2));
45 for (i=1;i<=6;i++)
46 for (j=1;j<=6;j++) scanf("%d",&maze2[i][j]);
47 T--;
48 while (T--)
49 {
50 memcpy(maze1,maze2,sizeof(maze1));
51 for (i=1;i<=6;i++)
52 for (j=1;j<=6;j++) scanf("%d",&maze2[i][j]);
53 memset(vis,0,sizeof(vis));
54 memset(dir,0,sizeof(dir));
55 memset(fa,0,sizeof(fa));
56 int x1,y1,x2,y2;
57 for (i=1;i<=6;i++) for (j=1;j<=6;j++)
58 if ((maze1[i][j]>>5)&1) {x1=i; y1=j;}
59 for (i=1;i<=6;i++) for (j=1;j<=6;j++)
60 if ((maze2[i][j]>>5)&1) {x2=i; y2=j;}
61 int e=bfs(x1,y1,x2,y2);
62 if (e==-1) {printf("-1\n"); continue;}
63 int m=0;
64 for (i=e;i;i=fa[i]) dir2[++m]=dir[i]; m--;
65 for (i=m;i;i--) printf("%c",dd[dir2[i]]);
66 printf("\n");
67 }
68 return 0;
69 }

 

posted on 2012-02-01 00:28  Qiuqiqiu  阅读(403)  评论(0编辑  收藏  举报