USACO-cha2-sec2.1 The Castle

The Castle
IOI'94 - Day 1

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.

PROGRAM NAME: castle

INPUT FORMAT

The map is stored in the form of numbers, one number for each module, M numbers on each of N lines to describe the floorplan. The input order corresponds to the numbering in the example diagram above.

Each module number tells how many of the four walls exist and is the sum of up to four integers:

  • 1: wall to the west
  • 2: wall to the north
  • 4: wall to the east
  • 8: wall to the south

Inner walls are defined twice; a wall to the south in module 1,1 is also indicated as a wall to the north in module 2,1.

Line 1:Two space-separated integers: M and N
Line 2..:M x N integers, several per line.

SAMPLE INPUT (file castle.in)

7 4
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

OUTPUT FORMAT

The output contains several lines:

Line 1:The number of rooms the castle has.
Line 2:The size of the largest room
Line 3:The size of the largest room creatable by removing one wall
Line 4:The single wall to remove to make the largest room possible

Choose the optimal wall to remove from the set of optimal walls by choosing the module farthest to the west (and then, if still tied, farthest to the south). If still tied, choose 'N' before 'E'. Name that wall by naming the module that borders it on either the west or south, along with a direction of N or E giving the location of the wall with respect to the module.

SAMPLE OUTPUT (file castle.out)

5
9
16
4 1 E
————————————————————偷懒的分割线————————————————————
前言:有整整一个星期没有做题了呢!唉,暑假的诱惑……看来要提前回去的时间了。下个星期吧……Roll神早回去了啊!真是紧张。
思路:算是第一道正式做的图论题吧。这是完全模仿题解里的种子染色法。Flood Fill。大概就是可以用dfs的方法,从一个点开始把它能到达的点都染上相同颜色吧。如果要统计房间的大小,那就要“染色”。每个连通子图有同样的颜色,这个颜色就是表示它们之间连通的关系。统计房间的数目不是问题,只要在主函数中进行dfs。每次dfs return的时候,计数。结果就是房间数目。然后对于每一种颜色,有一个数组单门存放该颜色的房间个数,暴力一下。
至于推倒哪面墙可以得到最大的房间,按照题意,西部最优先,南部最优先,那么只要从西向东从南向北暴力,得到的解自然就是最西、最南的墙。内层循环是从南向北,那么北墙自然优先于东墙。
代码如下:
/*
ID: j.sure.1
PROG: castle
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
/****************************************/
//数字对应的方向
//0 1 2 3
//西北东南
#define LIM nx >= 0 && nx < N && ny >= 0 && ny < M
int M, N, cnt = 0, max_size = 0, new_size = 0;
int row, col;
char dir;
const int MAXN = 55;
const int d[][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};//西北东南
int room_size[MAXN*MAXN];
struct Node
{
	bool wall[4];
	int color;
	bool vis;
}a[MAXN][MAXN];

void flood_fill(int x, int y)
{
	a[x][y].vis = true;
	room_size[cnt]++;//统计
	a[x][y].color = cnt;//染色
	for(int k = 0; k < 4; k++) {//西北东南
		int nx = x + d[k][0];
		int ny = y + d[k][1];
		if(LIM && !a[nx][ny].vis && !a[x][y].wall[k]) {
			flood_fill(nx, ny);
		}
	}
}

void push_wall(int x, int y)
{
	int S;
	if(x > 0 && a[x][y].color != a[x-1][y].color) {
		S = room_size[a[x][y].color] + room_size[a[x-1][y].color];
		if(S > new_size) {
			new_size = S;
			row = x;
			col = y;
			dir = 'N';
		}
	}
	if(y < M-1 && a[x][y].color != a[x][y+1].color) {
		S = room_size[a[x][y].color] + room_size[a[x][y+1].color];
		if(S > new_size) {
			new_size = S;
			row = x;
			col = y;
			dir = 'E';
		}
	}
}

int main()
{
	freopen("castle.in", "r", stdin);
	freopen("castle.out", "w", stdout);
	int t;
	scanf("%d%d", &M, &N);
	for(int i = 0; i < N; i++)
	for(int j = 0; j < M; j++) {
		scanf("%d", &t);
		if(t&8) a[i][j].wall[3] = true;
		if(t&4) a[i][j].wall[2] = true;
		if(t&2) a[i][j].wall[1] = true;
		if(t&1) a[i][j].wall[0] = true;
		a[i][j].vis = false;
	}//存图
	for(int i = 0; i < N; i++)
	for(int j = 0; j < M; j++) {
		if(!a[i][j].vis) {
			room_size[cnt] = 0;
			flood_fill(i, j);//dfs完成染色和大小的统计
			cnt++;
		}
	}
	for(int i = 0; i < cnt; i++) {
		if(max_size < room_size[i])
			max_size = room_size[i];
	}//找出最大的房间
	new_size = max_size;
	for(int j = 0; j < M; j++) {
		for(int i = N-1; i >= 0; i--) {
			push_wall(i, j);//从西向东从南向北推墙
		}
	}
	printf("%d\n%d\n%d\n%d %d %c\n", cnt, max_size, new_size, row+1, col+1, dir);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值