hdu 1180 诡异的楼梯
来源:互联网 发布:计算器java程序代码 编辑:程序博客网 时间:2024/06/09 21:05
hdu 1180 诡异的楼梯 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1180
题目分析:题目描述有点不大清晰,需要补充一点,虽然不能在楼梯上停留,但走廊还是可以停留的,比如在楼梯前等一分钟是可以的。其它的都跟普通BFS一样,楼梯需要加判断,判断当前能否直接通过眼前的楼梯,如果不能就等一分钟。需要注意的是楼梯有可能出现在出口前,从楼梯出来就是出口。
code:
#include<cstdio>#include<queue>#include<cstring>using namespace std;char map[30][30];int m,n,ex,ey,dir[4][2]={1,0,-1,0,0,1,0,-1};struct node{ int x,y,step;};bool judge(node no){ int x=no.x,y=no.y; return x>=0&&y>=0&&x<m&&y<n&&map[x][y]!='*';}bool able(node f,node n){ int sx=f.x,sy=f.y,ex=n.x,ey=n.y; int temp=(sx==ex?1:0); temp+=map[n.x][n.y]=='-'?1:0; temp+=f.step%2?1:0; return !(temp%2);}int BFS(int x,int y){ queue<node>q; node first,next; first.x=x; first.y=y; first.step=0; map[first.x][first.y]='*'; q.push(first); while(!q.empty()) { first=q.front(); q.pop(); for(int i=0;i<4;i++) { next.x=first.x+dir[i][0]; next.y=first.y+dir[i][1]; if(map[next.x][next.y]=='|'||map[next.x][next.y]=='-') { if(!able(first,next)) { next.step=first.step+2; } else next.step=first.step+1; next.x+=dir[i][0]; next.y+=dir[i][1]; } else next.step=first.step+1; if(next.x==ex&&next.y==ey)return next.step; if(judge(next)) { //printf("in x==%d y==%d step==%d\n",next.x,next.y,next.step); q.push(next); map[next.x][next.y]='*'; } } } return -1;}int main(){ int i,j,sx,sy; while(scanf("%d%d",&m,&n)!=EOF) { for(i=0;i<m;i++) { scanf("%s",map[i]); for(j=0;j<n;j++) { if(map[i][j]=='S')sx=i,sy=j; if(map[i][j]=='T')ex=i,ey=j; } } printf("%d\n",BFS(sx,sy)); } return 0;}
PS:该去找剪枝学了…
0 0
- hdu 1180 诡异的楼梯
- hdu 1180 诡异的楼梯
- HDU 1180 诡异的楼梯
- Hdu-1180(诡异的楼梯)
- HDU 1180 诡异的楼梯
- hdu 1180 诡异的楼梯
- 诡异的楼梯 hdu 1180
- HDU-1180 诡异的楼梯
- hdu 1180 诡异的楼梯
- hdu 1180诡异的楼梯
- HDU 1180诡异的楼梯
- hdu 1180 诡异的楼梯
- hdu 1180 诡异的楼梯
- HDU 1180 诡异的楼梯
- hdu 1180 诡异的楼梯
- hdu 1180 诡异的楼梯
- HDU 1180 诡异的楼梯
- hdu 1180 诡异的楼梯
- 学习iOS开发之扫描二维码
- android学习——使用TableLayout动态生成表格,并为tablerow中的列添加点击监听
- 顺序表的就地逆置
- php 函数来区别ip地址是否是本地域
- NYOJ 676 小明的求助
- hdu 1180 诡异的楼梯
- FastJson使用
- Freemarker源码解析
- 开源项目之工具包
- leetcode之 Path Sum
- android异步UI刷新实例总结
- HUD 1219 AC Me
- item17: 以独立语句将newed对象置入智能指针
- 客户端 用不用 bind 的区别