您的位置:首页 > 其它

HDU 1072 Nightmare

2014-12-19 15:45 501 查看
题目链接:Nightmare

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

#include<cstdio>
#include<cstring>
#include<queue>
#define MAX 10
#define MAXT 7

using 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  简单题 bfs