Topic Link: please poke here. The main idea of the topic and ideas: Read test instructions is good, is DFS find unicom block.
There's nothing to say, see the code.
#include <cstdio> #include <cstring> #include <algorithm> #define N 100+5using namespace Std;int n,m; Char g[n][n];int dir[4][2]={1,0,0,1,-1,0,0,-1};void dfs (int x,int y) {for (int i=0;i<4;i++) {int nx=x+dir[ I][0],NY=Y+DIR[I][1]; if (nx>=0 && nx<n && ny>=0 && ny<m && g[nx][ny]== ' # ') {G[NX] [ny]= '. '; This will change ' # ' to '. ', indicating that it has been visited. DFS (NX,NY); }}}int Main () {int T; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&m); for (int i=0;i<n;i++) scanf ("%s", &g[i]); int ans=0; for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {if (g[i][j]== ' # ') {g[i][j]= '. '; DFS (I,J); After each DFS, ans is + +. ans++; }}} printf ("%d\n", ans); } return 0;}
HDU 2952 counting Sheep (Dfs find unicom block)