USACO The Castle
来源:互联网 发布:淘宝客伴侣龙腾管家 编辑:程序博客网 时间:2024/05/22 16:06
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 NLine 2..:M x N integers, several per line.SAMPLE INPUT (file castle.in)
7 411 6 11 6 3 10 67 9 6 13 5 15 51 10 12 7 13 7 513 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 roomLine 3:The size of the largest room creatable by removing one wallLine 4:The single wall to remove to make the largest room possibleChoose 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)
59164 1 E
/* ID: qhn9992 PROG: castle LANG: C++11 */ /* ***********************************************Author :码代码的猿猿CKbossCreated Time :2014/2/7 22:21:01File Name :TheCastle.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <cstdio>#include <iostream>#include <algorithm>#include <sstream>#include <cstdlib>#include <cstring>#include <climits>#include <string>#include <ctime>#include <cmath>#include <queue>#include <stack>#include <set>#include <map>using namespace std;const int INF=0x3f3f3f3f;const double eps=1e-8;const double pi=acos(-1.0);typedef long long int LL;int n,m,cnt=1;int g[100][100],area[10000];struct gezhi{int west,north,east,south;}node[100][100];void done(int x,int i,int j){if(x&(1<<0)) node[i][j].west=1;if(x&(1<<1)) node[i][j].north=1;if(x&(1<<2)) node[i][j].east=1;if(x&(1<<3)) node[i][j].south=1;}bool ck(int x,int y){if((x>=0&&x<n)&&(y>=0&&y<m)) return true;return false;}void dfs(int x,int y,int color){g[x][y]=color;area[color]++;int X,Y;///to northX=x-1;Y=y;if(ck(X,Y)&&g[X][Y]==0&&node[x][y].north==0&&node[X][Y].south==0){dfs(X,Y,color);}///to southX=x+1;Y=y;if(ck(X,Y)&&g[X][Y]==0&&node[X][Y].north==0&&node[x][y].south==0){dfs(X,Y,color);}///to westX=x;Y=y-1;if(ck(X,Y)&&g[X][Y]==0&&node[X][Y].east==0&&node[x][y].west==0){dfs(X,Y,color);}///to eastX=x;Y=y+1;if(ck(X,Y)&&g[X][Y]==0&&node[X][Y].west==0&&node[x][y].east==0){dfs(X,Y,color);}} int main(){ freopen("castle.in","r",stdin); freopen("castle.out","w",stdout);scanf("%d%d",&m,&n);for(int i=0;i<n;i++){for(int j=0;j<m;j++){int sign;scanf("%d",&sign);done(sign,i,j);}}int num=0;for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(g[i][j]==0){num++;dfs(i,j,cnt++);}}}int MAXAREA=-INF;for(int i=1;i<cnt;i++){MAXAREA=max(MAXAREA,area[i]);}///cash wallint MAXdouble=-INF;int posx,posy;char dir;for(int j=0;j<m;j++){for(int i=n-1;i>=0;i--){///to northif(node[i][j].north){int X=i-1,Y=j;if(ck(X,Y)){int color1=g[i][j];int color2=g[X][Y];if(color1!=color2){if(area[color1]+area[color2]>MAXdouble){MAXdouble=area[color1]+area[color2];posx=i;posy=j;dir='N';}}}}///to eastif(node[i][j].east){int X=i,Y=j+1;if(ck(X,Y)){int color1=g[i][j];int color2=g[X][Y];if(color1!=color2){if(area[color1]+area[color2]>MAXdouble){MAXdouble=area[color1]+area[color2];posx=i;posy=j;dir='E';}}}}}}printf("%d\n%d\n%d\n%d %d %c\n",num,MAXAREA,MAXdouble,posx+1,posy+1,dir); return 0;}
- USACO 2.1 The Castle (castle)
- 【图论】【USACO】The Castle
- usaco-the castle
- USACO Chapter2 The Castle
- USACO:PROB The Castle
- USACO-The Castle
- USACO The Castle
- USACO The castle
- USACO 2.1 The Castle
- USACO 2.1 The Castle
- USACO--2.1The Castle
- USACO 2.1 The Castle
- USACO[2.1]The Castle
- USACO 2.1-The Castle
- USACO The Castle
- USACO 2.1 The Castle
- usaco The Castle
- USACO The Castle
- U盘安装windows xp操作系统
- Android 网络通信框架Volley简介(Google IO 2013)
- JAVA与模式 之模板方法模式
- 几种内存泄露检测工具的比较
- QC(质量控制)图,新旧七图汇总连载(什么是因果图、树状图、直方图……)
- USACO The Castle
- Java概述
- 最小路径覆盖
- PHP的CURLOPT_POSTFIELDS参数使用数组和字符串的区别
- Java之命令模式(Command Pattern)
- QC新旧七图汇总连载1——因果图
- POJ 1595 Prime Cuts (ZOJ 1312) 素数打表
- 十三个强大的Linux性能监测工具
- Cracking the coding interview--Q4.1