//双重墙,只算一次 #include <iostream> #include <cstring> using namespace std; int map[55][55]; bool vis[55][55]; int area = 0,ans = 0; int col,row; void dfs(int i,int j) { if(i<1||j<1||i>row||j>col) return ; if(vis[i][j]) return ; else { vis[i][j] = true; ++ans; //可能好几个方向均没墙,所以不是级联式if语句 if(map[i][j]%2 == 0) // 1 2 4 8 西边肯定没墙 { if(j>=2) dfs(i,j-1); } map[i][j] /= 2; if(map[i][j]%2 == 0) // 0 1 2 4 北边肯定没墙 { if(i>=2) dfs(i-1,j); } map[i][j] /= 2; if(map[i][j]%2 == 0) // 0 0 1 2 东边肯定没墙 { if(j<=col-1) dfs(i,j+1); } map[i][j] /= 2; if(map[i][j]%2 == 0) // 0 0 0 1 南边肯定没墙 { if(i<=row-1) dfs(i+1,j); } } } int main() { while(cin>>row>>col) { memset(map,0,sizeof(map)); int i,j; for(i=1 ;i<=row ;i++) for(j=1 ;j<=col ;j++) cin>>map[i][j]; ans = 0; int count = 0; memset(vis,false,sizeof(vis)); for(i=1 ;i<=row ;i++) for(j=1 ;j<=col ;j++) { if(!vis[i][j]) { ans = 0; dfs(i,j); if(ans>area) area=ans; //联通的小方格算一个房间 if(ans) ++count; } } cout<<count<<endl; cout<<area<<endl; } return 0; } |
Double click to view unformatted code.