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

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

逃离迷宫

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4785    Accepted Submission(s): 1145


Problem Description
  给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去。令人头痛的是,gloria是个没什么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的。我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以选择4个方向的任何一个出发,而不算成一次转弯。gloria能从一个位置走到另外一个位置吗?
 

 

Input
  第1行为一个整数t (1 ≤ t ≤ 100),表示测试数据的个数,接下来为t组测试数据,每组测试数据中,
  第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数,接下来m行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'*'表示该位置为障碍,输入数据中只有这两种字符,每组测试数据的最后一行为5个整数k, x1, y1, x2, y2 (1 ≤ k ≤ 10, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m),其中k表示gloria最多能转的弯数,(x1, y1), (x2, y2)表示两个位置,其中x1,x2对应列,y1, y2对应行。
 

 

Output
  每组测试数据对应为一行,若gloria能从一个位置走到另外一个位置,输出“yes”,否则输出“no”。
 

 

Sample Input
2 5 5 ...** *.**. ..... ..... *.... 1 1 1 1 3 5 5 ...** *.**. ..... ..... *.... 2 1 1 1 3
 

 

Sample Output
no yes
 

 

Source
 

 

Recommend
lcy
 
我的理解
BFS,注意起点与终点一样的情况。
 
我的代码
 1 #include <stdio.h>
2 #include <string.h>
3 const int N=110;
4 const int step[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
5 struct coord
6 {
7 int x,y;
8 }c1,c;
9 int m,n,k,x,y,flag,hash[N][N];
10 void init()
11 {
12 char s[N][N];
13 int i,j;
14 scanf("%d%d",&m,&n);
15 for (i=0;i<=m+1;i++) for (j=0;j<=n+1;j++) hash[i][j]=-1;
16 for (i=1;i<=m;i++)
17 {
18 scanf("%s",s[i]);
19 for (j=1;j<=n;j++)
20 if (s[i][j-1]=='.') hash[i][j]=0;
21 else hash[i][j]=-1;
22 }
23 scanf("%d%d%d%d%d",&k,&c1.y,&c1.x,&c.y,&c.x);
24 hash[c1.x][c1.y]=1;
25 }
26 int bfs()
27 {
28 coord que[N*N],c2,c3;
29 int head,tail,i;
30 que[1]=c1; head=0; tail=1;
31 if (c.x==c1.x && c.y==c1.y) return 1;
32 if (hash[c.x][c.y]==-1) return 0;
33 while (head<tail)
34 {
35 c2=que[++head];
36 if (hash[c2.x][c2.y]-2>=k) return 0;
37 for (i=0;i<4;i++)
38 {
39 c3=c2;
40 do{
41 c3.x+=step[i][0];
42 c3.y+=step[i][1];
43 if (c3.x==c.x && c3.y==c.y) return 1;
44 if (!hash[c3.x][c3.y])
45 {
46 que[++tail]=c3;
47 hash[c3.x][c3.y]=hash[c2.x][c2.y]+1;
48 }
49 }while (hash[c3.x][c3.y]!=-1);
50 }
51 }
52 return 0;
53 }
54 void main()
55 {
56 int t;
57 scanf("%d",&t);
58 while (t--)
59 {
60 init();
61 if (bfs()) printf("yes\n");
62 else printf("no\n");
63 }
64 }
posted on 2011-11-04 15:30  Qiuqiqiu  阅读(2243)  评论(0编辑  收藏  举报