zoukankan      html  css  js  c++  java
  • hdu 1072

    http://acm.hdu.edu.cn/showproblem.php?pid=1072
    题目难点在于标记 状态
    vis[x][y][time] 表示在倒计时为time时走到了(x,y)处
    优先队列+BFS
    #include<iostream>
    #include<stdio.h>
    #include<queue>
    #include<cstring>
    using namespace std;
    int cas,n,m;
    int map[10][10],vis[10][10][7],dir[4][2]={1,0,-1,0,0,1,0,-1},ans;
    struct node
    {
        int x,y,step,time;
        node(int _x=0,int _y=0,int _step=0,int _time=0):x(_x),y(_y),step(_step),time(_time){};
        friend bool operator<(const node&a,const node&b)
        {
            return a.step>b.step;
        }
    };
    node tar;
    priority_queue <node > q;
    void BFS()
    {
        while(!q.empty())
        {
            node t=q.top();
            q.pop();
            if(t.time==1)continue;
            
            for(int k=0;k<4;k++)
            {
                int x=t.x+dir[k][0];
                int y=t.y+dir[k][1];
                if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y][t.time-1]&&map[x][y]!=0)
                {
                     if(map[x][y]==1||map[x][y]==2)
                     {
                         vis[x][y][t.time-1]=1;
                         q.push(node(x,y,t.step+1,t.time-1));
                     }
                     if(map[x][y]==3)
                     {
                         vis[x][y][t.time-1]=1;
                         else {ans=t.step+1;return;}
    
                     }
                     
                     if(map[x][y]==4)
                     {
                         vis[x][y][t.time-1]=1;
                         q.push(node(x,y,t.step+1,t.time-1));
                         if(!vis[x][y][6])
                         {
                             vis[x][y][6]=1;
                             q.push(node(x,y,t.step+1,6));
                         }
                     }
                }
            }
        }
        
    }
    int main()
    {
         scanf("%d",&cas);
         while(cas--)
         {
             ans=-1;
             memset(vis,0,sizeof(vis));
             scanf("%d%d",&n,&m);
             for(int i=0;i<n;i++)
                 for(int j=0;j<m;j++)
                 {
                     scanf("%d",&map[i][j]);
                     if(map[i][j]==2)
                     {
                         q.push(node(i,j,0,6));
                         vis[i][j][6]=1;
                     }
                    
                 }
                 BFS();
               printf("%d\n",ans);
             
    
         }
    }
    
  • 相关阅读:
    动态规划-树形dp
    排队打水
    耍杂技的牛
    合并果子
    贪心问题-区间类
    动态规划-状态压缩dp
    Linux分区知识及企业场景分区76
    企业面试题-find结合sed查找替换
    企业面试题-利用三剑客
    alias-unalias
  • 原文地址:https://www.cnblogs.com/sook/p/2014281.html
Copyright © 2011-2022 走看看