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. HDU 4287 Intelligent IME

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. Linux禁止ping服务

    ping是一个通信协议,是ip协议的一部分,tcp/ip 协议的一部分.利用它可以检查网络是否能够连通,用好它可以很好地帮助我们分析判定网络故障.应用格式为:Ping IP地址.但服务启用ping有时 ...

  3. 屏幕录像专家V7.5(完美破解版,无水印)下载

    分享一个屏幕录像工具.屏幕录像专家V7.5(完美破解版,无水印).经测试可破解使用. 破解注册 点击安装.EXE,安装软件. 点击安装好的屏幕录像专家,会给你一个机器码,要你注册,复制下这个机器码(下 ...

  4. html+css+js实现复选框全选与反选

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  5. fluentd结合kibana、elasticsearch实时搜索分析hadoop集群日志<转>

    转自 http://blog.csdn.net/jiedushi/article/details/12003171 Fluentd是一个开源收集事件和日志系统,它目前提供150+扩展插件让你存储大数据 ...

  6. shell 求总分

    求总分并且输出: 文件a.txt 学号 姓名 性别 年龄 张三 男 赵四 男 李丽 女 文件b.txt 学号 语文 数学 英语 方法1: #!/bin/sh paste a.txt b.txt ccc ...

  7. java 项目request.getParameter("")接收不到值

    如果发现这个方法 以前能接收到参数,现在不能接收到参数的情况下 很有问题出来tocat 或许jak 的问题上, 换个低版本可能就好了

  8. #BeginLibraryItem 的疑问...

    <!-- #BeginLibraryItem "/library/ur_here.lbi" --><div style="padding:3px 15p ...

  9. Linux 网络配置(固定IP)

    通常linux作为服务器系统时,是不推荐安装图形界面的,因此我们需要掌握非图形界面下如何配置网络,主要两种方式,如下: 一.使用SETUP工具(redhat系列才可以,推荐此修改方式) 1.在命令行直 ...

  10. 该项目中不存在目标 precomputecompiletypescript The target "PreComputeCompileTypeScript" does not exist in the project

    Open Microsoft.TypeScript.targets file located under C:\Program Files (x86)\MSBuild\Microsoft\Visual ...