BFS小记

题目:求从S走到G点所需步数

 

#S######.#
......#..#
.#.##.##.#
.#........
##.##.####
....#....#
.#######.#
....#.....
.####.###.
....#...G#

 

 

 

 

 

 

 

此代码BFS遍历顺序:

 

宽度优先搜索关键点:

  1. 所用容器:队列
  2. 更新:取队列的队首
  3. BFS终止条件:队列不为空
  4. 入队条件:判断边界和障碍。  注:条件满足后可追加处理代码

下面贴上代码:

 1 #include <iostream>
 2 #include <fstream>
 3 #include <queue>
 4 #include <iomanip>
 5 
 6 using namespace std;
 7 typedef pair<int, int> P;
 8 const int INF = 100000000;
 9 int M;
10 int N;
11 int d[256][256] = { 0 };
12 char maze[256][256] = {0};
13 int dx[4] = { 1, 0, -1, 0 };
14 int dy[4] = { 0, 1, 0, -1 };
15 int sx, sy;
16 int gx, gy;
17 int  BFS()
18 {
19     queue<P> que;
20     for (int i = 0; i < N; i++)
21     {
22         for (int j = 0; j < M; j++)
23         {
24             d[i][j] = INF;
25         }
26     }
27     que.push(P(sx, sy));
28     d[sx][sy] = 0;
29     while (que.size())
30     {
31         P p = que.front();
32         que.pop();
33         if (p.first == gx && p.second == gy)break;
34 
35         for (int i = 0; i < 4; i++)
36         {
37             int nx = p.first + dx[i], ny = p.second + dy[i];
38             if ( maze[nx][ny] == 'G' || 0 <= nx && nx < N && 0 <= ny && ny < M && maze[nx][ny] != '#' && maze[nx][ny] == '.' && d[nx][ny] == INF )
39             {
40                 que.push(P(nx, ny));
41                 d[nx][ny] = d[p.first][p.second] + 1;
42             }
43         }
44     }
45     return d[gx][gy];
46 
47 }
48 void solve( )
49 {
50     int res = BFS();
51     cout << endl << res << endl;
52 }
53 int main()
54 {
55     ifstream filein("data.txt");
56     int i = 0;
57     while (!filein.eof())
58     {
59         filein.getline(maze[i], 100);
60         cout << maze[i++] << endl;
61     }
62     int m = 0, n = 0;
63     while (maze[0][n])n++;
64     while (maze[m][0])
65     {
66         for (int i = 0; i < n; i++)
67         {
68             if (maze[m][i] == 'S')
69             {
70                 sx = m;
71                 sy = i;
72             }
73             else if (maze[m][i] == 'G')
74             {
75                 gx = m;
76                 gy = i;
77             }
78         }
79         m++;
80     }
81     cout << "S:" << sx << "  " << sy << "  " << endl;
82     cout << "G:" << gx << "  " << gy << "  " << endl;
83     cout << "m:" << m << endl;
84     cout << "n:" << n << endl;
85     M = m;
86     N = n;
87     solve();
88     return 0;
89 }

 

运行结果:

这是典型的BFS迷宫问题,也是最基本的BFS,需要熟练掌握,但并不困难。

 

2019/2/13更新

lintcode 897

传送门:https://www.lintcode.com/problem/island-city/

 

 1 class Solution {
 2 public:
 3     /**
 4      * @param grid: an integer matrix
 5      * @return: an integer 
 6      */
 7     class dot{
 8     public:
 9        int x;
10        int y;
11        dot(int xx,int yy){
12            this->x=xx;
13            this->y=yy;
14            
15        }
16          
17      };
18 
19     bool bfs(vector<vector<int>> &grid,int sx,int sy,int m,int n){
20         int dx[4]={0,1,0,-1};
21         int dy[4]={1,0,-1,0};
22         bool res=false;
23         queue<dot> q;
24         q.push(dot(sx,sy));
25         while(q.size()!=0){
26             dot d=q.front();
27             q.pop();
28             
29             if(grid[d.x][d.y]==2)res=true;
30             grid[d.x][d.y]=3;
31             for(int i=0;i<4;i++){
32                 int nx=d.x+dx[i];
33                 int ny=d.y+dy[i];
34                 if(nx>=0 && ny>=0 && nx<m && ny<n && grid[nx][ny]!=3 && grid[nx][ny]!=0){
35                     q.push(dot(nx,ny));
36                 }
37              }
38          }
39         
40         return res;
41          
42          
43     }
44     int numIslandCities(vector<vector<int>> &grid) {
45         // Write your code here
46         int island_count=0;
47         int m=grid.size();
48         int n=grid[1].size();
49         
50         for(int i=0;i<m;i++)
51             for(int j=0;j<n;j++){
52                 if(grid[i][j]==3||grid[i][j]==0){
53                     continue;
54                 }
55                 if(bfs(grid,i,j,m,n))
56                 {
57                     island_count++;
58                 }
59             }
60         return island_count;
61     }
62  
63 };

 

posted @ 2019-02-10 09:36  virgil_devil  阅读(177)  评论(0编辑  收藏  举报