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. tomcat绿色版及安装版修改内存大小的方法

    1.对于安装版,比较方便了,直接运行tomcat6w.exe,选择Java选项卡, 在这里,可以设置初始化内存,最大内存,线程的内存大小. 初始化内存:如果机器的内存足够大,可以直接将初始化内存设置为 ...

  2. Enterprise Architect使用教程

    一.Enterprise Architect简介 Enterprise Architect是一个对于软件系统开发有着极好支持的CASE软件(Computer Aided Software Engine ...

  3. SimpleDateFormat使用简析

    title: SimpleDateFormat使用简析 date: 2016-07-11 11:48:20 tags: Java SimpleDateFormat --- [转载自博客:http:// ...

  4. cogs 53 多人背包

    /* 要求每个最优 即累加前k优解 注意不用去重 */ #include<iostream> #include<cstdio> #include<cstring> ...

  5. 【开源java游戏框架libgdx专题】-11-核心库-演员类

    演员类,又称为Actor类,是libgdx开发中最基本的元素,可以被继承. 演员类,从OpenGL类的角度来理解,可以称为一个二维场景节点.它本身具有位置(postion).边界矩形(类似Retang ...

  6. Windows XP CD 函数不正确

    参考这篇文章:http://support.hp.com/cn-zh/document/c00760286 一,在设备管理中查看,如果刻录机名称中含 ROM,则需确认设备是否可写 二,若确定设备可写, ...

  7. log4net 生成多个空文件问题

    使用 log4net 的伙伴,相信很多人会遇到我现在这个问题 ,一般项目需求,便于管理和查找原因,会让项目的日志文件分类记录,然而会出现很多空日志, 出现这个问题的原因通常是我们web.config配 ...

  8. datazen Active Directory AD 配置

    今天苦心经营的datazen 链接AD,文档已经无法吐槽了简单的几句话,根本不够用. 先说一下链接AD 的好处吧, 1 首先免去设置密码的麻烦,因为直接用AD账号的密码. 2 更安全,因为客户可不想自 ...

  9. 安卓开机启动service后台运行

    安卓开机启动service后台运行 Android开机启动时会发送一个广播android.intent.action.BOOT_COMPLETED,捕捉到这个广播,然后可以进行相应的操作,比如:通过捕 ...

  10. Javascript闭包简单理解

    提到闭包,想必大家都早有耳闻,下面说下我的简单理解.平时写代码.第三方框架和组件都或多或少用到了闭包.所以,了解闭包是非常必要的.呵呵... 一.什么是闭包简而言之,就是能够读取其他函数内部变量的函数 ...