[POJ]1164 The Castle
//markdown复制进来一堆问题 还是链接方便点
POJ 1164 The Castle
首先想到用9个方格来表示一个房间,如此一来复杂许多,MLE代码如下:
//Writer:GhostCai && His Yellow Duck
#include<iostream>
#define MAXN 50
using namespace std;
int m,n;
int a[MAXN][MAXN];
bool vis[MAXN][MAXN];
void opet(int num,int x,int y){
bool up=0,dn=0,lf=0,rt=0;//!!!
if(num>=8){
dn=1;
num-=8;
}
if(num>=4){
rt=1;
num-=4;
}
if(num>=2){
up=1;
num-=2;
}
if(num>=1){
lf=1;
}
a[x*2][y*2]=2;
if(up) a[x*2-1][y*2]=1,a[x*2-1][y*2+1]=1,a[x*2-1][y*2-1]=1;
if(dn) a[x*2+1][y*2]=1,a[x*2+1][y*2+1]=1,a[x*2+1][y*2-1]=1;
if(lf) a[x*2][y*2-1]=1,a[x*2+1][y*2-1]=1,a[x*2-1][y*2-1]=1;
if(rt) a[x*2][y*2+1]=1,a[x*2+1][y*2+1]=1,a[x*2-1][y*2+1]=1;
}
void dfs(int x,int y){
if(vis[x][y]) return;
if(x<1||x>m*2+1||y<1||y>n*2+1) return;
if(a[x][y]==1) return;
if(a[x][y]!=0) vis[x][y]=1;//!!
if(a[x][y]==2) a[x][y]=-1;
dfs(x+1,y);
dfs(x-1,y);
dfs(x,y+1);
dfs(x,y-1);
}
int calc(){
int cnt=0;
for(register int i=1;i<=m*2+1;i++){
for(register int j=1;j<=n*2+1;j++){
if(a[i][j]==-1){
cnt++;
a[i][j]=2;
}
}
}
return cnt;
}
int main(){
cin>>m>>n;
int i,j;
int r;
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
cin>>r;
opet(r,i,j);
}
}
int ans=0,mx=0,cnt=0;
for(i=1;i<=m*2+1;i++) a[i][1]=1,a[i][n*2+1]=1;
for(i=1;i<=n*2+1;i++) a[1][i]=1,a[m*2+1][i]=1;
// for(i=1;i<=m*2+1;i++){
// for(j=1;j<=n*2+1;j++){
// cout<<a[i][j]<<" ";
// }
// cout<<endl;
// }
for(i=1;i<=m*2+1;i++){
for(j=1;j<=n*2+1;j++){
if(!vis[i][j]&&a[i][j]==2){
dfs(i,j);
cnt++;
ans=calc();
mx=max(ans,mx);
}
}
}
cout<<cnt<<endl<<mx<<endl;
return 0;
}
百般无奈下,重新思路。
想到其实没必要用9个方格,一个就挺好。
只不过在搜索的时候,利用数字判断一下这个方向走不走。
注意的一点是局部变量要赋初值,dfs里四个布尔位置变量一开始没有赋初值,导致奇怪的结果。
还是很像swim,嗯。
//Writer:GhostCai && His Yellow Duck
#include<iostream>
#define MAXN 200
using namespace std;
int m,n;
int a[MAXN][MAXN];
bool vis[MAXN][MAXN];
void dfs(int x,int y){
if(vis[x][y]) return;
if(x<1||x>m||y<1||y>n) return;
int d=a[x][y];
vis[x][y]=1;//!!
a[x][y]+=666;
bool lf=0,rt=0,up=0,dn=0;//!!!
if(d>=8){
d-=8;
dn=1;
}
if(d>=4){
d-=4;
rt=1;
}
if(d>=2){
up=1;
d-=2;
}
if(d>=1){
lf=1;
}
if(!lf) dfs(x,y-1);
if(!rt) dfs(x,y+1);
if(!dn) dfs(x+1,y);
if(!up) dfs(x-1,y);
}
int calc(){
int cnt=0;
for(register int i=1;i<=m;i++){
for(register int j=1;j<=n;j++){
if(a[i][j]>16){
cnt++;
a[i][j]-=666;
}
}
}
return cnt;
}
int main(){
int ans=0,mx=0,cnt=0;
cin>>m>>n;
int i,j;
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
cin>>a[i][j];
}
}
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
if(!vis[i][j]){
dfs(i,j);
cnt++;
ans=calc();
mx=max(mx,ans);
}
}
}
cout<<cnt<<endl<<mx<<endl;
}
[POJ]1164 The Castle的更多相关文章
- OpenJudge 2815 城堡问题 / Poj 1164 The Castle
1.链接地址: http://bailian.openjudge.cn/practice/2815/ http://poj.org/problem?id=1164 2.题目: 总时间限制: 1000m ...
- POJ 1164:The Castle
The Castle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6677 Accepted: 3767 Descri ...
- POJ 1164 城堡问题【DFS/位运算/种子填充法/染色法】
1 2 3 4 5 6 7 ############################# 1 # | # | # | | # #####---#####---#---#####---# 2 # # | ...
- POJ --- 1164 放苹果
放苹果 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24947 Accepted: 15887 Description ...
- poj 1164 深度优先搜索模板题
#include<iostream> //用栈进行的解决: #include<cstdio> #include<algorithm> #include<cst ...
- 计算几何--求凸包模板--Graham算法--poj 1113
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28157 Accepted: 9401 Description ...
- poj 1113:Wall(计算几何,求凸包周长)
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28462 Accepted: 9498 Description ...
- 【POJ】1113 Wall(凸包)
http://poj.org/problem?id=1113 答案是凸包周长+半径为l的圆的周长... 证明?这是个坑.. #include <cstdio> #include <c ...
- poj 题目分类(1)
poj 题目分类 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K--0.50K:中短代码:0.51K--1.00K:中等代码量:1.01K--2.00K:长代码:2.01 ...
随机推荐
- button 获取 cell
- (void)cellBtnClicked:(id)sender event:(id)event { NSSet *touches =[event allTouches]; ...
- 即时通讯新手入门:一文读懂什么是Nginx?它能否实现IM的负载均衡?
本文引用了“蔷薇Nina”的“Nginx 相关介绍(Nginx是什么?能干嘛?)”一文部分内容,感谢作者的无私分享. 1.引言 Nginx(及其衍生产品)是目前被大量使用的服务端反向代理和负载均衡 ...
- LCT 学习笔记
LCT学习笔记 前言 自己定的学习计划看起来完不成了(两天没学东西,全在补题),决定赶快学点东西 于是就学LCT了 简介 Link/Cut Tree是一种数据结构,我们用它解决动态树问题 但是LCT不 ...
- Ubuntu下修改权限时出现"unable to execute /bin/chmod: Argument list too long"
Ubuntu14.04下修改权限时出现"unable to execute /bin/chmod: Argument list too long" ,大概意思是卧槽,你这参数也 ...
- AS学习系列[1]——初识Android Studio
写在前面的话:由于于方老师的高墙所限,网络成了学习Android第一道“拦路虎”.所以,个人以为,在学习Android之前需要了解下FQ技术(仅仅是为了技术学习). 1.AS AS(Android s ...
- 解决更新到os x10.11后openssl头文件无法找到的问题
os x从10.10更新到10.11后,原有代码编译报错,#include <openssl/ssl.h>等头文件无法找到: "openssl/ssl.h: No such fi ...
- MySQL流程控制和存储过程介绍
/*定义变量方式1:set @变量名=值;方式2:select 值 into @变量名;方式3:declare 变量名 类型(字符串类型加范围) default 值; in参数 入参的值会仅在存储过程 ...
- strophe.js 插件 XMPP openfire
参考资料:http://strophe.im/strophejs/ https://github.com/strophe/strophejs-plugins http://amazeui.org/ 最 ...
- Tunneling cannot be enabled without the local_ip bound to an interface on the host. Please configure local_ip 192.168.30.71 on the host interface to be used for tunneling and restart the agen
按照官方文档配置linux bridge 会出现一下问题 Tunneling cannot be enabled without the local_ip bound to an interface ...
- leetcode_1033. Moving Stones Until Consecutive
https://leetcode.com/problems/moving-stones-until-consecutive/ 题意:给定3个点,每次从两个端点(位置最小或位置最大)中挑选一个点进行移动 ...