# （BFS）1097: Yuchang and Zixiang ‘s maze

1097: Yuchang and Zixiang ‘s maze
Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 863 Solved: 149

Description

One day , Yuchang and Zixiang go out of school to find some thing interesting . But both of them is LuChi , so the miss the way easily .
“Where am I ? who am I ?” Yuchang says . “Who attack you? You must want to say .” Zixiang adds . “Don’t say that , how we get out there ? I want my mom 555555……” Yuchang started crying . Zixiang
become very panic . He doesn’t know how to get out there.
Now , they find they are in a N*M maze , they are at the (a,b) point , they know they want to go to the point (c,d) , they want to finish as soon as possible, so , could you help them ?
Same as other maze , there are some point has boom , means they can’t get the point . Give you N,M and Num (the number of points that have booms . ) , then , Num lines contains pairs (x,y) means
point (x,y) have booms . then , one line contains a , b , c , d ,the begin and end point .
They can mov forward , back , left and right . And every move cost 1 second . Calculate how many seconds they need to get to the finish point .

Input

The first line contains tow numbers N,M (0 < x,y < 1000)means the size of the maze.
The second line contains a number Num (0 < N < X*Y), means the number of points which have booms .
Then next N lines each contain two numbers , xi,yi , means (xi,yi) has a boom .

Output
One line , contains one number , the time they cost .
If they can’t get to the finish point , output -1 .

Sample Input
1000 1000 4
5 5
5 7
4 6
6 6
1 1 5 6

Sample Output
-1

“我在哪儿?”我是谁?”“谁攻击你?你一定想说。”子巷补充道。“别说了，我们怎么出去的?”我要我妈妈555555……”余昌哭了起来。子巷变得非常恐慌。他不知道怎么出去。

1000 1000 4
5 5
5 7
4 6
6 6
1 1 5 6

-1

  1 #include<bits/stdc++.h>
2 using namespace std;
3
4 const int maxn = 1010;
5 char maze[maxn][maxn];    //存储迷宫
6 int vis[maxn][maxn];    //存储是否访问过标记
7 int step[maxn][maxn];    //存储步数
8
9 int n, m;    //m,n分别是迷宫的大小，在check中判断越界要使用，需要声明为全局变量
10
11 int Move[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};    //四个方向可走，使用for循环来对当前坐标进行上下左右移动
12
13 struct point    //结构体存储x,y的坐标，结构体的对象放在队列中
14 {
15     int x, y;
16 } in, out, beg;
17
18 int check(int x, int y)
19 {
20     if(vis[x][y] == 0 && x >= 1 && x <= n && y >= 1 && y <= m && maze[x][y] != '#')
21         return 1;
22     else
23         return 0;
24 }
25
26 int bfs()
27 {
28     memset(vis, 0, sizeof(vis));    //初始所有坐标的访问设置为0
29     memset(step, 0, sizeof(step));    //初始所有步数为0
30
31     vis[beg.x][beg.y] = 1;    //beg坐标点开始标记为1，表示已经访问
32     step[beg.x][beg.y] = 0;    //beg所在的坐标为第一个，步数为0
33
34     queue<point>q;
35
36     q.push(beg);    //开始的那个点进队
37
38     while(!q.empty())
39     {
40         out = q.front();    //out记住队头元素
41         q.pop();
42         for(int i = 0; i < 4; i++)
43         {
44             in.x = out.x + Move[i][0];    //循环四次，in分别是队头元素的上、下、左、右邻接坐标点
45             in.y = out.y + Move[i][1];
46             if(check(in.x, in.y))    //对in作是否访问过、越界、是否是障碍检查
47             {
48                 if(maze[in.x][in.y] == 'E')    //判断in是否是到达的坐标
49                 {
50                     return step[out.x][out.y] + 1;    //若到达,返回in前一个坐标的步数+1
51                 }
52                 q.push(in);   //不是终点，继续将in进队
53                 vis[in.x][in.y] = 1;
54                 step[in.x][in.y] = step[out.x][out.y] + 1;    //    将in的步数在它前一个坐标的步数+1
55             }
56         }
57     }
58     return -1;
59 }
60
61 int main()
62 {
63     while(~scanf("%d%d", &n, &m))
64     {
65         for(int i = 1; i <= n; i++)
66         {
67             for(int j = 1; j <= m; j++)
68             {
69                 maze[i][j] = '.';
70             }
71         }
72         int t;
73         scanf("%d", &t);
74         while(t--)
75         {
76             int x, y;
77             scanf("%d%d", &x, &y);
78             maze[x][y] = '#';    //将障碍的坐标设置为#
79         }
80         int a, b, c, d;
81         scanf("%d%d%d%d", &a, &b, &c, &d);
82         if(a == c && b == d)    //判断如果出发点和终点坐标一样，就直接输出0
83         {
84             printf("0\n");
85         }
86         else    //否则进行广度搜索
87         {
88             maze[a][b] = 'S';
89             maze[c][d] = 'E';
90             for(int i = 1; i <= n; i++)
91             {
92                 for(int j = 1; j <= m; j++)
93                 {
94                     if(maze[i][j] == 'S')    //    找到起始点，将起始点的坐标存入结构体变量beg中
95                     {
96                         beg.x = i;
97                         beg.y = j;
98                     }
99                 }
100             }
101             int ans = bfs();
102             printf("%d\n", ans);
103         }
104     }
105     return 0;
106 }

posted @ 2019-03-14 11:44  WindSun  阅读(195)  评论(0编辑  收藏  举报