USACO Section 2.1 The Castle
来源:互联网 发布:上海网站关键词优化 编辑:程序博客网 时间:2024/05/16 16:27
题目原文:
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
分析
enum Direction{WEST=0,NORTH,EAST,SOUTH};int hasWall[16][4]={{0,0,0,0}//0,{1,0,0,0}//1,{0,1,0,0}//2,{1,1,0,0}//3,{0,0,1,0}//4,{1,0,1,0}//5,{0,1,1,0}//6,{1,1,1,0}//7,{0,0,0,1}//8,{1,0,0,1}//9,{0,1,0,1}//10,{1,1,0,1}//11,{0,0,1,1}//12,{1,0,1,1}//13,{0,1,1,1}//14,{1,1,1,1}//15};举个例子,某个单元输入的值为value,那么hasWall[value][EAST]则表示该单元的东边有没有墙。
提交代码
/*ID: PROG: castleLANG: C++*/#include <iostream>#include <fstream>#include <vector>#include <algorithm>#include <string>#include <math.h>#include <map>using namespace std;enum Direction{WEST=0,NORTH,EAST,SOUTH};int hasWall[16][4]={{0,0,0,0}//0,{1,0,0,0}//1,{0,1,0,0}//2,{1,1,0,0}//3,{0,0,1,0}//4,{1,0,1,0}//5,{0,1,1,0}//6,{1,1,1,0}//7,{0,0,0,1}//8,{1,0,0,1}//9,{0,1,0,1}//10,{1,1,0,1}//11,{0,0,1,1}//12,{1,0,1,1}//13,{0,1,1,1}//14,{1,1,1,1}//15};bool is_ad(int c1_x,int c1_y, int c2_x,int c2_y, const vector<vector<int> > &castle){if(c1_x==c2_x && abs(c1_y - c2_y)==1)//up and down{if(c1_y < c2_y)return hasWall[castle[c1_y][c1_x]][SOUTH] == 0 && hasWall[castle[c2_y][c2_x]][NORTH] == 0;elsereturn hasWall[castle[c1_y][c1_x]][NORTH] == 0 && hasWall[castle[c2_y][c2_x]][SOUTH] == 0;}else if((c1_y==c2_y&&abs(c1_x-c2_x)==1))//left and right{if(c1_x < c2_x)return hasWall[castle[c1_y][c1_x]][EAST] == 0 && hasWall[castle[c2_y][c2_x]][WEST] == 0;elsereturn hasWall[castle[c1_y][c1_x]][WEST] == 0 && hasWall[castle[c2_y][c2_x]][EAST] == 0;}else{return false;}}void search(int w, int h,const vector<vector<int> > &castle,vector<vector<int> > &room,int &room_id,map<int,int> &room_size,vector<vector<bool> > &isCheck){if(room[h][w]==room_id)return;isCheck[h][w] = true;if(room_size.find(room_id)!=room_size.end())room_size[room_id]++;elseroom_size[room_id]=1;room[h][w]=room_id;if(!hasWall[castle[h][w]][WEST])search(w-1,h,castle,room,room_id,room_size,isCheck);if(!hasWall[castle[h][w]][NORTH])search(w,h-1,castle,room,room_id,room_size,isCheck);if(!hasWall[castle[h][w]][EAST])search(w+1,h,castle,room,room_id,room_size,isCheck);if(!hasWall[castle[h][w]][SOUTH])search(w,h+1,castle,room,room_id,room_size,isCheck);}bool compare(const pair<int,int> &a,const pair<int,int> &b){return a.second <= b.second;}int main(){ifstream fin("castle.in");ofstream fout("castle.out");int M,N;fin >> M >> N;vector<vector<int> > castle(N);for (int i=0;i!=N;i++){for (int j=0;j!=M;j++){int t;fin >> t;castle[i].push_back(t);}}vector<vector<int> > room(N);for (int i=0;i!=N;i++){room[i].resize(M,-1);}vector<vector<bool> > isCheck(N);for (int i=0;i!=N;i++){isCheck[i].resize(M,false);}map<int,int> room_size;int roomid=0;int max_size = 0;for (int j=0;j!=N;j++){for (int i=0;i!=M;i++){if(!isCheck[j][i]){search(i,j,castle,room,roomid,room_size,isCheck);roomid++;}}}fout << room_size.size() << endl;for (map<int,int>::iterator itr = room_size.begin();itr!=room_size.end();itr++){if(max_size < itr->second)max_size = itr->second;}fout << max_size << endl;string dir;int x,y;for (int i=0;i!=M;i++){for (int j=N-1;j>=0;j--){if(j>0 && hasWall[castle[j][i]][NORTH] && room[j][i] != room[j-1][i] && room_size[room[j][i]]+room_size[room[j-1][i]] > max_size){x = i,y=j;dir="N";max_size = room_size[room[j][i]]+room_size[room[j-1][i]];}if(i<M-1 && hasWall[castle[j][i]][EAST] && room[j][i] != room[j][i+1] && room_size[room[j][i]]+room_size[room[j][i+1]] > max_size){x = i,y=j;dir="E";max_size = room_size[room[j][i]]+room_size[room[j][i+1]];}}}fout <<max_size << endl << y+1 <<" " << x+1 <<" " << dir <<endl; return 0;}
提交结果
TASK: castleLANG: C++Compiling...Compile: OKExecuting... Test 1: TEST OK [0.005 secs, 3508 KB] Test 2: TEST OK [0.008 secs, 3508 KB] Test 3: TEST OK [0.008 secs, 3508 KB] Test 4: TEST OK [0.003 secs, 3508 KB] Test 5: TEST OK [0.005 secs, 3508 KB] Test 6: TEST OK [0.008 secs, 3508 KB] Test 7: TEST OK [0.014 secs, 3508 KB] Test 8: TEST OK [0.008 secs, 3508 KB]All tests OK.
官方参考答案
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#define MAXDIM 50#define MAXN 100#define MAXCOLOR 100#define MAXROOM (MAXDIM*MAXDIM)enum { Wwest = 1, Wnorth = 2, Weast = 4, Wsouth = 8};typedef struct SquareSquare;struct Square { int wall; int numbered; int room;}; int wid, ht;Square castle[MAXDIM][MAXDIM];int roomsize[MAXROOM];voidnumber(int room, int x, int y){ int w; if(castle[x][y].numbered) {assert(castle[x][y].room == room);return; } castle[x][y].numbered = 1; castle[x][y].room = room; roomsize[room]++; w = castle[x][y].wall; if(x > 0 && !(w & Wwest))number(room, x-1, y); if(x+1 < wid && !(w & Weast))number(room, x+1, y); if(y > 0 && !(w & Wnorth))number(room, x, y-1); if(y+1 < ht && !(w & Wsouth))number(room, x, y+1);}voidmain(void){ FILE *fin, *fout; int x, y, w, nroom, bigroom; int i, n, m, mx, my; char mc; fin = fopen("castle.in", "r"); fout = fopen("castle.out", "w"); assert(fin != NULL && fout != NULL); fscanf(fin, "%d %d", &wid, &ht); /* read in wall info */ for(y=0; y<ht; y++) {for(x=0; x<wid; x++) { fscanf(fin, "%d", &w); castle[x][y].wall = w;} } /* number rooms */ nroom = 0; for(y=0; y<ht; y++) for(x=0; x<wid; x++)if(!castle[x][y].numbered) number(nroom++, x, y); /* find biggest room */ bigroom = roomsize[0]; for(i=1; i<nroom; i++)if(bigroom < roomsize[i]) bigroom = roomsize[i]; /* look at best that can come of removing an east or north wall */ m = 0; for(x=0; x<wid; x++) for(y=ht-1; y>=0; y--) {if(y > 0 && castle[x][y].room != castle[x][y-1].room) { n = roomsize[castle[x][y].room] + roomsize[castle[x][y-1].room]; if(n > m) {m = n;mx = x;my = y;mc = 'N'; }}if(x+1 < wid && castle[x][y].room != castle[x+1][y].room) { n = roomsize[castle[x][y].room] + roomsize[castle[x+1][y].room]; if(n > m) {m = n;mx = x;my = y;mc = 'E'; }} } fprintf(fout, "%d\n", nroom); fprintf(fout, "%d\n", bigroom); fprintf(fout, "%d\n", m); fprintf(fout, "%d %d %c\n", my+1, mx+1, mc); exit(0);}
THE END
- Usaco Section 2.1 The Castle
- USACO Section 2.1 The Castle
- USACO Section 2.1 The Castle
- USACO section 2.1 The Castle(dfs)
- [题目] Section 2.1 The Castle(USACO)
- USACO-Section 2.1 The Castle(DFS)
- USACO Section 2.1 The Castle (POJ 1164) floodfill
- POJ1164 + USACO Section 2.1 The Castle - 简单的DFS...
- Section 2.1 The Castle
- USACO 2.1 The Castle (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 2.1 The Castle
- USACO 2.1.1The Castle
- poj1200(字符串hash)
- Linux上创建共享库so
- Opencv学习笔记之Mat类读入并显示图片
- Android CheckBox复选框 在ListView中, 有时被点击时不会立刻选上
- Zend Framework 2 入门-视图(View)
- USACO Section 2.1 The Castle
- 用iptables做端口映射
- Coin test
- 【转】TFT液晶简介
- 16进制转ascii接口
- sql语句
- minicom无法输入问题
- Android 内存溢出解决方案(OOM)整理总结
- 在UIAlertView上面添加输入框