2.1.1---The Castle

来源:互联网 发布:巨人网络2017利润 编辑:程序博客网 时间:2024/05/22 02:22

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

以一个几乎超乎想像的运气,农民约翰在他的生日收到了一张爱尔兰博彩的奖券。这一张奖券成为了唯一中奖的奖券。农民约翰嬴得爱尔兰的乡下地方的一个传说中的城堡。吹牛在他们威斯康辛州不算什么,农民约翰想告诉他的牛所有有关城堡的事。他想知道城堡有多少房间,而且最大的房间有多大。事实上,他想去掉一面墙来制造一个更大的房间。你的任务是帮助农民约翰去了解正确房间数目和大小。城堡的平面图被分为 M(wide)*N(1 <=M,N<=50)个小正方形。每个这样的小正方形有0到4面墙。城堡在它的外部边缘总是有墙壁的,好遮挡风雨。考虑这注解了一个城堡的平面图:     1   2   3   4   5   6   7   #############################1 #   |   #   |   #   |   |   #   #####---#####---#---#####---#   2 #   #   |   #   #   #   #   #   #---#####---#####---#####---#3 #   |   |   #   #   #   #   #      #---#########---#####---#---#4 # ->#   |   |   |   |   #   #      ############################# # =墙壁 -,    | = 没有墙壁-> =移除这面墙能使得到的新房间最大例子的城堡的大小是7 x 4。一个 "房间"是平面图上有连接的"小正方形"的集合。这个平面图包含五个房间。(它们的大小是9,7,3,1, 和 8 排列没有特别的顺序)。移除被箭作记号的墙壁来合并两个房间来制造最大的可能房间(移除一面墙所能产生的)。城堡总是至少有二个房间并且总是有一面墙壁以可能被移除。PROGRAM NAME: castleINPUT FORMAT地图以一个表格来储存,每个数字描述一个小正方形,N行每行M个数来描述这个平面图。输入顺序符合那个在上面例子的编号方式。每个描述小正方形的数字说明小正方形的四面的墙的分布情况,它是下面4个数的和:1: 在西面有墙 2: 在北面有墙 4: 在东面有墙 8: 在南面有墙 内部的墙壁是会被定义两次;小正方形(1,1)南面的墙也被指出是小正方形(2,1)北面的墙。第 1 行: 二个被空格分开的整数: M 和 N 第 2 到 N+1 行:  M x N 个整数,每行M个。 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 13OUTPUT FORMAT输出包含一些行:第 1 行:  城堡的房间数目。 第 2 行:  最大的房间的大小 第 3 行:  移除一面墙能得到的最大的房间的大小  第 4 行:  移除哪面墙 选择最佳的墙来移除,(选择最靠西的,如果仍然不能确定,再选择最靠南的。编者注:墙的位置应该由它的中点来定义)(【原文】Choose the optimal wall to remove from the set of optimal walls by choosing the wall farther to the west (and then, if still tied, farthest to the south).)墙壁由它在相邻的小正方形的西部或南方来命名
/*ID:******PROG:castleLANG:C++*/#include <iostream>#include <fstream>using namespace std;int wall[50][50][4];//记录墙的情况int n,m;//城堡的规模int room[50][50];//房间分布int size[50*50+1];//房间面积int r_num;//房间号void dfs (int x, int y){if(room[x][y] == r_num)return ;room[x][y] = r_num;size[r_num]++;if(!wall[x][y][0])//搜索西邻dfs( x, y-1);if(!wall[x][y][1])//搜索北邻dfs(x -1 ,y);if(!wall[x][y][2])//搜索东邻dfs(x , y + 1);if(!wall[x][y][3])//搜索南邻dfs(x+1 ,y );}int main(){ofstream fout("castle.out");ifstream fin ("castle.in");int s_max=0;//房间最大面积int fix_max = 0;//拆除一面墙后可得的最大房间面积int fix_x,fix_y;//fix_x,fix_y为拆除墙的位置char fix_sym;//fix_sym为拆除墙的方位类型fin>>n>>m;//输入城堡规模//读取房间墙情况for(int i = 0;i < m;i ++)for(int j = 0;j < n;j ++){int temp;fin >> temp;for(int k = 0; k <= 3;k ++)wall[i][j][k] = (temp >> k)&1;}//逐一搜索,规划房间号、整理各房间面积for (int i = 0;i < m;i ++)for (int j = 0;j < n;j ++)if(!room[i][j]){r_num ++;size[r_num] = 0;dfs (i , j);s_max = max (s_max ,size[r_num]);}//搜索要移除的墙for (int i = 0;i <n;i ++)for (int j = m - 1;j >= 0;j --){int a = room[j][i],b = room[j][i < n-1?i+1:0],c = room[j>0?j-1:0][i];//a当前块,b东邻,c北邻if(j >0 && wall[j][i][1] && a != c && size[c] + size[a] > fix_max){fix_max = size[a] + size[c];fix_x = j+1;fix_y = i+1;fix_sym = 'N';}if(i < n - 1 && wall[j][i][2] && a != b && size[a] + size[b] > fix_max){fix_max = size[a] + size[b];fix_x = j+1;fix_y = i+1;fix_sym = 'E';}}fout<<r_num<<endl;//输出房间数fout<<s_max<<endl;//输出最大房间面积fout<<fix_max<<endl;//输出拆除墙后的最大房间面积fout<<fix_x<<" "<<fix_y<<" "<<fix_sym<<endl;//输出需拆除墙的位置及方位标志return 0;}


原创粉丝点击