1 2 3 4 5 6 7
#############################
1 # | # | # | | #
#####---#####---#---#####---#
2 # # | # # # # #
#---#####---#####---#####---#
3 # | | # # # # #
#---#########---#####---#---#
4 # # | | | | # #
#############################
(图 1)
# = Wall
| = No wall
- = No wall
方向:上北下南左西右东。
图1是一个城堡的地形图。
请你编写一个程序,计算城堡一共有多少房间,最大的房间有多大。
城堡被分割成 m∗n个方格区域,每个方格区域可以有0~4面墙。
注意:墙体厚度忽略不计。
输入格式
第一行包含两个整数 m 和 n,分别表示城堡南北方向的长度和东西方向的长度。
接下来 m 行,每行包含 n 个整数,每个整数都表示平面图对应位置的方块的墙的特征。
每个方块中墙的特征由数字 P 来描述,我们用1表示西墙,2表示北墙,4表示东墙,8表示南墙,P 为该方块包含墙的数字之和。
例如,如果一个方块的 P 为3,则 3 = 1 + 2,该方块包含西墙和北墙。
城堡的内墙被计算两次,方块(1,1)的南墙同时也是方块(2,1)的北墙。
输入的数据保证城堡至少有两个房间。
输出格式
共两行,第一行输出房间总数,第二行输出最大房间的面积(方块数)。
数据范围
1≤m,n≤50
0≤P≤15
输入样例:
4 7
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13
输出样例:
5
9
_____________________________________________________________________________
写作不易,点个赞呗!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
_____________________________________________________________________________
#include <bits/stdc++.h>
#define PII pair<int, int>
#define xx first
#define yy second
using namespace std;
const int N = 55;
int n, m, ans, cnt;
bool G[N][N][4];
bool f[N][N];
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
void bfs(int x, int y){
int S = 1;
queue<PII> Q;
Q.push({x, y});
f[x][y] = true;
while(!Q.empty()){
auto t = Q.front();
Q.pop();
for(int i = 0; i < 4; i ++){
int fx = t.xx + dx[i];
int fy = t.yy + dy[i];
if(fx < 1 || fy < 1 || fx > n || fy > m)continue;
if(G[t.xx][t.yy][i])continue;
if(f[fx][fy])continue;
S ++;
Q.push({fx, fy});
f[fx][fy] = true;
}
}
cnt = max(cnt, S);
}
void check_wall(int x, int y, int w){
for(int i = 0; i < 4; i ++){
G[x][y][i] = w & 1;
w = w >> 1;
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i ++)
for(int v, j = 1; j <= m; j ++){
cin >> v;
check_wall(i, j, v);
}
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
if(!f[i][j])bfs(i, j), ans ++;
cout << ans << "\n" <<cnt;
}