hdu1180 诡异的楼梯
2015-05-08 16:16
183 查看
这道题其实就是在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; }
相关文章推荐
- hdu 1180 诡异的楼梯
- hdu 1180 诡异的楼梯
- 诡异的楼梯 hdu 1180
- hdu1180——诡异的楼梯(优先队列+bfs)
- HDU 1180 诡异的楼梯(BFS+奇偶步数判断)
- HDU 1180 诡异的楼梯
- Hdu1180 - 诡异的楼梯 - 广度优先搜索
- 【BFS&&优先队列】HDU 1180 诡异的楼梯
- HDU 1180 诡异的楼梯
- hdu1180诡异的楼梯【优先队列+广搜】
- hdu 1180 诡异的楼梯 BFS+优先队列
- Hdu-1180(诡异的楼梯)
- hdu 1180:诡异的楼梯(BFS广搜)
- HDU 1180 (诡异的楼梯)+HDU 2102 (A计划)
- HDU 1180 诡异的楼梯 BFS
- HDU 1180 诡异的楼梯
- 诡异的楼梯 HDU - 1180
- HDU1180——诡异的楼梯(BFS+优先队列)
- HDU 1180诡异的楼梯
- HDU-1180-诡异的楼梯