CC

自然万物都趋向从有序变得无序

0%

逃离迷宫 HDU - 1728

题目链接:点我

题目


给定一个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, x 1, y 1, x 2, y 2 (1 ≤ k ≤ 10, 1 ≤ x 1, x 2 ≤ n, 1 ≤ y 1, y 2 ≤ m),其中k表示gloria最多能转的弯数,(x 1, y 1), (x 2, y 2)表示两个位置,其中x 1,x 2对应列,y 1, y 2对应行。

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

题意:

中文题目,不用说了.

思路:

这题有个要求,即转向数不能超过k,这样的话我们可以每次固定一个方向扩展,直到不能扩展为止,每次换一个方向转向数加1即可.

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;

bool vis[110][110];
char w[110][110];
int n,m;
int px,py,sx,sy,k;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};

struct ss
{
int x,y,turn;
};

void bfs()
{
memset(vis,false,sizeof(vis));
ss q[10000+10];
int head=0;
int tail=0;
q[tail].x=sx;
q[tail].y=sy;
q[tail].turn=-1;
// w[sx][sy]='*';
// vis[sx][sy]=true;
tail++;
while(head<tail){
ss p=q[head++];
for(int i=0;i<4;++i){
int x=p.x+dx[i];
int y=p.y+dy[i];
while(x>0&&y>0&&x<=n&&y<=m&&w[x][y]=='.'){
if(!vis[x][y]){
if(x==px&&y==py&&k-1>=p.turn){
cout<<"yes"<<endl;
return ;
}
vis[x][y]=true;
q[tail].x=x;
q[tail].y=y;
q[tail++].turn=p.turn+1;
}
x+=dx[i];
y+=dy[i];
}
}
}
cout<<"no"<<endl;
}
int main()
{
int t;
scanf("%d", &t);
while(t--){
scanf("%d %d",&n,&m);
for(int i=1;i<=n;++i)
scanf("%s",w[i]+1);
scanf("%d %d %d %d %d",&k,&sy,&sx,&py,&px);
if(sx==px&&sy==py){
cout<<"yes"<<endl;
continue;
}
bfs();
}
return 0;
}