BFS用洪水填充染色。 记录每个颜色的房间数量
然后按照从下往上,从左往右的顺序穷举一边,拆掉墙,如果墙两边的房间颜色相同,跳过。 颜色不同,把颜色所代表的房间数量相加,和答案对比,更新答案即可。
Compiling...
Compile: OK
Executing...
Test 1: TEST OK [0.008 secs, 3572 KB]
Test 2: TEST OK [0.008 secs, 3572 KB]
Test 3: TEST OK [0.005 secs, 3572 KB]
Test 4: TEST OK [0.008 secs, 3572 KB]
Test 5: TEST OK [0.008 secs, 3572 KB]
Test 6: TEST OK [0.008 secs, 3704 KB]
Test 7: TEST OK [0.008 secs, 3572 KB]
Test 8: TEST OK [0.022 secs, 3704 KB]
All tests OK.
/*
TASK:castle
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int wall[55][55]={0};
vector<int>path[2510];
int m, n, t=0;
int my_map[55][55]={0}, colo[2510] = {0}, colo_kinds=0;
int room_size[2510];
const int dx[]={0,0,-1,0,1};
const int dy[]={0,-1,0,1,0};
const int D[] = {0,1,2,4,8};
void init()
{
for (int i = 1; i <= m; ++ i)
for (int j = n; j != 0; -- j) my_map[j][i] = ++t;
for (int i = 1; i <= n; ++ i)
for (int j = 1; j <= m; ++ j)
{
cin >> wall[i][j];
for (int k = 1; k <= 4; ++ k)
if (!(wall[i][j] & D[k])) path[my_map[i][j]].push_back(my_map[i + dx[k]][j + dy[k]]);
}
}
queue<int>q;
void doit()
{
int max_room = 0;
for (int i = 1; i <= m; ++ i)
for (int j = n; j != 0; -- j)
{
int tmp = my_map[j][i];
if (colo[tmp]) continue;
q.push(tmp);
colo[tmp] = ++ colo_kinds;
room_size[colo_kinds] = 1;
while (!q.empty())
{
int now = q.front();
q.pop();
for (int i = 0; i != path[now].size(); ++ i)
{
int will = path[now][i];
if (!will || colo[will]) continue;
++ room_size[colo_kinds];
colo[will] = colo_kinds;
q.push(will);
}
max_room = max(max_room, room_size[colo_kinds]);
}
}
cout<<colo_kinds<<endl;
cout<<max_room<<endl;
max_room = 0;
int dir(0);
int tmpx, tmpy;
for (int i = 1; i <= m; ++ i)
for (int j = n; j != 0; -- j)
{
for (int k = 1; k <= 4; ++ k)
{
int now = colo[my_map[j][i]];
int will = colo[my_map[j + dx[k]][i + dy[k]]];
if (now == will) continue;
int tmp = room_size[now] + room_size[will];
if (tmp > max_room)
{
tmpx = j;
tmpy = i;
max_room = tmp;
dir = k;
}
}
}
cout<<max_room<<endl;
cout<<tmpx<<" "<<tmpy<<" ";
if (dir == 2) cout<<'N'<<endl;
else cout<<'E'<<endl;
}
int main()
{
freopen("castle.in", "r", stdin);
freopen("castle.out", "w", stdout);
std::ios::sync_with_stdio(false);
cin >>m >> n;
init();
doit();
return 0;
}