HDOJ 1072 Nightmare

来源:互联网 发布:诺基亚6220c软件 编辑:程序博客网 时间:2024/05/29 11:30

题目很简单,一个图,0表示墙,2代表入口,3代表出口,4代表一个炸弹重启器,可以把计数的时间变为0;求最短逃出的时间

题目求最短时间,所以可以用BFS来做,首先记录的x,y坐标,还需要一个计数时间t,如果t大于6了爆炸了,还需要一个总时间time,代表走的总时间,注意的是,当到达4时,就把时间变为0这样可以获得更多的逃跑时间,但因为每个地方可以重复走,而4重复走是没有意义的,因为每走到这个点重启一个,相当于没走,但总时间会++,所以要标记4 的位置,不能重复使用来达到BFS的效果;
至于清空队列,是有必要的,否则会出WA;
至于DFS也应该是能做的,懒就没有做;

Sample Input
33 32 1 11 1 01 1 34 82 1 1 0 1 1 1 01 0 4 1 1 0 4 11 0 0 0 0 0 0 11 1 1 4 1 1 1 35 81 2 1 1 1 1 1 4 1 0 0 0 1 0 0 1 1 4 1 0 1 1 0 1 1 0 0 0 0 3 0 1 1 1 4 1 1 1 1 1
 

Sample Output
4-113

#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>using namespace std;int map[10][10];int dir[4][2]={-1,0,0,-1,1,0,0,1};int use[10][10];int n,m;struct te{    int x;    int y;    int t;    int time;}ans,tem;queue<te>que;int BFS(){    while(que.size())    {        tem=que.front();        que.pop();        if(map[tem.x][tem.y]==3&&tem.t<6)        {            return tem.time;        }        for(int i=0;i<4;i++)        {            int dx=tem.x+dir[i][0];            int dy=tem.y+dir[i][1];            int t=tem.t;            int time=tem.time;            if(dx>=0&&dx<n&&dy>=0&&dy<m&&map[dx][dy]!=0&&tem.t<5&&!use[dx][dy])            {                if(map[dx][dy]==4)                {                    use[dx][dy]=1;                    t=-1;                }                te tem1;                tem1.x=dx;                tem1.y=dy;                tem1.t=t+1;                tem1.time=time+1;                que.push(tem1);            }        }    }    return -1;}int main(){    int flag;    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        memset(use,0,sizeof(use));        for(int i=0;i<n;i++)            for(int j=0;j<m;j++)            scanf("%d",&map[i][j]);        while(que.size())        {            que.pop();        }        for(int i=0;i<n;i++)            for(int j=0;j<m;j++)            {                if(map[i][j]==2)                {                    ans.x=i;                    ans.y=j;                    ans.t=0;                    ans.time=0;                    que.push(ans);                     flag=BFS();                }            }        printf("%d\n",flag);    }return 0;}


0 0
原创粉丝点击