【算法笔记第8.2节-BFS 】广度优先搜索

  1. 给出一个m x n的矩阵,矩阵中的元素为0或1。称位置(x, y)与其上下左右四个位置(x,y+1)、(x, y-1)、(x+1, y)、(x-1, y)是相邻的。如果矩阵中有若干个1是相连的(不必两两相连),那么称这些1构成了一个“块”。求给定的矩阵中“块”的个数。
    0 1 1 1 0 0 1
    0 0 1 0 0 0 0
    0 0 0 0 1 0 0
    0 0 0 1 1 1 0
    1 1 1 0 1 0 0
    1 1 1 1 0 0 0

    m = 6, n=7;

    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<memory.h>
    #include<stdio.h>
    #include<vector>
    using namespace std;
    int dis[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};
    int n,m,sum,sx, sy, ex, ey;
    int vis[20][20];
    int a[20][20];
    struct Point
    {
        int x, y;
    };
    vector<Point> ans;
    int flag ;
    void bfs(int x, int y)
    {
        queue<Point> q;
        Point t;
        t.x = x;
        t.y = y;
        vis[x][y]=1;
        q.push(t);
        while(!q.empty())
        {
            Point top = q.front();
            q.pop();
            for(int i=0; i<4; i++)
            {
                int tx,ty;
                tx = top.x + dis[i][0];
                ty = top.y + dis[i][1];
                if(tx>=0&&tx<n&&ty>=0&&ty<m&&a[tx][ty]==1&&vis[tx][ty]==0)
                {
                    t.x = tx;
                    t.y = ty;
                    q.push(t);
                    vis[tx][ty] = 1;
                }
            }
        }
    
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
           for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                scanf("%d", &a[i][j]);
           int ans = 0;
           for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
           {
               if(a[i][j]==1&&vis[i][j]==0)
               {
                   ans++;
                   bfs(i,j);
               }
           }
           printf("%d\n",ans);
        }
        return 0;
    }
    /*
    
    6 7
    0 1 1 1 0 0 1
    0 0 1 0 0 0 0
    0 0 0 0 1 0 0
    0 0 0 1 1 1 0
    1 1 1 0 1 0 0
    1 1 1 1 0 0 0
    4
    */
    

     

  2. 给定一个n*m大小的迷宫,其中*代表不可通过的墙壁,而“.”代表平地,S表示起点,T代表终点。移动过程中,如果当前位置为(x,y)(下标从0开始), 且每次只能前往上下左右四个位置的平地,求从起点S到达终点T的最少步数。

.....
.*.*.
.*S*.
.***.
...T*
#include<iostream>
#include<algorithm>
#include<queue>
#include<memory.h>
#include<stdio.h>
#include<vector>
using namespace std;
int dis[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};
int n,m;
char a[20][20];
struct Point
{
    int x, y;
    int step; //step 为从起点S到达该位置的最小步数(即层数)
}s, e;
int bfs( )
{
    queue<Point> q;
    q.push(s);//将起点S入队
    while(!q.empty())
    {
        Point top = q.front();//取队首元素
        q.pop();    //队首元素出队
        if(top.x==e.x&&top.y==e.y)
        {
            return top.step;//找到重点,直接返回最少步数
        }
        for(int i=0; i<4; i++)//找相邻位置
        {
            int tx = top.x + dis[i][0];
            int ty = top.y + dis[i][1];
            if(tx>=0&&tx<n&&ty>=0&&ty<m&&(a[tx][ty]=='.'||a[tx][ty]=='T'))//走到墙壁或者终点
            {
                Point temp;
                temp.x = tx;
                temp.y = ty;
                temp.step = top.step+1;
                q.push(temp);//入队
                a[tx][ty] = '*';//此处已经走过了,标记为墙壁
            }
        }
    }
   return -1;//无法到达终点返回-1
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
       for(int i=0; i<n; i++)
       {
        getchar();//过滤掉每行后面的换行符
        for(int j=0; j<m; j++)
            scanf("%c", &a[i][j]);
       }

       scanf("%d%d%d%d",&s.x, &s.y, &e.x,&e.y);//起点和终点的坐标
       s.step = 0;
       a[s.x][s.y] = '*';
       printf("%d\n", bfs());
    }
    return 0;
}
/*

5 5
.....
.*.*.
.*S*.
.***.
...T*
2 2 4 3
11

*/

3. 队列中放置的只是原元素的副本。

#include<iostream>
#include<algorithm>
#include<queue>
#include<memory.h>
#include<stdio.h>
#include<vector>
using namespace std;
/*
队列中放置的只是原元素的副本
*/
struct Point
{
    int data;
}a[10];

int main()
{
    queue<Point> q;
    for(int i=1; i<=3; i++)
    {
        a[i].data = i;//a[1] = 1, a[2]=2, a[3]=3
        q.push(a[i]);
    }
    //尝试直接把队首元素的数据域改为100
    q.front().data = 100;
    //事实上对队列元素的修改无法改变原元素
    printf("%d %d %d\n", a[1].data, a[2].data, a[3].data);
    //然后尝试直接修改a[1]的数据域为200,
    a[1].data=200;
    //对原元素的修改也无法改变队列中的元素
    printf("%d\n", q.front().data);
    return 0;

}
/*
1 2 3
100


*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值