【搜索】洛谷 P1457 城堡 The Castle

xiaoxiao2021-02-28  121

题目描述

我们憨厚的USACO主人公农夫约翰(Farmer John)以无法想象的运气,在他生日那天收到了一份特别的礼物:一张“幸运爱尔兰”(一种彩票)。结果这张彩票让他获得了这次比赛唯一的奖品——坐落于爱尔兰郊外的一座梦幻般的城堡!

喜欢吹嘘的农夫约翰立刻回到有着吹嘘传统的威斯康辛老家开始吹嘘了, 农夫约翰想要告诉他的奶牛们关于他城堡的一切。他需要做一些吹嘘前的准备工作:比如说知道城堡有多少个房间,每个房间有多大。另外,农夫约翰想要把一面单独的墙(指两个单位间的墙)拆掉以形成一个更大的房间。 你的工作就是帮农夫约翰做以上的准备,算出房间数与房间的大小。

城堡的平面图被划分成M*N(1 <=M,N<=50)个正方形的单位,一个这样的单位可以有0到4面墙环绕。城堡周围一定有外墙环绕以遮风挡雨。(就是说平面图的四周一定是墙。)

请仔细研究下面这个有注解的城堡平面图: 友情提示,这个城堡的平面图是7×4个单位的。一个“房间”的是平面图中一个由“#”、“-”、“|”围成的格子(就是图里面的那一个个的格子)。比如说这个样例就有5个房间。(大小分别为9、7、3、1、8个单位(排名不分先后))

移去箭头所指的那面墙,可以使2个房间合为一个新房间,且比移去其他墙所形成的房间都大。(原文为: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. )

城堡保证至少有2个房间,而且一定有一面墙可以被移走。

输入输出格式

输入格式:

第一行有两个整数:M和N 城堡的平面图用一个由数字组成的矩阵表示,一个数字表示一个单位,矩阵有N行M列。输入与样例的图一致。

每一个单位的数字告诉我们这个单位的东西南北是否有墙存在。每个数字是由以下四个整数的某个或某几个或一个都没有加起来的。

1: 在西面有墙

2: 在北面有墙

4: 在东面有墙

8: 在南面有墙

城堡内部的墙会被规定两次。比如说(1,1)南面的墙,亦会被标记为(2,1)北面的墙。

输出格式:

输出包含如下4行:

第 1 行: 城堡的房间数目。

第 2 行: 最大的房间的大小

第 3 行: 移除一面墙能得到的最大的房间的大小

第 4 行: 移除哪面墙可以得到面积最大的新房间。

选择最佳的墙来推倒。有多解时选最靠西的,仍然有多解时选最靠南的。同一格子北边的墙比东边的墙更优先。

用该墙的南邻单位的北墙或西邻单位的东墙来表示这面墙,方法是输出邻近单位的行数、列数和墙的方位(”N”(北)或者”E”(东))。

输入输出样例

输入样例#1:

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

输出样例#1:

5 9 16 4 1 E

说明

USACO 2.1

翻译来自NOCOW

思路

按照题意,不加任何思考,直接强写,240多行暴力AC

代码

#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int MAXN=50+5; int n,m,ans_1,c[MAXN][MAXN],ans_2,ans_3,ans_x,ans_y; char ans_direction; bool b[MAXN][MAXN]; struct node{ bool north; bool south; bool west; bool east; node(){ north=true; south=true; west=true; east=true; } }; node a[MAXN][MAXN]; inline void read(int &x) { x=0; char c=getchar(); while(c<'0'||c>'9')c=getchar(); while(c>='0'&&c<='9') { x=x*10+c-'0'; c=getchar(); } } inline void search(int i,int j,int sum) { b[i][j]=false; sum++; c[i][j]=sum; if(a[i][j].east&&b[i][j+1]) { search(i,j+1,c[i][j]); c[i][j]=c[i][j+1]; } if(a[i][j].south&&b[i+1][j]) { search(i+1,j,c[i][j]); c[i][j]=c[i+1][j]; } if(a[i][j].north&&b[i-1][j]) { search(i-1,j,c[i][j]); c[i][j]=c[i-1][j]; } if(a[i][j].west&&b[i][j-1]) { search(i,j-1,c[i][j]); c[i][j]=c[i][j-1]; } return ; } inline int work(int i,int j,int sum) { b[i][j]=false; sum++; if(a[i][j].east&&b[i][j+1])sum=work(i,j+1,sum); if(a[i][j].south&&b[i+1][j])sum=work(i+1,j,sum); if(a[i][j].north&&b[i-1][j])sum=work(i-1,j,sum); if(a[i][j].west&&b[i][j-1])sum=work(i,j-1,sum); return sum; } int main() { read(m);read(n); for(register int i=1;i<=n;++i) { for(int j=1;j<=m;++j) { int x; read(x); if(x==1)a[i][j].west=false; else if(x==2)a[i][j].north=false; else if(x==3) { a[i][j].west=false; a[i][j].north=false; } else if(x==4)a[i][j].east=false; else if(x==5) { a[i][j].east=false; a[i][j].west=false; } else if(x==6) { a[i][j].east=false; a[i][j].north=false; } else if(x==7) { a[i][j].east=false; a[i][j].north=false; a[i][j].west=false; } else if(x==8)a[i][j].south=false; else if(x==9) { a[i][j].south=false; a[i][j].west=false; } else if(x==10) { a[i][j].south=false; a[i][j].north=false; } else if(x==11) { a[i][j].south=false; a[i][j].west=false; a[i][j].north=false; } else if(x==12) { a[i][j].east=false; a[i][j].south=false; } else if(x==13) { a[i][j].east=false; a[i][j].west=false; a[i][j].south=false; } else if(x==14) { a[i][j].east=false; a[i][j].north=false; a[i][j].south=false; } else if(x==15) { a[i][j].east=false; a[i][j].north=false; a[i][j].south=false; a[i][j].west=false; } } } memset(b,true,sizeof(b)); for(register int i=1;i<=n;++i) { for(int j=1;j<=m;++j) { if(b[i][j]) { search(i,j,0); ans_1++; } } } cout<<ans_1<<endl; for(register int i=1;i<=n;++i) { for(int j=1;j<=m;++j)ans_2=max(ans_2,c[i][j]); } cout<<ans_2<<endl; for(register int j=1;j<=m;++j) { for(register int i=n;i>=1;--i) { memset(b,true,sizeof(b)); if(i==1&&j==m)continue; else if(i==1) { if(!a[i][j].east) { // cout<<i<<' '<<j<<' '<<"east"<<' '; a[i][j].east=true; a[i][j+1].west=true; int temp=work(i,j,0); // cout<<temp<<endl; if(temp>ans_3) { ans_3=temp; ans_x=i;ans_y=j;ans_direction='E'; } a[i][j].east=false; a[i][j+1].west=false; } } else if(j==m) { if(!a[i][j].north) { // cout<<i<<' '<<j<<' '<<"north"<<' '; a[i][j].north=true; a[i-1][j].south=true; int temp=work(i,j,0); // cout<<temp<<endl; if(temp>ans_3) { ans_3=temp; ans_x=i;ans_y=j;ans_direction='N'; } a[i][j].north=false; a[i-1][j].south=false; } } else { if(!a[i][j].north) { // cout<<i<<' '<<j<<' '<<"north"<<' '; a[i][j].north=true; a[i-1][j].south=true; int temp=work(i,j,0); // cout<<temp<<endl; if(temp>ans_3) { ans_3=temp; ans_x=i;ans_y=j;ans_direction='N'; } a[i][j].north=false; a[i-1][j].south=false; } if(!a[i][j].east) { // cout<<i<<' '<<j<<' '<<"east"<<' '; a[i][j].east=true; a[i][j+1].west=true; int temp=work(i,j,0); // cout<<temp<<endl; if(temp>ans_3) { ans_3=temp; ans_x=i;ans_y=j;ans_direction='E'; } a[i][j].east=false; a[i][j+1].west=false; } } } } cout<<ans_3<<endl; cout<<ans_x<<' '<<ans_y<<' '<<ans_direction; return 0; }
转载请注明原文地址: https://www.6miu.com/read-27081.html

最新回复(0)