zoukankan      html  css  js  c++  java
  • hdu 1728 逃离迷宫(dFS+优先队列)

    求转弯最少的走路方式!!!!


    #include<stdio.h>

    #include<string.h>
    #include<queue>
    using namespace std;


    struct node
    {
    int x,y;
    int step;
    friend bool operator<(node a,node b)
    {
    return a.step>b.step;
    }
    };


    char map[200][200];
    int visit[200][200],n,m,p;
    int dir[4][2]={0,1,1,0,-1,0,0,-1};


    int judge(int x,int y)
    {
    if(x>=1&&x<=n&&y>=1&&y<=m&&map[x][y]=='.')
    return 1;
    return 0;
    }


    int dfs(int sx,int sy,int dx,int dy)
    {
    priority_queue<node>q;
    int i,x,y;
    node cur,next;
    cur.x=sx;cur.y=sy;cur.step=-1;
    q.push(cur);
    visit[sx][sy]=1;
    if(sx==dx&&sy==dy)
    return 1;
    while(!q.empty())
    {
    next=q.top();
    q.pop();
    for(i=0;i<4;i++)
    {
    x=next.x+dir[i][0];
    y=next.y+dir[i][1];
    while(judge(x,y))//同一方向走到底
    {
    if(visit[x][y]==0)
    {
    cur.x=x;
    cur.y=y;
    cur.step=next.step+1;
    if(x==dx&&y==dy&&cur.step<=p)
    return 1;
    q.push(cur);
    visit[x][y]=1;
    }
    x=x+dir[i][0];
    y=y+dir[i][1];
    }
    }
    }
    return 0;


    }
    int main()
    {
    int i,T,sx,sy,dx,dy;
    scanf("%d",&T);
    while(T--)
    {
    scanf("%d%d",&n,&m);
    memset(visit,0,sizeof(visit));
    for(i=1;i<=n;i++)
    scanf("%s",map[i]+1);
    scanf("%d%d%d%d%d",&p,&sy,&sx,&dy,&dx);
    if(dfs(sx,sy,dx,dy))//输入方式坑人
    printf("yes ");
    else
    printf("no ");
    }
    return 0;


    }

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1728

  • 相关阅读:
    POJ_2513Colored Sticks 字典树+
    hdu1098:Ignatius's puzzle
    hdu1010:Tempter of the Bone 搜索+剪枝
    轻院1875: 蛤玮的财宝
    POJ3069:Saruman's Army
    轻院1874: 蛤玮学计网
    Educational Codeforces Round 18 E. Colored Balls
    浏览器f12的方法下载资源
    把手机上的新浪微博客户端卸载了
    Xmind使用总结
  • 原文地址:https://www.cnblogs.com/pangblog/p/3266760.html
Copyright © 2011-2022 走看看