hdu 1072 Nightmare(广搜)

来源:互联网 发布:sql 按条件合计 编辑:程序博客网 时间:2024/04/29 19:09

看题目请点这里

题意:

给你一幅nXm的地图,其中‘0’是不能走的,‘1’是可以走的,‘2’是起点,‘3’是终点,‘4’是可以重置时间的点,每次可以朝上下左右四个方向走,但不能超出地图范围,走一步需要1秒的时间。

若能在炸弹爆炸前走出去,则输出最少的时间,否则输出-1。

最初炸弹的时间是6,倒计时6秒就会爆炸,炸弹的时间可在在地图上的‘4’处重置为6秒,重置炸弹不需要时间。

若到‘4’时的时间为0,则不能重置时间;若到‘3’时的时间为0,则不能逃出。

代码:

#include<iostream>    #include<queue>    using namespace std ; struct node{    int x,y,t,p;    friend bool operator<(node c, node d)     //优先队列:t小的优先级高     {        return c.t>d.t ;     }    }first,next; int T,n,m,i,j,map[10][10],dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};          int bfs()    {        priority_queue<node>Q;    //优先队列    Q.push(first);            while(Q.empty()==0)        {            first=Q.top();        Q.pop() ;             for(i=0;i<4;i++)            {                next.x=first.x+dir[i][0];                 next.y=first.y+dir[i][1];             if(next.x>=0 && next.x<n && next.y>=0 && next.y<m && map[next.x][next.y]!=0 && first.p>1)            {                       if(map[next.x][next.y]==4)   //重置时间                {                    next.p=6;                    map[next.x][next.y]=0;   //一个重置点最多道一次就可以了                }                else                {                    next.p=first.p-1;                }                next.t=first.t+1;                if(map[next.x][next.y]==3)   //判断是否到终点                {                    return next.t;                }                Q.push(next);                    }            }        }    return -1;}int main()    {        scanf("%d",&T);    while(T--)    {        memset(map,0,sizeof(map));        scanf("%d %d",&n,&m);        for(i=0;i<n;i++)        {            for(j=0;j<m;j++)            {                scanf("%d",&map[i][j]);                if(map[i][j]==2)   //起点坐标及状态                {                    first.x=i;                    first.y=j;                    first.t=0;                    first.p=6;                    map[i][j]=0;                }            }        }        printf("%d\n",bfs());    }    return 0;}

原创粉丝点击