标签:
dfs不需要维护栈,更简短一些
#include<cstdio> #include<queue> using namespace std; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int cell[200][200]; void dfs(int x, int y) { cell[x][y] = 0; for(int d = 0; d < 4; d++) { int tx = x + dx[d]; int ty = y + dy[d]; if(cell[tx][ty]) dfs(tx, ty); } } int main() { int n, m, i, j, sum = 0; scanf("%d%d", &n, &m); for(i = 1; i <= n; i++) for(j = 1; j <= m; j++) scanf("%1d", &cell[i][j]); for(i = 1; i <= n; i++) { for(j = 1; j <= m; j++) { if(cell[i][j]) { sum++; dfs(i, j); } } } printf("%d\n", sum); return 0; } /* 4 10 0234500067 1034560500 2045600671 0000000089 */
标签:
原文地址:http://www.cnblogs.com/4bytes/p/5579300.html