BFS識別矩陣中的塊數

2021-08-20 03:22:57 字數 614 閱讀 5799

題目描述:

給出乙個m*n的矩陣,矩陣中的元素為0或1.稱位置(x,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

輸出:4

#include #include#includeusing namespace std; const int maxn=100; struct node node; int n,m; int matrix[maxn][maxn]; bool inq[maxn][maxn]=; int x[4]=; int y[4]=; bool judge(int x,int y) void bfs(int x,int y)         }     } }

int main()     }     int ans=0;     for(int x=0;x

BFS 求給定的矩陣中「塊」的個數

給出乙個 m n 的矩陣,矩陣中的元素為 0 或 1。稱位置 x,y 與其上 下 左 右四個位置是相鄰的。如果矩陣中有若干個 1 是相鄰的 不必考慮兩兩相鄰 那麼稱這些 1 構成了乙個 塊 求給定矩陣中 塊 的個數。4思路 對於矩陣 matrix m n 遍歷每個元素,如果為0,則跳過 如果為 1 ...

BFS寬搜模板(求矩陣中連通塊的個數)

給出乙個n m的矩陣,元素為0或1.稱每個位置的上下左右與之相鄰。如果矩陣中有若干個1是相鄰的,那麼這些1就構成了乙個塊。求矩陣中 塊 的個數。include include include include include include using namespace std typedef lo...

Algorithm 矩陣中「塊」的個數

以下分別給出dfs和bfs實現方法 include include using namespace std define maxn 1000 define ptf printf n define here a,b printf here here here d,d n a,b int n,m,num...