码迷,mamicode.com
首页 > 其他好文 > 详细

Sicily Maze(BFS计算迷宫最短路径)

时间:2016-05-06 20:21:54      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:

 

   
 
 
1000. Maze
 
     
     
     
 
Time Limit: 1sec    Memory Limit:256MB
Description

Master Tang is kidnapped by a monster and put in a maze, and Mr. Sha wants to rescue him.

The maze is an n*m matrix. There are two types rooms in the maze, 1 for safe and 0 for dangerous. Of course, Mr. Sha can only stay in the safe room. Mr. Sha is now in the top left corner room, the room (1,1). The lower right corner room is room (n,m)
Mr. Sha can move to another safe room in up, down, left or right direction. Each move takes 1 unit of time. Mr. Sha wants to find Master Tang as soon as possible. 
Can you help Mr. Sha to count the least time to get to the room where Master Tang is in?
Input

The first line contains an integer t (0<t<=10), which means t test cases followed.

The first line of each test case contains 2 integers n (1≤n≤120) and m (1≤n≤120), which is the size of the maze. The next n lines input the maze, each line contains m number (0 or 1) seperated by one space. 
The next line contains 2 integers x (1≤n≤120) and y (1≤n≤120), which means Master Tang is in the room (x,y).
Output

For each test case, output the answer in a single line: the least time for Mr. Sha to get to Master Tang. If Mr. Sha can‘t get to Mast Tang, output -1. 

Sample Input
Copy sample input to clipboard
2
2 2
1 0
0 1
2 2
4 4 
1 1 1 0
1 0 1 1
1 0 1 1
1 1 1 0
3 4
Sample Output
-1
5

题目:从(1,1)出发,前往输入的终点救人。中间经过的房间,1表示安全,0表示危险。
如果可以到达,去最短路径。不能就输出-1.
这是一道走迷宫的题目,简单的计算最短路径就可以了。但是一直陷入queue里面。导致时间浪费。
用vector就可以了。

 1 #include<iostream>
 2 #include<cmath>
 3 #include<queue>
 4 #include<vector>
 5 
 6 using namespace std;
 7 
 8 struct node
 9 {
10     int x;
11     int y;
12 };
13 
14 //四个方向
15 int move[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
16 int n,m,room[1001][1001],x,y;
17 
18 //这条路是否可走
19 bool safe(int x,int y)
20 {
21     if(x > 0 && y > 0 && x <= n && y <= m && room[x][y] == 1)
22     {
23         return true;
24     }
25     return false;
26 }
27 
28 int main()
29 {
30     int T;
31     
32     cin>>T;
33     
34     while(T--)
35     {
36         cin>>n>>m;
37         vector <node> v[1001];
38         node no;
39         
40         for(int i = 1; i <= n; i++)
41         {
42             for(int j = 1; j <= m; j++)
43             {
44                 cin>>room[i][j];
45             }
46         }
47         cin>>x>>y;
48         no.x = 1;
49         no.y = 1;
50         v[0].push_back(no);
51         int k = 0;
52         int flag = 0;
53         //用vector做广度优先搜索
54         while(1)
55         {
56             if(v[k].begin() == v[k].end())
57             {
58                 break;
59             }
60             vector <node>::iterator it;
61             for(it=v[k].begin();it!=v[k].end();it++)
62             {
63                 node f = *it; 
64                 for(int i = 0; i < 4; i++)
65                 {
66                     node tmp;
67                     tmp.x = f.x + move[i][0];
68                     tmp.y = f.y + move[i][1];
69                     if(tmp.x == x && tmp.y == y)
70                     {
71                         flag = 1;
72                         break;
73                     }
74                     if(safe(tmp.x,tmp.y))
75                     {
76                         v[k + 1].push_back(tmp);
77                         room[tmp.x][tmp.y] = 0;
78                     }
79                 }
80             }
81             k++;
82             if(flag == 1)
83             {
84                 break;
85             }    
86         }
87         if(flag)
88         {
89             cout<<k<<endl;
90         }
91         else
92         {
93             cout<<"-1"<<endl;
94         }
95         
96     }
97     return 0;
98  } 

 

Sicily Maze(BFS计算迷宫最短路径)

标签:

原文地址:http://www.cnblogs.com/huang22/p/5466916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!