# USACO 2.1 The Castle-漫水填充

### 题目描述

In a stroke of luck almost beyond imagination, Farmer John was sent a ticket to the Irish Sweepstakes (really a lottery) for his birthday. This ticket turned out to have only the winning number for the lottery! Farmer John won a fabulous castle in the Irish countryside.

Bragging rights being what they are in Wisconsin, Farmer John wished to tell his cows all about the castle. He wanted to know how many rooms it has and how big the largest room was. In fact, he wants to take out a single wall to make an even bigger room.

Your task is to help Farmer John know the exact room count and sizes.

The castle floorplan is divided into M (wide) by N (1 <=M,N<=50) square modules. Each such module can have between zero and four walls. Castles always have walls on their "outer edges" to keep out the wind and rain.

Consider this annotated floorplan of a castle:

     1   2   3   4   5   6   7
#############################
1 #   |   #   |   #   |   |   #
#####---#####---#---#####---#
2 #   #   |   #   #   #   #   #
#---#####---#####---#####---#
3 #   |   |   #   #   #   #   #
#---#########---#####---#---#
4 # ->#   |   |   |   |   #   #
#############################

#  = Wall     -,|  = No wall
-> = Points to the wall to remove to
make the largest possible new room

By way of example, this castle sits on a 7 x 4 base. A "room" includes any set of connected "squares" in the floor plan. This floorplan contains five rooms (whose sizes are 9, 7, 3, 1, and 8 in no particular order).

Removing the wall marked by the arrow merges a pair of rooms to make the largest possible room that can be made by removing a single wall.

The castle always has at least two rooms and always has a wall that can be removed.

### Code

/*
PROB: castle
LANG: C++
ID: yizhuor1
*/
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }

template <typename T> inline T read(){
T sum = 0;
int fl = 1,ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') fl = -1;
for(; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

const int maxn = 50 + 5;

int n,m;
int sum,cnt,ansx,ansy,ansfx,maxans;
int a[maxn][maxn][5],vis[maxn][maxn];
int dx[] = {0,-1,0,1};
int dy[] = {-1,0,1,0};

void dfs(int x,int y){  //就是个简单的漫水填充
vis[x][y] = 1;
cnt++;
for(int i = 0; i < 4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(a[x][y][i]) continue;
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && !vis[nx][ny]){
dfs(nx,ny);
}
}
}

void calc(int x,int y,int fx){  //拆除(x,y)的fx方向的墙
memset(vis,0,sizeof(vis));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(vis[i][j]) continue;
cnt = 0;
dfs(i,j);
sum++;
if(cnt > maxans){
maxans = cnt;
ansx = x;
ansy = y;
ansfx = fx;
}
}
}
}

inline void Solve (){
calc(0,0,0);
printf("%d\n%d\n",sum,maxans);
maxans = 0;
for(int j = 1; j <= m; j++){
for(int i = n; i >= 1; i--){
for(int k = 1; k < 3; k++){
if(!a[i][j][k]) continue;
a[i][j][k] = 0;
if(k == 1) a[i - 1][j][3] = 0;
if(k == 2) a[i][j + 1][0] = 0;
calc(i,j,k);
a[i][j][k] = 1;
if(k == 1) a[i - 1][j][3] = 1;
if(k == 2) a[i][j + 1][0] = 1;
}
}
}
printf("%d\n",maxans);
char ch;
if(ansfx == 1){
ch = 'N';
}else ch = 'E';
printf("%d %d %c\n",ansx,ansy,ch);
}

inline void Input (){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
int x;
}