The Castle
来源:互联网 发布:手机动漫配音软件 编辑:程序博客网 时间:2024/05/20 03: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
读题目读了半个小时好开心 一下就注意到1 2 4 8 可以和二进制挂钩 可是条件也太多了吧特别是 拆墙一定要靠西靠南妈的不都一样!然后二进制位运算符优先权又没考虑 search代码懒得简化了。。
/*ID: des_jas1PROG: castleLANG: C++*/#include <iostream>#include <fstream>#include <string>#include <string.h>#include <cmath>#include <algorithm>//#define fin cin//#define fout coutusing namespace std;const int d[4]={1,2,4,8},rr[4]={0,-1,0,1},col[4]={-1,0,1,0},NUM=55;int N,M,r1=NUM,r2=NUM,maxx,mmax,cn=0,a[55][55],belongmod[55][55],modsize[2550]={0};bool visited[NUM][NUM];char c;void dfs(int n,int m){if(a[n][m]<0)return;if(visited[n][m])return;visited[n][m]=true;belongmod[n][m]=cn;modsize[cn]++;int i;for(i=0;i<4;i++)if((a[n][m]&d[i]) == 0)dfs(n+rr[i],m+col[i]);return;}void search(){int n,m,p,q;for(n=1;n<=N;n++)for(m=M;m>=1;m--){p=modsize[belongmod[n][m]];if((a[n][m]&4) == 4){if(a[n][m+1]>=0){ q=modsize[belongmod[n][m+1]]; if(belongmod[n][m]!=belongmod[n][m+1]){if(mmax<p+q || (mmax==p+q && m<=r2)){ r1=n; r2=m; mmax=p+q; c='E';}}}}if((a[n][m]&2) == 2){if(a[n-1][m]>=0){ q=modsize[belongmod[n-1][m]]; if(belongmod[n][m] != belongmod[n-1][m]){ if(mmax<p+q || (mmax==p+q && m<=r2)){ r1=n; r2=m; mmax=p+q; c='N';}}}}}}void find(){int i,j;for(i=1;i<=N;i++)for(j=1;j<=M;j++){if(!visited[i][j]){cn++;//from mod 1dfs(i,j);maxx=maxx>modsize[cn]?maxx:modsize[cn];}}}int main() {ofstream fout("castle.out"); ifstream fin("castle.in");fin>>M>>N;memset(visited,0,sizeof(visited));memset(a,-1,sizeof(a));int i,j;for(i=1;i<=N;i++)for(j=1;j<=M;j++)fin>>a[i][j];find();search();fout<<cn<<endl<<maxx<<endl<<mmax<<endl<<r1<<" "<<r2<<" "<<c<<endl;fout.close();fin.close(); return 0;}
- The Castle
- The Castle
- the castle
- The Castle
- The Castle
- The Castle
- The Castle
- The Castle
- USACO 2.1 The Castle (castle)
- USACO2.1.1 The Castle (castle)
- USACO2.1.1 The Castle (castle)
- 【图论】【USACO】The Castle
- 2.1.2 The Castle
- Section 2.1 The Castle
- 2.1The Castle
- usaco-the castle
- 【染色】The Castle 城堡
- 深搜:The Castle
- linux下java程序使用第三方jar包
- 关于学习数学
- Mapreduce 读取Hbase,写入hbase IO 不均衡问题
- Android开发之对上下两个图层的操作
- 解析codepage和charset的含义及其应用
- The Castle
- memcached全面剖析–2.理解memcached的内存存储
- 多少块地(求可以分成多少个独立的区域)
- 在三层架构的B层应用TransactionScope事务
- Linux下判断文件或文件夹是否存在的方法
- 各自天涯 各自珍重
- memcached全面剖析–3.memcached的删除机制和发展方向
- 关于QQ2009聊天消息获取原理说明 .
- ADO.Net 精简的三层架构(初学者)