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. Shiro Quartz之Junit測试Session管理

    Shiro的quartz主要API上提供了org.apache.shiro.session.mgt.quartz下session管理的两个类:QuartzSessionValidationJob和Qu ...

  2. ExtJS学习-----------Ext.String,ExtJS对javascript中的String的扩展

    关于ExtJS对javascript中的String的扩展,能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 以 ...

  3. [TypeScript] Inheritance

    Inheritance is a way toindicate that a class receives behavior from a parent class. Then we can over ...

  4. MyBatis报错

    1.错误描写叙述 2014-11-2 15:03:11 org.apache.catalina.core.StandardEngine start 信息: Starting Servlet Engin ...

  5. [译]C++如何切分字符串

    声明: 翻译仅以技术学习和交流为目的,如需转载请务必标明原帖链接. http://stackoverflow.com/questions/236129/how-to-split-a-string-in ...

  6. Linux 下实现控制屏幕显示信息和光标的状态

    //display.h /************************************************************* FileName : display.h File ...

  7. mac svn .a文件的上传方法

    1.首先确认是否安装了Command Line Tools,如果没有,就Xcode-Preference-Downloads,选择Command Line Tools-install就可以了 2.打开 ...

  8. asp.net,mvc4,mysql数据库,Ef遇到问题集合

    asp.net  mvc  mysql数据库,在我一个新手自学MVC4时遇到如下的问题,一一解决掉的方法记录如下方便自己日后查看,也为了方便一些像我一样的新手遇到如下问题时,提供参考 问题一: 解决办 ...

  9. ASPNET5的依赖注入

    ASP.NET5设计的时候就是以DI为基础的,它可以利用内建的框架在Startup类的方法中,把依赖注入进去.应用服务也可以被配置的注入.默认的服务容器提供一些基本的功能,它并不打算代替现代主流的DI ...

  10. java工程中使用freemarker例子

    新建java project,引入freemarker.jar, 本工程是用的版本:freemarker-2.3.20 版本 java工程目录如下: test.ftl文件 HTML代码 name : ...