hdu 1728 逃离迷宫 bfs
2012-11-04 14:16
405 查看
每个点加4个方向
View Code
View Code
#include <stdio.h> #include <string.h> #include <queue> using namespace std; struct node{ int x,y,dir,step; friend bool operator <(node aa, node bb) { return aa.step>bb.step; } }a; int n,m,k,x1,y1,x2,y2; char map[105][105]; int dp[105][105][4]; int d[4][2]={0,1,1,0,0,-1,-1,0}; void init() { int i; for (i=0;i<n;i++)scanf("%s",map[i]); scanf("%d%d%d%d%d",&k,&y1,&x1,&y2,&x2); x1-=1;x2-=1;y1-=1;y2-=1; a.dir=-1,a.step=0,a.x=x1,a.y=y1; memset(dp,0,sizeof(dp)); } void bfs() { int i,tx,ty; priority_queue<node> q; q.push(a); while (!q.empty()) { a=q.top();q.pop(); for (i=0;i<4;i++) { tx=a.x+d[i][0]; ty=a.y+d[i][1]; while (tx>=0&&ty>=0&&tx<n&&ty<m&&map[tx][ty]=='.'&&!dp[tx][ty][i]) { node b; b.x=tx;b.y=ty;b.dir=i;b.step=a.step; if(i!=a.dir)b.step++; if(b.step>k+1)break; q.push(b); dp[tx][ty][i]=1; if(tx==x2&&ty==y2&&b.step<=k+1) { printf("yes\n"); return; } tx+=d[i][0]; ty+=d[i][1]; } } } printf("no\n"); } int main() { int T; scanf("%d",&T); while (T--) { scanf("%d%d",&n,&m); init(); bfs(); } return 0; }
相关文章推荐
- HDU 1728 逃离迷宫(DFS||BFS)
- hdu1728 逃离迷宫 bfs
- HDU 1728 逃离迷宫(BFS)
- hdu 1728 逃离迷宫 bfs
- HDU 1728 逃离迷宫 (bfs)
- HDU 1728 逃离迷宫(BFS)
- HDU 1728 逃离迷宫 BFS
- HDU 1728 逃离迷宫 BFS
- hdu 1728 逃离迷宫 bfs记转向
- HDU-1728-逃离迷宫(BFS)
- HDU 1728 逃离迷宫【BFS】
- HDU-1728 逃离迷宫(BFS)
- hdu 1728 逃离迷宫(bfs)
- hdu 1728 逃离迷宫(bfs)
- HDU 1728逃离迷宫(DFS或者BFS)
- HDU 1728逃离迷宫(DFS或者BFS)
- hdu 1728 逃离迷宫 bfs记步数
- hdu 1728 逃离迷宫 (论DFS思想在BFS中的应用)
- hdu 1728 逃离迷宫(BFS 有 限制条件,陷阱比较多)
- HDU 1728 逃离迷宫(拐弯问题,对BFS最优解的新理解)