解题思路: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.5 (C语言代码)浏览:640 |
钟神赛车 (C++代码)浏览:866 |
妹子杀手的故事 (C语言代码)浏览:1234 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:484 |
C语言程序设计教程(第三版)课后习题7.4 (C语言代码)浏览:1246 |
C语言程序设计教程(第三版)课后习题8.5 (C语言代码)浏览:937 |
C语言程序设计教程(第三版)课后习题8.6 (C语言代码)浏览:567 |
【计算两点间的距离】 (C语言代码)浏览:1494 |
sizeof的大作用 (C语言代码)浏览:1028 |
数组与指针的问题浏览:718 |