hdu 3912 Turn Right
http://acm.hdu.edu.cn/showproblem.php?pid=3912
:
#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的更多相关文章
- HDU 4869 Turn the pokers(推理)
HDU 4869 Turn the pokers 题目链接 题意:给定n个翻转扑克方式,每次方式相应能够选择当中xi张进行翻转.一共同拥有m张牌.问最后翻转之后的情况数 思路:对于每一些翻转,假设能确 ...
- hdu 4869 Turn the pokers (2014多校联合第一场 I)
Turn the pokers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 2438 Turn the corner(三分查找)
托一个学弟的福,学了一下他的最简便三分写法,然后找了一道三分的题验证了下,AC了一题,写法确实方便,还是我太弱了,漫漫AC路!各路大神,以后你们有啥好的简便写法可以在博客下方留个言或私信我,谢谢了! ...
- 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 ...
- hdu 2438 Turn the corner(几何+三分)
Problem Description Mr. West bought a new car! So he is travelling around the city. One day he comes ...
- hdu 4869 Turn the pokers (思维)
Turn the pokers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 2348 Turn the corner(三分&&几何)(中等)
Turn the corner Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 2438 Turn the corner [ 三分 ]
传送门 Turn the corner Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 4869 Turn the pokers (2014多校联合训练第一场1009) 解题报告(维护区间 + 组合数)
Turn the pokers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- Invocation of init method failed; nested exception is org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.impl
严重: Exception sending context initialized event to listener instance of class org.springframework.we ...
- php做站点购物车 你搞懂了吗?
网上购物现已成为时尚,客户选择一个商品将其放入到购物车,然后返回继续购物或者去收银台,这个功能怎样实现呢?今天capucivar就将使用PHP来实现这个购物车的功能. 首先,做一个简单的首页,从数据库 ...
- 老鸟的Python新手教程
重要说明 这不是给编程新手准备的教程,假设您入行编程不久,或者还没有使用过1到2门编程语言,请移步!这是有一定编程经验的人准备的.最好是熟知Java或C,懂得命令行,Shell等.总之,这是面向老鸟的 ...
- [转] boost库的Singleton的实现以及static成员的初始化问题
http://www.cnblogs.com/kex1n/archive/2011/04/05/2006194.html effectie c++的条款4中提到: (global对象,定义在names ...
- [转] weak_ptr解决shared_ptr环状引用所引起的内存泄漏
http://blog.csdn.net/liuzhi1218/article/details/6993135 循环引用: 引用计数是一种便利的内存管理机制,但它有一个很大的缺点,那就是不能管理循环引 ...
- iOS网络HTTP、TCP、UDP、Socket 知识总结
OSI 七层模型 我们一般使用的网络数据传输由下而上共有七层,分别为物理层.数据链路层.网络层.传输层.会话层.表示层.应用层,也被依次称为 OSI 第一层.第二层.⋯⋯. 第七层. 如下图: 各层功 ...
- Python 代码实现模糊查询
Python 代码实现模糊查询 1.导语: 模糊匹配可以算是现代编辑器(如 Eclipse 等各种 IDE)的一个必备特性了,它所做的就是根据用户输入的部分内容,猜测用户想要的文件名,并提供一个推荐列 ...
- EventBus 事件总线 原理
原理 一句话描述:register会把当前类中匹配的方法,存入一个map,而post会根据实参去map查找进行反射调用 撇开专业术语,其实EventBus就是在内部[存储]了一堆onEvent开头的方 ...
- codevs3013单词背诵
/* 手打的哈希+线性的维护 第一问:hash一下 并且用个h记录某个单词要背的 第二问:线性的跑一边 开始队列里装下前一些单词使这一坨符合要求 并且记录出现次数num 然后开始从前面删 删除的条件: ...
- php创建读取 word.doc文档
创建文档; <?php $html = "this is question"; for($i=1;$i<=3;$i++){ $word = new word(); $w ...