HDU 1072 Nightmare

来源:互联网 发布:淘宝高达模型店推 编辑:程序博客网 时间:2024/05/20 17:38

题目链接:Nightmare

解题思路:这道题目也是简单的广搜,主要处理好炸弹的状态就好了。


#include<cstdio>#include<cstring>#include<queue>#define MAX 10#define MAXT 7using namespace std;struct node{    int x, y, s, t;    node(){}    node(int xx, int yy, int tt, int ss){        x = xx, y = yy, t = tt, s = ss;    }};bool v[MAX][MAX][MAXT];int g[MAX][MAX];int n, m;int dir[4][2] = {1,0,-1,0,0,1,0,-1};int sx, sy;bool check(int x, int y){    if(x < 0 || x >= n || y < 0 || y >= m || g[x][y] == 0){        return false;    }    return true;}int bfs(){    int i, j, k;    queue<node> mq;    memset(v, false, sizeof(v));    mq.push(node(sx, sy, 6, 0));    v[sx][sy][6] = true;    while(!mq.empty()){        node s = mq.front();        //printf("%d %d %d %d\n", s.x, s.y, s.t, s.s);        mq.pop();        for(i = 0; i < 4; i++){            int xx = s.x + dir[i][0];            int yy = s.y + dir[i][1];            int tt = s.t - 1;            int ss = s.s + 1;            if(check(xx, yy) && !v[xx][yy][tt] && tt > 0){                v[xx][yy][tt] = true;                if(g[xx][yy] == 3){                    return ss;                }                else if(g[xx][yy] == 4){                    mq.push(node(xx, yy, 6, ss));                    v[xx][yy][6] = true;                }                else{                    mq.push(node(xx, yy, tt, ss));                }            }        }    }    return -1;}int main(){    int i, j, k, t;    scanf("%d", &t);    while(t--){        scanf("%d%d", &n, &m);        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());    }    return 0;}


0 0
原创粉丝点击