开发者社区> 云雷> 正文

[usaco] castle

简介: <p>IOI'94 - Day 1 <br>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 t
+关注继续查看

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 N
Line 2..:  M x N integers, several per line. 


SAMPLE INPUT (file castle.in)
7 4
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 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 room
Line 3:  The size of the largest room creatable by removing one wall 
Line 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)
5
9
16
4 1 E

-------------------------------------------------------------------------------------------------
本题的关键点
1:如果保存点和边的信息。
 把每个小的格子看成一个点,如果两个格子同属于一个房间的话,表明两个点之间存在一个边。
 由于题目中已经给出了明确的信息:每个格子四周的墙是用二进制数来表示的。因此,只需要M*N个点的信息,
 每个点要储存:点是否被访问过,点所属的连通分量,点四周的墙信息。
2:如何查找room的数量和最大的room的面积。
 利用BFS,遍历所有的节点,把每个节点所属的component进行初始化。
3:寻找可能连接两个房间构成一个最大房间的墙
 遍历每一个墙,找出最大的。
我的代码:
--------------------------------------------------------------------------------------------------
/*
ID: yunleis2
PROG: castle
LANG: C++
*/

#include<iostream>
#include<fstream>
#include<queue>
#include<vector>
using namespace std;
typedef unsigned int uint;
class vertex
{
public:
 uint wall;
 int component;
 int visited;
 vertex()
 {
  component=-1;
  visited=0;
 }
};
static uint WALL=1;
static uint CROSS=2;
 
int main()
{

 fstream fin("castle.in",ios::in);
 int M,N;
 fin>>M>>N;
 vertex * src=new vertex[M*N];
 vector<int > rooms;
 for(int i=0;i<N;i++)
 {
  for(int j=0;j<M;j++)
  {
   fin>>src[i*M+j].wall;
  }
 }
 edge * lr1=new edge[(M-1)*N];
 edge * hl2=new edge [(N-1)*M];
 for(int i=0;i<N;i++)
 {
  for(int j=0;j<(M-1);j++)
  {
   uint wall=src[i*M+j].wall;
   if((wall&4)!=0)
    lr1[i*(M-1)+j].type=WALL;
   else
   { lr1[i*(M-1)+j].type=CROSS;
    lr1[i*(M-1)+j].left=j;
    lr1[i*(M-1)+j].right=j+1;
   }
  }
 }
 
 int g_cmpnt=1;
 int g_large=0;
 for(int i=0;i<(M*N);i++)
 {
   if(src[i].visited==0)
  {
   /************************************************************************/
   /* bfs                                                                     */
   /************************************************************************/
    queue<int> q;
   q.push(i);
   int t_large=0;
   int t_cmpnt=g_cmpnt++;
   src[i].component=t_cmpnt;
   while(!q.empty())
   {
    int tmp=q.front();
    q.pop();
    if(src[tmp].visited==0)
    {
     src[tmp].component=t_cmpnt;
     src[tmp].visited=1;
     t_large++;
     if((src[tmp].wall&1)==0)
     {
      q.push(tmp-1);
     }
     if((src[tmp].wall&2)==0)
     {
      q.push(tmp-M);
     }
     if((src[tmp].wall&4)==0)
     {
      q.push(tmp+1);
     }
     if((src[tmp].wall&8)==0)
     {
      q.push(tmp+M);
     }
    }
   }
   if(t_large>g_large)
    g_large=t_large;
   rooms.push_back(t_large);
  }
 }
 fstream fout("castle.out",ios::out);
 fout<<g_cmpnt-1<<endl;
 fout<<g_large<<endl;
 
 //search all the walls;
 int more_large=g_large;
 int r,c;
 char direc;
 for(int i=0;i<M;i++)
 {
  for(int j=N-1;j>0;j--)
  {
   if((src[j*M+i].wall&2)!=0)
   {
    if(src[j*M+i].component!=src[(j-1)*M+i].component)
    {
     int t1;
     if((t1=rooms.at(src[j*M+i].component-1)+rooms.at(src[(j-1)*M+i].component-1))>more_large)
     {
      more_large=t1;
      r=j+1;c=i+1;
      direc='N';
     }
    }
   }
  }
  if(i==(M-1))
   continue;
  for(int j=N-1;j>=0;j--)
  {
   if((src[j*M+i].wall&4)!=0)
   {
    if(src[j*M+i].component!=src[(j)*M+i+1].component)
    {
     int t1;
     if((t1=rooms.at(src[j*M+i].component-1)+rooms.at(src[(j)*M+i+1].component-1))>more_large)
     {
      more_large=t1;
      r=j+1;c=i+1;
      direc='E';
     }
    }
   }
  }
 }
 fout<<more_large<<endl;
 fout<<r<<" "<<c<<" "<<direc<<endl;

 
 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
如何设置阿里云服务器安全组?阿里云安全组规则详细解说
阿里云安全组设置详细图文教程(收藏起来) 阿里云服务器安全组设置规则分享,阿里云服务器安全组如何放行端口设置教程。阿里云会要求客户设置安全组,如果不设置,阿里云会指定默认的安全组。那么,这个安全组是什么呢?顾名思义,就是为了服务器安全设置的。安全组其实就是一个虚拟的防火墙,可以让用户从端口、IP的维度来筛选对应服务器的访问者,从而形成一个云上的安全域。
19096 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
28323 0
阿里云服务器安全组设置内网互通的方法
虽然0.0.0.0/0使用非常方便,但是发现很多同学使用它来做内网互通,这是有安全风险的,实例有可能会在经典网络被内网IP访问到。下面介绍一下四种安全的内网互联设置方法。 购买前请先:领取阿里云幸运券,有很多优惠,可到下文中领取。
22193 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
20289 0
阿里云服务器ECS登录用户名是什么?系统不同默认账号也不同
阿里云服务器Windows系统默认用户名administrator,Linux镜像服务器用户名root
15781 0
腾讯云服务器 设置ngxin + fastdfs +tomcat 开机自启动
在tomcat中新建一个可以启动的 .sh 脚本文件 /usr/local/tomcat7/bin/ export JAVA_HOME=/usr/local/java/jdk7 export PATH=$JAVA_HOME/bin/:$PATH export CLASSPATH=.
14878 0
使用OpenApi弹性释放和设置云服务器ECS释放
云服务器ECS的一个重要特性就是按需创建资源。您可以在业务高峰期按需弹性的自定义规则进行资源创建,在完成业务计算的时候释放资源。本篇将提供几个Tips帮助您更加容易和自动化的完成云服务器的释放和弹性设置。
20888 0
+关注
云雷
日志计算驱动业务增长 让天下没有难分析的日志
104
文章
2
问答
文章排行榜
最热
最新
相关电子书
更多
JS零基础入门教程(上册)
立即下载
性能优化方法论
立即下载
手把手学习日志服务SLS,云启实验室实战指南
立即下载