[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 ...
随机推荐
- eclipse中,添加JavaSE帮助文档和源码
- [題解]luogu_P1120小木棍(搜索)
好久以前抄的題解,現在重新抄題解做一下 1.對所有木棍從大到小排序,後用小的比較靈活 2.限制加入的木棍單調遞減,因為先/后用長/短木棍等價,反正就是那兩根 3.預處理出重複木棍的位置,防止重複搜索相 ...
- Zynq7000开发系列-7(在Zybo上运行Linaro桌面系统)
目标板:Zybo(7Z010) 主机操作系统:Ubuntu 14.04.5 LTS 64bit 交叉编译链: arm-xilinx-linux-gnueabi- [gcc version ...
- [已读]了不起的Node.js
2015/1/22 昨天下班前看完了这本,也不算看完,redis与mysql部分没有去翻,觉得暂时用不上. 觉得第一部分的内容还不错. 第二部分主要讲fs,tcp和http这三个模块. 第三个部分是例 ...
- js 中对字符串的操作
1.split() split() 方法用于把一个字符串分割成字符串数组. 用法:stringObject.split(separator,howmany) separator:必选,类型为字符串或者 ...
- 如何写一个跨浏览器的事件处理程序 js
如何 写一个合格的事件处理程序,看如下代码: EventUtil可以直接拿去用 不谢 <!DOCTYPE html> <html> <head> <title ...
- SpringBoot 2.x (11):定时任务与异步任务
定时任务:有时候我们需要做定时的一些操作,比如统计信息,定时发送邮件等 在SpringBoot中如何进行整合和使用呢? 有哪些方式可以实现定时任务呢? Java自带的java.util.timer: ...
- 除虫记——有关WindowsAPI文件查找函数的一次压力测试
作者:朱金灿 来源:http://blog.csdn.net/clever101 这里说的除虫是指排除bug的意思.今天排除了一个有意思的bug,其中的场景大致是这样的:现在你要统计一个文件夹下非隐藏 ...
- 异步 ThreadPool
线程池是单例,一个进程里只有一个线程池 private void btnThreadPool_Click(object sender, EventArgs e) { Stopwatch watch = ...
- 洛谷 P2916 [USACO08NOV]为母牛欢呼Cheering up the Cows
题目描述 Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths tha ...