hdu1180 诡异的楼梯
来源:互联网 发布:淘宝上怎么收藏店铺 编辑:程序博客网 时间:2024/05/19 00:36
这道题其实就是在BFS基础上的一个变形。要注意的细节是到达楼梯前若不能通过,可以选择停留在原地等楼梯转过来再过去,要把这种情况考虑进去。还有就是用当前的步数和楼梯的初始状态来判断楼梯此时的状态。另外一点就是楼梯所在的位置不需要判重,因为我们可能从下通过楼梯往上走,然后再通过这个楼梯从左往右。下面是AC代码。
#include<stdio.h>#include<queue>#include<string.h>using namespace std;int n,m,p[21][21],d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};char a[21][21];int sx,sy,ex,ey;struct node{ int x,y,step;};int pd(int x,int y){ if(x>0&&y>0&&x<=n&&y<=m&&!p[x][y]&&a[x][y]!='*') return 1; return 0;}void bfs(){ queue<node> q; node cur,next; cur.x=sx,cur.y=sy,cur.step=0; q.push(cur); while(!q.empty()) { cur=q.front(); //printf("%d %d %d\n",cur.x,cur.y,cur.step); q.pop(); for(int i=0;i<4;i++) { next.x=cur.x+d[i][0]; next.y=cur.y+d[i][1]; next.step=cur.step+1; if(pd(next.x,next.y)) { p[next.x][next.y]=1; if(a[next.x][next.y]=='|') { p[next.x][next.y]=0; if(i<=1&&next.step%2==1||i>1&&next.step%2==0) { next.x+=d[i][0]; next.y+=d[i][1]; if(p[next.x][next.y]) continue; else p[next.x][next.y]=1; } else { next.x=cur.x; next.y=cur.y; } } if(a[next.x][next.y]=='-') { p[next.x][next.y]=0; if(i>1&&next.step%2==1||i<=1&&next.step%2==0) { next.x+=d[i][0]; next.y+=d[i][1]; if(p[next.x][next.y]) continue; else p[next.x][next.y]=1; } else { next.x=cur.x; next.y=cur.y; } } if(next.x==ex&&next.y==ey) { printf("%d\n",next.step); return ; } q.push(next); } } }}int main(){ int i,j; while(scanf("%d%d",&n,&m)!=EOF) { getchar(); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { scanf("%c",&a[i][j]); if(a[i][j]=='S') sx=i,sy=j; if(a[i][j]=='T') ex=i,ey=j; } getchar(); } memset(p,0,sizeof(p)); p[sx][sy]=1; bfs(); } return 0;}
0 0
- hdu1180 诡异的楼梯
- HDU1180 诡异的楼梯
- hdu1180 诡异的楼梯
- hdu1180诡异的楼梯
- HDU1180 诡异的楼梯
- HDU1180-诡异的楼梯
- HDU1180 诡异的楼梯--BFS
- hdu1180诡异的楼梯 bfs
- hdu1180诡异的楼梯(bfs)
- HDU1180 诡异的楼梯[dfs]
- hdu1180 诡异的楼梯--BFS
- HDU1180-诡异的楼梯(bfs)
- HDU1180 诡异的楼梯(BFS)
- hdu1180——诡异的楼梯
- hdu1180诡异的楼梯(BFS+优先队列)
- hdu1180诡异的楼梯(广搜)
- Hdu1180 - 诡异的楼梯 - 广度优先搜索
- 【搜索-BFS】HDU1180诡异的楼梯
- To free pagecache
- 如何在webview中加载本地包含图片的html文件?
- 云平台简介
- 我们需要什么样的开发人员
- Socket通信
- hdu1180 诡异的楼梯
- HDU 2177 取(2堆)石子游戏 Wythoff Game 求第一步方案
- 利用Scala语言开发Spark应用程序
- 兔子--动态设置控件的宽高
- 条款03:尽可能使用const
- android 获取手机SD卡和手机的内部存储
- LVS学习笔记 3LVS-DR实验 初步试验
- Javascript学习
- 修改ubuntu系统的时间