Title Link: http://poj.org/problem?id=1164
1, deep Search, each point is visited once, no mark words, do deep search, at the same time mark.
#include <iostream>#include<algorithm>#include<cstring>using namespacestd;intr,c;introoms[ -][ -],color[ -][ -];intmax=0, num=0;intArea ;voidDfsintIintj) { if(color[i][j]!=0) return ; Else{Color[i][j]=1; Area++; if((rooms[i][j]&1)==0) DFS (i,j-1);///to the West if((rooms[i][j]&2)==0) DFS (I-1, j);///to the North if((rooms[i][j]&4)==0) DFS (i,j+1);///Eastward if((rooms[i][j]&8)==0) DFS (i+1, j);///to the South }}intMain () {CIN>>r>>C; memset (color,0,sizeof(color)); for(intI=1; i<=r; i++) for(intj=1; j<=c; J + +) Cin>>Rooms[i][j]; for(intI=1; i<=r; i++) { for(intj=1; j<=c; J + +) { if(color[i][j]==0) {NUM++; Area=0; DFS (I,J); MAX=Max (Max,area); } }} cout<<NUM<<endl<<MAX; return 0;}
Depth-First search (DFS), Castle issues