hdu 1072 Nightmare

还是bfs,主要考虑剪枝,数组标记走过时炸弹剩余的时间,以及炸弹延时后将4变成1

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

struct node
{
    int x,y,s,w;

}t,t0;

int n,m,g[10][10],vis[10][10];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};

queue <node> q;
int bfs(int sx,int sy)
{

    int i,flag=-1;
    while(!q.empty()) q.pop();
    t.x=sx;
    t.y=sy;
    t.s=6;
    t.w=0;
    vis[t.x][t.y]=6;
    q.push(t);
    while(!q.empty())
    {
        t0=q.front();
        q.pop();
        //if(g[t0.x][t0.y]==4) t0.s=6;

        if(g[t0.x][t0.y]==3)
        {
            flag=t0.w;
            break;
        }

        for(i=0;i<4;i++)
        {
            t.x=t0.x+dx[i];
            t.y=t0.y+dy[i];
            t.w=t0.w+1;
            t.s=t0.s-1;
            if(g[t.x][t.y]==0||t.x<0||t.x>n||t.y<0||t.y>m||t.s==0) continue;
            if(g[t.x][t.y]==4)
            {
                    g[t.x][t.y]=0;
                    t.s=6;
            }
            if(vis[t.x][t.y]<t.s)
            {
                vis[t.x][t.y]=t.s;
                q.push(t);
            }
        }
    }
    return flag;
}

int main()
{
    int i,j,sx,sy,ex,ey,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(i=0;i<9;i++)
            for(j=0;j<9;j++)
                vis[i][j]=0;

        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
        {
            scanf("%d",&g[i][j]);
            if(g[i][j]==2)
            {
                sx=i;
                sy=j;
            }
        }

        printf("%d\n",bfs(sx,sy));
    }
    return 0;
}
时间: 2024-10-21 18:30:18

hdu 1072 Nightmare的相关文章

HDU 1072 Nightmare (BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意: 走迷宫,初始剩余时间为6min,每步1min:到reset区是若剩余时间大于0,则可以重置.到终点3区,若时间大于0,则成功逃脱.(可以走回路) 0:wall 1:可以走 2:起点 3:终点 4:剩余时间重置为6 源代码: #include<iostream> #include<cstring> #include<cstdio> #include<q

hdu 1072 Nightmare BFS,第一次刷BFS的题,感好牛逼的。。。

Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7758    Accepted Submission(s): 3723 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ti

HDU 1072 Nightmare(BFS)

Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9120    Accepted Submission(s): 4389 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ti

HDU 1072 Nightmare( 身上带有定时炸弹的他能否在炸弹爆炸之前离开—— BFS+DP思想)

Nightmare Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the

HDU 1072 Nightmare BFS

其实就是多加了一个引爆时间的限制条件,反正n,m给的很小,直接记录3维状态,之后就很随意了. #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque&g

HDU 1072 (不一样的入队条件) Nightmare

之前的BFS都是需要一个标记数组,但这个题不一样,因为可能一个格子不止走一次. 那么我们就要寻找新的入队条件:left比上次经过的时候大才入队(left表示上次经过该点时剩余的时间). 为什么呢?我们重复走过一个点只有一个可能,那就是为了去取那个,所以如果取完后再回头经过这个点的时候剩余时间变多了,我们的目的就达到了. left数组初值为0 优化: 重置时间的装置最多取一次就够了,所以可以再开一个标记数组vis记录装置是否用过. 1 //#define LOCAL 2 #include <cst

HDU 1072 bfs

Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7083    Accepted Submission(s): 3409 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a tim

HDU 3085 Nightmare Ⅱ (双向广搜)

题意:有M,G两人和鬼魂(Z)在n*m的方格内,M每秒走3步,G每秒走一步,鬼魂每秒走2步,问是否能 不遇到鬼魂下两人相遇,鬼魂可以穿墙(X),人不可以.初始鬼魂有2个. #include<stdio.h> #include<string.h> #include<string> #include<queue> #include<map> #include<iostream> #include<algorithm> #def

HDUJ 1072 Nightmare 搜索

Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6992    Accepted Submission(s): 3354 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ti