解题思路:BFS
注意事项:
参考代码:
#include<iostream> #include<queue> using namespace std; const int N = 1e3 +10; char a[N][N]; int n,m,cnt; bool v[N][N]; struct node{ int x, y; }; int d[4][2] = {{-1,0},{0,1},{1,0},{0,-1}}; queue<node> q; void bfs(int x, int y){ q.push(node{x,y}); v[x][y] = true; while(!q.empty()){ node nd = q.front(); q.pop(); for(int i = 0; i < 4; i ++){ int nx = nd.x + d[i][0]; int ny = nd.y + d[i][1]; if(nx >= 0 && nx < n && ny >= 0 && ny < m && !v[nx][ny] && a[nx][ny] != '0'){ v[nx][ny] = true; q.push(node{nx,ny}); } } } } int main() { cin >> n >> m; for(int i = 0; i < n; i ++) for(int j = 0; j < m; j ++) cin >> a[i][j]; for(int i = 0; i < n; i ++) for(int j = 0; j < m; j ++) if(!v[i][j] && a[i][j] != '0') { bfs(i,j); cnt ++; } cout << cnt; return 0; }
0.0分
1 人评分
C语言训练-排序问题<1> (C++代码)浏览:598 |
【亲和数】 (C语言代码)浏览:501 |
C语言程序设计教程(第三版)课后习题6.10 (C语言代码)浏览:880 |
【计算直线的交点数】 (C语言代码)浏览:1450 |
A+B for Input-Output Practice (V) (C语言代码)浏览:466 |
Tom数 (C语言代码)浏览:527 |
钟神赛车 (C语言代码)浏览:595 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:470 |
1005答案错误为什么浏览:1975 |
陶陶摘苹果2 (C++代码)浏览:541 |