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>
#include <bitset>
#include <list>
#include <cstdlib>
#include <climits>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <stack>
#include <sstream>
#include <numeric>
#include <fstream>
#include <functional>

using namespace std;

#define MP make_pair
#define PB push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef pair<int,int> pii;
const int INF = INT_MAX / 3;
const double eps = 1e-8;
const LL LINF = 1e17;
const double DINF = 1e60;
const int maxn = 15;
const int WALL = 0;
const int EMPTY = 1;
const int STR = 2;
const int TAR = 3;
const int RESET = 4;
const int dx[] = {0,0,1,-1}, dy[] = {1,-1,0,0};
int mp[maxn][maxn], st[maxn][maxn][7];
int n,m,sx,sy,ex,ey;

void bfs() {
    queue<int> qx,qy,qt;
    qx.push(sx); qy.push(sy); qt.push(6);
    st[sx][sy][6] = 0;
    while(!qx.empty()) {
        int x = qx.front(), y = qy.front(), t = qt.front();
        int nowt = st[x][y][t];
        qx.pop(); qy.pop(); qt.pop();
        for(int i = 0;i < 4;i++) {
            int nx = x + dx[i], ny = y + dy[i];
            int nt = mp[nx][ny] == RESET ? 6 : t - 1;
            if(t - 1 > 0 && mp[nx][ny] != WALL) {
                if(nowt + 1 < st[nx][ny][nt]) {
                    st[nx][ny][nt] = nowt + 1;
                    qx.push(nx); qy.push(ny); qt.push(nt);
                }
            }
        }
    }
}

int main() {
    int T; scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&n,&m);
        memset(mp,0,sizeof(mp));
        memset(st,0x3f,sizeof(st));
        int inf = st[0][0][0],ans = inf;
        for(int i = 1;i <= n;i++) {
            for(int j = 1;j <= m;j++) {
                scanf("%d",&mp[i][j]);
                if(mp[i][j] == STR) {
                    sx = i; sy = j;
                }
                if(mp[i][j] == TAR) {
                    ex = i; ey = j;
                }
            }
        }
        bfs();
        for(int i = 1;i < 6;i++) ans = min(ans,st[ex][ey][i]);
        if(ans >= inf) puts("-1");
        else printf("%d\n",ans);
    }
    return 0;
}

  

时间: 2024-10-21 01:53:31

HDU 1072 Nightmare BFS的相关文章

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)

题目链接: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)

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,主要考虑剪枝,数组标记走过时炸弹剩余的时间,以及炸弹延时后将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[]={

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