http://acm.hdu.edu.cn/showproblem.php?pid=3912


这个题我用递归深搜模拟,直接爆栈了。哭啊!为什么!

这个题最主要是能走重复格子,但是方向不一样。

我用的剪枝条件:
        1、判断有没有走出迷宫,走出迷宫了直接退出。
        2、由于题目要求只要判断有没有把所有格子走完,则可以用一个计数器统计走过格子的数量(重复走的不算)。走完了所有格子则直接退出搜索。
        3、在这个格子,当前走的方向以前走过,则重复了(判重)
别人的代码贴在最后面吧。。

我自己爆栈的代码
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; int r,c,enty,exitx,exity;
int yn,sum; //yn用于标记是否走出迷宫,sum记录走过几个格子
int map[510][510][5]; //地图,最后一个代表这个格子的边缘是否有墙,map[i][j][]代表ij这个格子的上下左右是否有墙
int dis[510][510][5]; //标记 struct Node
{
int x,y; //当前坐标位置
int fx; //当前前进方向,1下,2上,3左,4右
}p; void init()
{
int i,j,k,q;
for(i = 0; i < 510; i++) //初始化全为墙
{
for(j = 0; j < 510; j++)
{
map[i][j][0]=map[i][j][1]=map[i][j][2]=map[i][j][3]=map[i][j][4] = 1;
}
}
memset(dis,0,sizeof(dis)); //初始标记,均没有走过
scanf("%d%d%d%d",&r,&c,&enty,&exity);
exitx = r-1;
p.x = 0;
p.y = enty;
p.fx = 1; //p把入口位置和方向保存
q = 0;
for(i = 0; i < 2*r-1; i++) //更新边缘是否有墙
{
if(i&1)
{
for(j = 0; j < c; j++)
{
scanf("%d",&k);
map[q][j][1] = k; //格子上边
map[q+1][j][2] = k; //格子下边
}
q++;
}
else
{
for(j = 0; j < c-1; j++)
{
scanf("%d",&k);
map[q][j][4] = k; //格子的右边
map[q][j+1][3] = k; //格子的左边
}
}
}
return ;
} void right(Node now)
{
void Dfs(Node now);
Node next;
switch(now.fx)
{
case 1:
{
if(map[now.x][now.y][3]==0) //向下右转则向左,如果没有墙则能走
{
next.x = now.x;
next.y = now.y-1;
next.fx = 3; //更改当先方向
if(dis[next.x][next.y][0]==0) //查看下一个格子以前走过没
{
sum++; //没走过,增加一个
}
Dfs(next);
}
break;
}
case 2:
{
if(map[now.x][now.y][4]==0)
{
next.x = now.x;
next.y = now.y+1;
next.fx = 4;
if(dis[next.x][next.y][0]==0)
{
sum++;
}
Dfs(next);
}
break;
}
case 3:
{
if(map[now.x][now.y][2]==0)
{
next.x = now.x-1;
next.y = now.y;
next.fx = 2;
if(dis[next.x][next.y][0]==0)
{
sum++;
}
Dfs(next);
}
break;
}
case 4:
{
if(map[now.x][now.y][1]==0)
{
next.x = now.x+1;
next.y = now.y;
next.fx = 1;
if(dis[next.x][next.y][0]==0)
{
sum++;
}
Dfs(next);
}
break;
}
}
return ;
} void stright(Node now)
{
void Dfs(Node now);
Node next;
if(map[now.x][now.y][now.fx]==0)
{
switch(now.fx)
{
case 1:
{
next.x = now.x+1;
next.y = now.y;
next.fx = now.fx;
if(dis[next.x][next.y][0]==0)
{
sum++;
}
Dfs(next);
break;
}
case 2:
{
next.x = now.x-1;
next.y = now.y;
next.fx = now.fx;
if(dis[next.x][next.y][0]==0)
{
sum++;
}
Dfs(next);
break;
}
case 3:
{
next.x = now.x;
next.y = now.y-1;
next.fx = now.fx;
if(dis[next.x][next.y][0]==0)
{
sum++;
}
Dfs(next);
break;
}
case 4:
{
next.x = now.x;
next.y = now.y+1;
next.fx = now.fx;
if(dis[next.x][next.y][0]==0)
{
sum++;
}
Dfs(next);
break;
}
}
}
return ;
} void left(Node now)
{
void Dfs(Node now);
Node next;
switch(now.fx)
{
case 1:
{
if(map[now.x][now.y][4]==0)
{
next.x = now.x;
next.y = now.y+1;
next.fx = 4;
if(dis[next.x][next.y][0]==0)
{
sum++;
}
Dfs(next);
}
break;
}
case 2:
{
if(map[now.x][now.y][3]==0)
{
next.x = now.x;
next.y = now.y-1;
next.fx = 3;
if(dis[next.x][next.y][0]==0)
{
sum++;
}
Dfs(next);
}
break;
}
case 3:
{
if(map[now.x][now.y][1]==0)
{
next.x = now.x+1;
next.y = now.y;
next.fx = 1;
if(dis[next.x][next.y][0]==0)
{
sum++;
}
Dfs(next);
}
break;
}
case 4:
{
if(map[now.x][now.y][2]==0)
{
next.x = now.x;
next.y = now.y-1;
next.fx = 2;
if(dis[next.x][next.y][0]==0)
{
sum++;
}
Dfs(next);
}
break;
}
}
return ;
} void Dfs(Node now)
{
Node next;
if(yn) //如果已经出去了,则不用搜索了
{
return ;
}
if(sum == r*c) //如果所有格子已经经过,则直接返回
{
return ;
}
if(now.x == exitx && now.y == exity && now.fx == 1) //如果到出口且出去则标记并返回
{
yn = 1; //标记已经出去了
return ;
}
if(dis[now.x][now.y][now.fx]==1) //如果这个格子的这个方向以前走过,则直接返回(判重)
{
return ;
}
if(dis[now.x])
dis[now.x][now.y][0]++; //标记这个格子经过的次数加一
dis[now.x][now.y][now.fx] = 1; //标记这个格子这个方向走过
right(now);
stright(now);
left(now);
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
yn = 0;
sum = 1;
Dfs(p);
if(sum==r*c)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
} return 0;
}

别人的代码:(来源:
http://blog.csdn.net/wsniyufang/article/details/6665488

/*
开始用dfs递归,爆栈了
后来模拟又因为 出迷宫的条件一直wa,细节很重要
*/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctime>
using namespace std;
#define N 50009
int R,C,ER,EC,num;
int map1[505][505];
int map2[505][505];
int vis[505][505];
struct Point
{
int x,y,f;
Point(int _x,int _y,int _f){x=_x;y=_y;f=_f;}
};
void init()
{
scanf("%d%d%d%d",&R,&C,&ER,&EC);
memset(vis,0,sizeof(vis));
num=0;
for(int i=0;i<2*R-1;i++)
{
if(i&1)
{
for(int j=0;j<C;j++)
scanf("%d",&map1[i/2][j]);
}
else
{
for(int j=0;j<C-1;j++)
scanf("%d",&map2[i/2][j]);
}
}
} void dfs(Point s,Point ss)
{
int ff=s.f;
for(;;)
{
if(!vis[s.x][s.y])num++;
vis[s.x][s.y]=1;
if(s.x==ss.x&&s.y==ss.y)//注意退出的条件,这里比较复杂
{
if((s.f==3&&ff==0)||(s.f==1&&ff==2))
break;
if(s.f==ff&&ff==0&&(s.y==0||map2[s.x][s.y-1]==1))
break;
if(s.f==ff&&ff==2&&(s.y==C-1||map2[s.x][s.y]==1))
break;
}
if(s.f==0)
{
if(s.y>0&&map2[s.x][s.y-1]==0)
{
s.y--;
s.f=1;
}
else if(s.x+1<R&&map1[s.x][s.y]==0)
{
s.x++;
s.f=0;
}
else if(s.y+1<C&&map2[s.x][s.y]==0)
{
s.y++;
s.f=3;
}
else if(s.x>0&&map1[s.x-1][s.y]==0)
{
s.x--;
s.f=2;
} }
else if(s.f==1)
{
if(s.x>0&&map1[s.x-1][s.y]==0)
{
s.x--;
s.f=2;
}
else if(s.y>0&&map2[s.x][s.y-1]==0)
{
s.y--;
s.f=1;
}
else if(s.x+1<R&&map1[s.x][s.y]==0)
{
s.x++;
s.f=0;
}
else if(s.y+1<C&&map2[s.x][s.y]==0)
{
s.y++;
s.f=3;
} } else if(s.f==2)
{
if(s.y+1<C&&map2[s.x][s.y]==0)
{
s.y++;
s.f=3;
}
else if(s.x>0&&map1[s.x-1][s.y]==0)
{
s.x--;
s.f=2;
}
else if(s.y>0&&map2[s.x][s.y-1]==0)
{
s.y--;
s.f=1;
}
else if(s.x+1<R&&map1[s.x][s.y]==0)
{
s.x++;
s.f=0;
}
}
else if(s.f==3)
{
if(s.x+1<R&&map1[s.x][s.y]==0)
{
s.x++;
s.f=0;
}
else if(s.y+1<C&&map2[s.x][s.y]==0)
{
s.y++;
s.f=3;
}
else if(s.x>0&&map1[s.x-1][s.y]==0)
{
s.x--;
s.f=2;
}
else if(s.y>0&&map2[s.x][s.y-1]==0)
{
s.y--;
s.f=1;
}
}
}
} void solve()
{
Point s=Point(0,ER,0);
Point ss=Point(R-1,EC,2);
dfs(s,ss);
dfs(ss,s);
if(num==R*C) puts("YES");
else puts("NO");
}
int main()
{
int Case; scanf("%d",&Case);
while(Case--)
{
init();
solve();
}
return 0;
}

hdu 3912 Turn Right的更多相关文章

  1. HDU 4869 Turn the pokers(推理)

    HDU 4869 Turn the pokers 题目链接 题意:给定n个翻转扑克方式,每次方式相应能够选择当中xi张进行翻转.一共同拥有m张牌.问最后翻转之后的情况数 思路:对于每一些翻转,假设能确 ...

  2. hdu 4869 Turn the pokers (2014多校联合第一场 I)

    Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. HDU 2438 Turn the corner(三分查找)

    托一个学弟的福,学了一下他的最简便三分写法,然后找了一道三分的题验证了下,AC了一题,写法确实方便,还是我太弱了,漫漫AC路!各路大神,以后你们有啥好的简便写法可以在博客下方留个言或私信我,谢谢了! ...

  4. HDU 4869 Turn the pokers (2014 Multi-University Training Contest 1)

    Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. hdu 2438 Turn the corner(几何+三分)

    Problem Description Mr. West bought a new car! So he is travelling around the city. One day he comes ...

  6. hdu 4869 Turn the pokers (思维)

    Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. hdu 2348 Turn the corner(三分&amp;&amp;几何)(中等)

    Turn the corner Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. hdu 2438 Turn the corner [ 三分 ]

    传送门 Turn the corner Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. HDU 4869 Turn the pokers (2014多校联合训练第一场1009) 解题报告(维护区间 + 组合数)

    Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. 【.NET调用Python脚本】C#调用python requests类库报错 'module' object has no attribute '_getframe' - IronPython 2.7

    最近在开发微信公众号,有一个自定义消息回复的需求 比如用户:麻烦帮我查询一下北京的天气? 系统回复:北京天气,晴,-℃... 这时候需要根据关键字[北京][天气],分词匹配需要执行的操作,然后去调用天 ...

  2. SQLServer中使用索引视图(物化视图)

    物化视图:以前用的普通的视图,普通视图就是一段逻辑语句,对性能没有任何的提升,也不能创建索引,而物化视图会把视图里查询出来的数据在数据库上建立快照,它和物理表一样,可以创建 索引,主键约束等等,性能会 ...

  3. border、margin、padding属性的区别

    可以先看下这个视频教程:http://my.tv.sohu.com/us/97014746/64226777.shtml 本文参考:http://www.cnblogs.com/chinhr/arch ...

  4. CSU1661: Query Mutiple

    Description One day,Little-Y saw many numbers standing in a row. A question suddenly appeared in her ...

  5. android 怎样内置/预置/预编译文件(运行程序,应用程序,apk, jar, lib 等随意文件)到系统中

    方法一:  如果要内置的软件名称为iperf.exe 1. 将iperf.exe放到Codebase的随意一个文件夹下(该文件夹必须可以在搜索Android.mk时被搜索到),比方system/ipe ...

  6. 国都企信通短信平台发送手机短信的python脚本一例

    一年前,由于工作需要,给以色列的同事解释一下国都短信平台的短信发送格式,本来不懂python的我硬着头皮写了一个sample,比较粗,能用,但不优美,希望以后学会python能改得像我同事写的那么优雅 ...

  7. C#HttpWebResponse请求常见的状态码

    成员名称 说明 Continue 等效于 HTTP 状态 100.Continue 指示客户端可能继续其请求. SwitchingProtocols 等效于 HTTP 状态 101.Switching ...

  8. left ,right ,cross ,full/left outer join/区别 详解

    --创建测试表wwif OBJECT_ID('qq') is not null drop table qqcreate table qq([序号] varchar(5),[内容1] varchar(1 ...

  9. Java方法的参数是按值传递的.【转】

    在Java中,所有的方法参数,都是"按值传递". 有那么一种说法,Java中基本类型是按值传递,对象是按引用传递.这个说法其实是不确切的,确切的说法是 Java中基本类型将值作为参 ...

  10. Linux sed命令删除指定行

    一.删除包含匹配字符串的行## 删除包含baidu.com的所有行sed -i '/baidu.com/d' domain.file 二.删除匹配行及后所有行## 删除匹配20160229的行及后面所 ...