1 2 3 4 5 6 7
#############################
1 # | # | # | | #
#####---#####---#---#####---#
2 # # | # # # # #
#---#####---#####---#####---#
3 # | | # # # # #
#---#########---#####---#---#
4 # # | | | | # #
#############################
(图 1)
# = Wall
| = No wall
- = No wall
方向:上北下南左西右东。
图1是一个城堡的地形图。
请你编写一个程序,计算城堡一共有多少房间,最大的房间有多大。
城堡被分割成 m∗nm∗n个方格区域,每个方格区域可以有0~4面墙。
注意:墙体厚度忽略不计。
输入格式
第一行包含两个整数 mm 和 nn,分别表示城堡南北方向的长度和东西方向的长度。
接下来 mm 行,每行包含 nn 个整数,每个整数都表示平面图对应位置的方块的墙的特征。
每个方块中墙的特征由数字 PP 来描述,我们用1表示西墙,2表示北墙,4表示东墙,8表示南墙,PP 为该方块包含墙的数字之和。
例如,如果一个方块的 PP 为3,则 3 = 1 + 2,该方块包含西墙和北墙。
城堡的内墙被计算两次,方块(1,1)的南墙同时也是方块(2,1)的北墙。
输入的数据保证城堡至少有两个房间。
输出格式
共两行,第一行输出房间总数,第二行输出最大房间的面积(方块数)。
数据范围
1≤m,n≤501≤m,n≤50,
0≤P≤150≤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 x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N=55;
int m,n;
int g[N][N];
bool st[N][N];
int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
int bfs(int sx,int sy){
int area=0;
queue<PII>q;
q.push({sx,sy});
st[sx][sy] = true;
while(q.size()){
PII t=q.front();
q.pop();
area++;
for (int i = 0; i < 4; i ++ )
{
int a = t.x + dx[i], b = t.y + dy[i];
if (a < 0 || a >= m || b < 0 || b >= n||st[a][b]) continue;
if (g[t.x][t.y]>>i&1) continue;//判断有没有墙
q.push({a,b});
st[a][b]=true;
}
}
return area;
}
int main(){
cin>>m>>n;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>g[i][j];
}
}
int cnt = 0, area = 0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(!st[i][j]){
area=max(area,bfs(i,j));
cnt++;
}
}
}
cout<<cnt<<endl<<area;
return 0;
}