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;
}
解题思路:这道题目也是简单的广搜,主要处理好炸弹的状态就好了。
#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;
}
相关文章推荐
- hdu 1072 Nightmare (bfs+优先队列)
- HDU 1072 Nightmare【BFS】
- HDU 1072Nightmare(BFS+剪枝)
- hdu 1072 Nightmare
- HDU 1072 Nightmare
- hdu 1072 Nightmare
- HDU 1072 Nightmare【广搜】
- HDU 1072 Nightmare
- HDU 1072 Nightmare - BFS + 队列
- hdu 1072 Nightmare
- HDU 1072Nightmare(dfs+剪枝)
- HDU 1072 Nightmare (BFS)
- HDU 1072-Nightmare
- hdu-1072-Nightmare
- HDU_1072_Nightmare
- hdu_1072_nightmare_BFS_用时间来限定
- hdu1072 Nightmare bfs(java版本)
- HDU 1072 Nightmare 限制BFS
- hdu 1072 Nightmare (bfs+贪心)
- HDU-1072-Nightmare