hdu 1728 逃离迷宫 bfs

每个点加4个方向

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;
}

 

posted @ 2012-11-04 14:16  104_gogo  阅读(117)  评论(0编辑  收藏  举报