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 <queue>
#include <cstdio>
#include <iostream>
#define x first
#define y second
using namespace std;
const int N = 55;
typedef pair<int, int> PII;
int ans;
int n, m;
int g[N][N];
bool st[N][N];
int dx[4] = {0, -1, 0, 1};//分别定义为西,北,东,南
int dy[4] = {-1, 0, 1, 0};
void bfs(int a, int b)
{
queue<PII> q;
q.push({a, b});
st[a][b] = true;
int res = 0;
while (q.size())
{
auto t = q.front();
q.pop();
res ++ ;
for (int i = 0; i < 4; i ++ )
{
int x = t.x + dx[i], y = t.y + dy[i];
if (x < 1 || x > n || y < 1 || y > m) continue;
if (g[t.x][t.y] >> i & 1) continue;//若在某个方向上存在墙
if (st[x][y]) continue;
st[x][y] = true;
q.push({x, y});
}
}
ans = max(ans, res);
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
cin >> g[i][j];
int cnt = 0;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
{
if (!st[i][j])
{
cnt ++ ;
bfs(i, j);
}
}
cout << cnt << endl;
cout << ans << endl;
return 0;
}