标签:return turn void bfs cout tchar main 组成 vector
题目描述
一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右若还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数。(1<=m,n<=100)?
输入输出格式
### 输入格式:
输入:整数m,n(m行,n列)
矩阵
输出格式:
输出:细胞的个数
输入输出样例
输入样例#1:
4 10
0234500067
1034560500
2045600671
0000000089
输出样例#1:
4
暴力跑bfs,对每个非0的点进行dfs并把和它相邻的点赋值为0,统计数量即可
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string>
#include<cstring>
using namespace std;
const int maxn=999999999;
const int minn=-999999999;
char a[108][108];
int n,m,ans;
inline int read() {
char c = getchar();
int x = 0, f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
void dfs(int x,int y) {
if(x>n||x<1||y>m||y<1||a[x][y]=='0')
return;
a[x][y]='0';
dfs(x+1,y);
dfs(x,y+1);
dfs(x-1,y);
dfs(x,y-1);
}
int main() {
cin>>n>>m;
for(int i=1; i<=n; ++i) {
for(int j=1; j<=m; ++j) {
cin>>a[i][j];
}
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
if(a[i][j]!='0')
ans++,dfs(i,j);
}
}
cout<<ans;
return 0;
}
标签:return turn void bfs cout tchar main 组成 vector
原文地址:https://www.cnblogs.com/pyyyyyy/p/10720152.html