Background: The main is familiar with BFS writing, although 1Y, but debugging in the middle several times, there are small mistakes. Read the @liujc, DFS can also, but feel bfs more efficient.
My Code:
#include <cstdio> #include <iostream> #include <cstring> #include <queue> using namespace std;
Char diagram[109][109]; struct Place{int x,y;}
TEMP1,TEMP2;
int dir[8][2]={1,0,-1,0,0,1,0,-1,1,1,1,-1,-1,1,-1,-1},n,m;
void Pond (int i,int j) {diagram[i][j]= '. ';
Temp1.x=i;
Temp1.y=j;
Queue<place> Q;
Q.push (TEMP1);
while (!q.empty ()) {Temp1=q.front ();
for (int k=0;k < 8;k++) {temp2.x=temp1.x+dir[k][0];
TEMP2.Y=TEMP1.Y+DIR[K][1];
if (diagram[temp2.x][temp2.y] = = ' W ') {diagram[temp2.x][temp2.y]= ';
Q.push (TEMP2);
} q.pop ();
int main (void) {while (~scanf ("%d%d", &n,&m)) {GetChar ();
memset (Diagram, '. ', sizeof (diagram));
for (int i=1;i <= n;i++) {for (int j=1;j <= m;j++) scanf ("%c", &diagram[i][j]);
GetChar ();
int count=0; Forint I=1;i <= n;i++) {for (int j=1;j <= m;j++) {if (diagram[i][j] = = ' W ') {
Pond (I,J);
count++;
} printf ("%d\n", count);
return 0;
}