The first deep search problem, although very simple, but very happy, refueling!!!
Topic Links:
Http://poj.org/problem?id=2386
The topic has been handed down several times, has been re, again wrote again finally is a
The first re is because it is s[x][y]= ' # ' placed behind DFS and will be placed in front of the original change.
Re Code, Warning:
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Chars[ the][ the];6 intN,m,total,tx,ty;7 intnext[8][2]={{1,1},{1,0},{1,-1},{0,1},{0,-1},{-1,1},{-1,0},{-1,-1}};8 intDfsintXinty)9 {Ten for(intI=0;i<8; i++) One { Atx=x+next[i][0]; -ty=y+next[i][1]; - if(tx<0|| tx>=n| | ty<0|| ty>=m) the Continue; - if(s[tx][ty]=='W') - DFS (tx,ty); -s[x][y]='#'; + } - return 0; + } A intMain () at { - while(~SCANF ("%d%d",&n,&m)) - { - for(intI=0; i<n;i++) -scanf"%s", S[i]); -Total=0; in for(intI=0; i<n;i++) - { to for(intj=0; j<m;j++) + { - if(s[i][j]=='W') the { * DFS (I,J); $total++;Panax Notoginseng } - } the } +printf"%d\n", total); A } the return 0; +}
View Code
AC Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Chars[ the][ the];6 intn,m,total;7 intnext[8][2]={{1,1},{1,0},{1,-1},{0,1},{0,-1},{-1,1},{-1,0},{-1,-1}};8 intDfsintXinty)9 {Ten intTx,ty; Ones[x][y]='#'; A for(intt=0;t<8; t++) - { -tx=x+next[t][0]; thety=y+next[t][1]; - if(tx>=n| | tx<0|| ty>=m| | ty<0) - Continue; - if(s[tx][ty]=='W') + DFS (tx,ty); - } + return 0; A } at intMain () - { - while(~SCANF ("%d%d",&n,&m)) - { - for(intI=0; i<n;i++) -scanf"%s", S[i]); inTotal=0; - for(intI=0; i<n;i++) to for(intj=0; j<m;j++) + if(s[i][j]=='W') - { the DFS (I,J); *total++; $ }Panax Notoginsengprintf"%d\n", total); - } the return 0; +}
View Code
poj-2386 Lake Counting