[VJ][bfs][迷宫转弯]逃离迷宫

逃离迷宫

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, 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”。

Examples

Input

2
5 5
...**
*.**.
.....
.....
*....
1 1 1 1 3
5 5
...**
*.**.
.....
.....
*....
2 1 1 1 3

Output

no
yes

正确解法:

以前是找迷宫要走多少路,今天是找转了多少个弯。

每走一点转弯就记录一下,然后一直朝着这个方向走,直到走到尽头。再开始转另一个弯。

有一个坑的点是 这个点上一次已经走过了 book数组记录过了 ,但你也可以再走,只是不重复记录在队列里而已。

把 if 改成 while

初始化的那个点 k=-1.

最近太累了,不想说话了,直接看code吧。

 1 #include "pch.h"
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<string>
 5 #include<cstring>
 6 #include<cmath>
 7 #include<algorithm>
 8 using namespace std;
 9 int t, n, m, kk,aa, bb, cc, dd;
10 char a[110][110];
11 bool book[110][110];
12 int next1[5][2] = { {-1,0},{1,0},{0,1},{0,-1} };
13 struct student
14 {
15     int x, y, k;
16 }que[100010];
17 bool check(int x, int y)
18 {
19     if (x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] == '.')
20         return 1;
21     return 0;
22 }
23 int bfs(int x1,int y1,int x2,int y2)
24 {
25     if (x1 == x2 && y1 == y2)    return -1;
26     int head = 1, tail = 2;
27     que[head].x = x1;
28     que[head].y = y1;
29     que[head].k = -1; 
30     book[x1][y1] = 1;
31     while (head < tail)
32     {
33         if (que[head].x == x2 && que[head].y == y2)    return que[head].k;
34         for (int i = 0; i < 4; i++)
35         {
36             int tx = que[head].x + next1[i][0];
37             int ty = que[head].y + next1[i][1];
38             while (check(tx, ty))
39             {
40                 if (book[tx][ty] == 0)
41                 {
42                     tail++;
43                     que[tail].x = tx;
44                     que[tail].y = ty;
45                     que[tail].k = que[head].k + 1;
46                     book[tx][ty] = 1;
47                     if (tx == x2 && ty == y2)    return que[tail].k;
48                 }
49                 tx = tx + next1[i][0];
50                 ty = ty + next1[i][1];
51             }
52         }
53         head++;
54     }
55 }
56 int main()
57 {
58     cin >> t;
59     while (t--)
60     {
61         cin >> n >> m;
62         memset(a, 0, sizeof(a));
63         memset(book, 0, sizeof(book));
64         memset(que, 0, sizeof(que));
65         for (int i = 1; i <= n; i++)
66             for (int j = 1; j <= m; j++)
67                 cin >> a[i][j];
68         cin >> kk >> aa >> bb >> cc >> dd;
69         int aaa = bfs(bb, aa, dd, cc);
70         if (aaa <= kk)    cout << "yes" << endl;
71         else cout << "no" << endl;
72     }
73     return 0;
74 }
View Code
No matter how you feel, get up , dress up , show up ,and never give up.
原文地址:https://www.cnblogs.com/Kaike/p/9936981.html