USACO The Castle(floodfill(DFS))
来源:互联网 发布:linux mount 挂载目录 编辑:程序博客网 时间:2024/05/30 04:58
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.
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: brahma_2PROG: castleLANG: C++*/#include<cstdio>#include<iostream>#include<cmath>#define UPDATE(x, y, z, k) {\if((map[i][j] & (x)) && (y) && vis z != vis[i][j] \&& s[vis[i][j]] + s[vis z] > maxnew){\maxnew = s[vis[i][j]] + s[vis z];\ansx = i;\ansy = j;\dx = k;\}\}using namespace std;int map[52][52],vis[52][52],s[2502];int xs,ys,room=0,area;void dfs(int i,int j){ if(vis[i][j]) return; area++; vis[i][j]=room; if(!(map[i][j]&1)&&j>1) dfs(i,j-1); if(!(map[i][j]&2)&&i>1) dfs(i-1,j); if(!(map[i][j]&4)&&j<ys) dfs(i,j+1); if(!(map[i][j]&8)&&i<xs) dfs(i+1,j);}int main(){ freopen("castle.in", "r", stdin);freopen("castle.out", "w", stdout); int maxroom=0; int maxnew = 0, ansx, ansy, dx; cin>>ys>>xs; for(int i=1;i<=xs;i++) for(int j=1;j<=ys;j++) cin>>map[i][j]; for(int i=1;i<=xs;i++) for(int j=1;j<=ys;j++) { if(!vis[i][j]) { room++; area=0; dfs(i,j); maxroom=max(area,maxroom); s[room] = area; } } cout<<room<<endl<<maxroom<<endl; for(int j=1; j<=ys; j++)for(int i=xs; i>=1; i--){UPDATE(1, j>1, [i][j-1], 'W')UPDATE(8, i<xs, [i+1][j], 'S')UPDATE(2, i>1, [i-1][j], 'N')UPDATE(4, j<ys, [i][j+1], 'E')}cout<<maxnew<<endl<<ansx<<' '<<ansy<<' '<<char(dx)<<endl; return 0;}
- USACO The Castle(floodfill(DFS))
- USACO / The Castle (Floodfill+DFS)
- USACO The Castle 位运算 FloodFill
- usaco 2.1.1 The Castle (使用 FloodFill 方法)
- USACO Section 2.1 The Castle (POJ 1164) floodfill
- USACO section 2.1 The Castle(dfs)
- [【USACO】The Castle(dfs+枚举)
- USACO-Section 2.1 The Castle(DFS)
- USACO 2.1 The Castle (castle)
- POJ1164 + USACO Section 2.1 The Castle - 简单的DFS...
- [2016/7/11][usaco 2.1][Flood Fill][dfs] The Castle
- 【图论】【USACO】The Castle
- usaco-the castle
- USACO Chapter2 The Castle
- USACO:PROB The Castle
- USACO-The Castle
- USACO The Castle
- USACO The castle
- hdu2097 sky数
- applet编程实例
- Sequoiadb 测试体验系列之二 – shell 控制台初探
- BT 3 validate, unique, recover
- android之开发总结,常见问题,细节问题,用户体验
- USACO The Castle(floodfill(DFS))
- EA强大功能之生成代码
- struts2集成更改默认配置文件路径
- 压缩后缀数组
- 关于光纤传播速度比铜线快的误解
- Sequoiadb 测试体验系列之三 – shell 控制台再探
- elasticsearch 修改 mapping
- 地方的好地方号发
- Pascal's Triangle 2