bfs 求0—1矩阵的块数

本文介绍了一种使用广度优先搜索(BFS)算法来计算二维矩阵中连通的1的数量的方法。通过定义一个简单的结构体和利用队列进行遍历,确保每个连通区域仅被访问一次,从而高效地找出所有独立的连通块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//1.求块数
/*#include <stdio.h>
#define maxn 100
typedef struct 
{
int x,y;
}node;


int n,m;//表示矩阵的大小;
int inq[maxn][maxn]={0};//记录位置(x,y)是否入过队列
int X[4]={0,0,1,-1};
int Y[4]={1,-1,0,0};
int matrix[maxn][maxn];//表示0 1 矩阵
int judge(int x,int y)//判断(x,y)是否需要访问
{
if(x>=n||x<0||y>=m||y<0)return 0;//越界不用访问
if(matrix[x][y]==0||inq[x][y]==1)return 0;//如果是0 的话或者已经入队列了,就不用访问了
return 1;//都不满足的前提下返回1;
}
void BFS(int x,int y)
{
node p,top,q[maxn];//分别定义了一个节点,和一个队列
int newX,newY,i;
int rear=0,front=0;
//Queue queue;
//queue.front=queue.rear=0;//初始化队列
p.x=x,p.y=y;//当前的节点定义为(x,y);
q[rear++]=p;//我们首先将p这个节点入队列;
inq[x][y]=1;//将入队的(x,y)设置1,表示已经入队了
while(front!=rear)//如果队列不空的话
{
top=q[front++];//将队首出队列,而此时top也是队首元素
for(i=0;i<4;i++)//此时将符合条件的周围的节点都入队后设置为1.
{
newX=top.x+X[i];
newY=top.y+Y[i];
if(judge(newX,newY)==1)//如果新位置需要访问的话,
{
p.x=newX;
p.y=newY;
q[rear++]=p;//将节点p加入队列
inq[newX][newY]=1;//设置(newX,newY)已经入队列
}
}
}
}
int main()
{
int x,y,ans=0;
//Queue queue;
//queue.front=queue.rear=0;//初始化队列
scanf("%d%d",&n,&m);
for(x=0;x<n;x++)
for(y=0;y<m;y++)
scanf("%d",&matrix[x][y]);
for(x=0;x<n;x++)
{
for(y=0;y<m;y++)
{
if(matrix[x][y]==1&&inq[x][y]==0)//如果元素为1,且表示米有入队
{
printf("[%d][%d] ",x,y);
ans++;
BFS(x,y);//访问整个块,将整个块的'1'都标记为1;
}
}
}
printf("%d\n",ans);
return 0;
}*/
以下是使用广度优先搜索(BFS)算法解可达矩阵的C++代码示例: ```cpp #include <iostream> #include <queue> #include <vector> using namespace std; // 定义矩阵的行列 const int ROWS = 5; const int COLS = 5; // 定义矩阵中的四个方向 const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, 1, -1}; // 判断坐标(x, y)是否在矩阵内 bool isValid(int x, int y) { return x >= 0 && x < ROWS && y >= 0 && y < COLS; } // 使用BFS算法解可达矩阵 vector<vector<bool>> findReachableMatrix(vector<vector<int>>& matrix) { vector<vector<bool>> reachable(ROWS, vector<bool>(COLS, false)); vector<vector<bool>> visited(ROWS, vector<bool>(COLS, false)); queue<pair<int, int>> q; q.push({0, 0}); visited[0][0] = true; reachable[0][0] = true; while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (isValid(nx, ny) && !visited[nx][ny] && matrix[nx][ny] == 1) { q.push({nx, ny}); visited[nx][ny] = true; reachable[nx][ny] = true; } } } return reachable; } int main() { // 定义示例矩阵 vector<vector<int>> matrix = { {1, 0, 1, 0, 1}, {1, 1, 0, 1, 0}, {0, 1, 1, 0, 1}, {0, 0, 1, 1, 0}, {1, 1, 1, 1, 1} }; // 解可达矩阵 vector<vector<bool>> reachableMatrix = findReachableMatrix(matrix); // 输出可达矩阵 for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { cout << reachableMatrix[i][j] << " "; } cout << endl; } return 0; } ``` 这段代码使用了一个二维矩阵来表示给定的图,其中1表示可以到达的位置,0表示不可到达的位置。通过BFS算法遍历矩阵,将所有可达的位置标记为true,并将结果存储在`reachableMatrix`中。最后,将结果输出到控制台上。 请注意,这只是一个简单的示例代码,实际应用中可能需要根据具体需进行相应的修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值