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
其实啊,这题非常简单,因为他只关心走不走的到,那么bfs每次入队的时候,把所有方向能练成一排的全部入队就行了,就是变通了一下,结果难到一片人哈哈哈。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <queue>
 6 #include <utility>
 7 char a[103][103];    
 8 bool vis[103][103];
 9 int cishu[103][103];
10 int nextt[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
11 int m,n;
12 using namespace std;
13 int main()
14 {
15     int t;
16     scanf("%d",&t);
17     while(t--)
18     {
19         memset(cishu,0,sizeof(cishu));
20         memset(a,0,sizeof(a));
21         memset(vis,0,sizeof(vis));
22         scanf("%d%d",&m,&n);
23         for(int i=0;i<m;i++)
24         {
25             scanf("%s",a[i]);    
26         }
27         int x1,x2,y1,y2,k;
28         cin>>k>>y1>>x1>>y2>>x2;
29         queue<pair<int,int> > q;
30         vis[x1-1][y1-1]=true;
31         cishu[x1-1][y1-1]=-1;
32         q.push(make_pair(x1-1,y1-1));
33         int flag=false;
34         while(!q.empty())
35         {    
36             int x=q.front().first;
37             int y=q.front().second;
38             q.pop();
39             if((x==x2-1)&&(y==y2-1)&&cishu[x][y]<=k)
40             {
41                 flag=true;
42             }
43             for(int i=0;i<4;i++)
44             {
45                 int nx=x+nextt[i][0];
46                 int ny=y+nextt[i][1];
47                 while(nx>=0&&nx<m&&ny<n&&ny>=0&&a[nx][ny]=='.')
48                 {     
49                     if(vis[nx][ny]==false)
50                     {    
51                         cishu[nx][ny]=cishu[x][y]+1;                    
52                         vis[nx][ny]=true;
53                         q.push(make_pair(nx,ny));
54                     }
55                 int tx=nx+nextt[i][0];
56                 int ty=ny+nextt[i][1];
57                 nx=tx;
58                 ny=ty;
59                 }
60             }
61         }
62         if(flag)
63         cout<<"yes"<<endl;
64         else
65         cout<<"no"<<endl;
66     }
67 }

 

posted @ 2019-07-06 23:15  coolwx  阅读(117)  评论(0编辑  收藏  举报