//双重墙,只算一次 #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; } /* F:\temp\22458602.966\Main.c:3:20: error: iostream: No such file or directory F:\temp\22458602.966\Main.c:4:19: error: cstring: No such file or directory F:\temp\22458602.966\Main.c:5: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'namespace' F:\temp\22458602.966\Main.c:7: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'vis' F:\temp\22458602.966\Main.c: In function 'dfs': F:\temp\22458602.966\Main.c:14: error: 'vis' undeclared (first use in this function) F:\temp\22458602.966\Main.c:14: error: (Each undeclared identifier is reported only once F:\temp\22458602.966\Main.c:14: error: for each function it appears in.) F:\temp\22458602.966\Main.c:18: error: 'true' undeclared (first use in this function) F:\temp\22458602.966\Main.c: In function 'main': F:\temp\22458602.966\Main.c:48: error: 'cin' undeclared (first use in this function) F:\temp\22458602.966\Main.c:57: error: 'vis' undeclared (first use in this function) F:\temp\22458602.966\Main.c:57: error: 'false' undeclared (first use in this function) F:\temp\22458602.966\Main.c:72: error: 'cout' undeclared (first use in this function) F:\temp\22458602.966\Main.c:72: error: 'endl' undeclared (first use in this function) */ |
Double click to view unformatted code.