USACO Section 2.1 The Castle

来源:互联网 发布:上海网站关键词优化 编辑:程序博客网 时间:2024/05/16 16:27

题目原文:

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

59164 1 E

分析

输入的数据为一组矩阵,表示每个“单元”东南西北四个方向是否有“墙”,若干个没有“墙”阻挡的相互连接的“墙”组成一个“房间”。最后要求输出的包括房间的个数,最大的房间面积,以及如何拆一堵墙使两个合并的房间面积达到最大,求最大面积值以及需要被拆掉的墙的位置。

显然,本题中的每个单元都可以抽象成图论中的节点,要求的结果实际上是一个图中的最大的连通子集。使用深度优先搜索就可以把所有连通节点搜索出来。


为了更方便搜索,首先需要对每个单元的四个方向的墙的状态作一个表示。由于输入矩阵的值的每个元素的值的范围为0~15,所以恰好可以表示一个单元四堵墙有没有的所有情况,这样就可以用一个hash表来描述。根据题意1,2,4,8分别表示西、北、东、南四个方向的墙,假设一个单元有西面和北面两堵墙,则该单元的值为1+2=3。这样,就可以通过下面的方法来表示每个单元四个方向有没有墙的情况:
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]则表示该单元的东边有没有墙。

表示好每个单元的四个方向的墙的状态之后,就可以进行深搜了。

值得注意的是,在求得最大的“房间”后,还要求如何拆一堵墙使两个合并的房间面积达到最大,实际上这一步的答案是多种的,因为两个“房间”相连的“墙”可能有多个,题目中特别强调要求的是(the module farthest to the west (and then, if still tied, farthest to the south). If still tied, choose 'N' before 'E'),就是离西边越远、离南边越远,然后再优先拆北面的墙。我就是因为这个地方的错误反复提交了好多次,感觉题目这里说的有点问题,根据答案来看,似乎是离西边越近,离南边也越近......即单元越靠近左下角越好。

提交代码

/*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

0 0