解题思路: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语言训练-数字母 (C语言代码)浏览:649 |
WU-图形输出 (C++代码)浏览:800 |
2004年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:1327 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:660 |
字符逆序 (C语言代码)浏览:609 |
DNA (C语言代码)浏览:390 |
2003年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:667 |
C二级辅导-阶乘数列 (C语言代码)浏览:508 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:663 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:1181 |