hdu1072
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
class Data
{
public:
int Etime;
int x, y;
int count;
};
int n, m, sx, sy;
int map[9][9];
int direction[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int bfs() //广度搜索
{
int i;
int tx, ty;
queue<Data> Que;
while(!Que.empty())
{
Que.pop();
}
Data Da, temp;
Da.x = sx; Da.y = sy;
Da.Etime = 6; Da.count = 0;
Que.push(Da); //起点入队
while(!Que.empty())
{
temp = Que.front();
Que.pop();
if(map[temp.x][temp.y] == 3) //到了终点
return temp.count;
if(temp.Etime == 1) //没到终点,时间变成1,下一步之后,时间变0,无论怎么走,都没时间了,直接跳过
continue; //忽略掉时间为0的,下面的引爆就不用判断时间
for(i = 0; i < 4; i++) //四个方向搜索
{
tx = temp.x + direction[i][0];
ty = temp.y + direction[i][1];
if(tx < 0 || tx >= n || ty < 0 || ty >= m || map[tx][ty] == 0)
continue;
Da.x = tx; Da.y = ty; Da.Etime = temp.Etime - 1; Da.count = temp.count + 1;
if(map[tx][ty] == 4) //引爆,重置时间
{
Da.Etime = 6;
map[tx][ty] = 0;
}
Que.push(Da);
}
}
return -1;
}
int main()
{
// freopen("data.txt", "r", stdin);
int T, i, j;
while(scanf("%d", &T) != EOF)
{
while(T--)
{
scanf("%d%d", &n, &m);
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
{
scanf("%d", &map[i][j]);
if(map[i][j] == 2) //找到起点
{
sx = i;
sy = j;
}
}
int ans = bfs();
cout << ans << endl;
}
}
return 0;
}
hdu1072的更多相关文章
- HDU-1072 Nightmare (bfs+贪心)
Nightmare Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- hdu1072 Nightmare---BFS
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意: 在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重 ...
- Nightmare HDU1072
非常标准的BFS 第一次写错了很多 1.到达4时设置为墙就好了 避免了死循环 2.不用开d数组 在结构体里面就行了 3.结构体初始化函数的写法: Node(int x=0,int y=0,int ...
- hdu1072(Nightmare)bfs
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU1072 Nightmare(BFS) 2016-07-24 14:02 40人阅读 评论(0) 收藏
Nightmare Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth w ...
- hdu1072(bfs)
#include<iostream> #include<queue> #include<cstring> using namespace std; int a[10 ...
- HDU1072:Nightmare
传送门 题意 给出一张n*m的图 0.墙 1.可走之路 2.起始点 3.终点 4.时间重置点 问是否能到达终点 分析 我的训练专题第一题,一开始我设个vis数组记录,然后写炸,不能处理重置点根vis的 ...
- HDU1072:Nightmare [DFS]
题目链接:Nightmare 题意: 给出一张n*m的图,0代表墙,1代表可以走,2代表起始点,3代表终点,4代表炸弹重置点 问是否能从起点到达终点 分析: 一道很好的DFS题目,炸弹重置点必然最多走 ...
- hdu1072 逃离迷宫系列 bfs
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1072/ 题意:逃离迷宫,路中可能有炸弹,总时间是6个单位,在有炸弹的位置,如果到达的时刻时间大于0,则恢复到6时 ...
随机推荐
- systemd基本使用
参考金步国翻译的systemd中文手册: http://www.jinbuguo.com/systemd/index.html 金步国翻译质量都很高, 非常适合做参考 原文:https://wiki. ...
- 算法(Algorithms)第4版 练习 1.3.41
方法实现: //1.3.41 public Queue(Queue<Item> q) { Queue<Item> result = new Queue<Item>( ...
- PHP如何得到数组最后元素的key
1.array_keys(end($arr)) $array = array( 'one'=>1, 'two'=>2, 'three'=>3, 'four'=>4, ); $a ...
- C - Alyona and SpreadsheetDP
题目链接 题意在一个矩阵中,询问l~r行是否有一列满足mp[i][j]>=mp[i-1][j](i属于l~r)即非递减序列,是输出Yes,否输出No 用vector<vector<i ...
- 【BZOJ 3238】差异 后缀自动机+树形DP
题意 给定字符串,令$s_i$表示第$i$位开始的后缀,求$\sum_{1\le i < j \le n} len(s_i)+len(s_j)-2\times lcp(s_i,s_j)$ 先考虑 ...
- 【leetcode刷题笔记】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- BZOJ4976: [Lydsy1708月赛]宝石镶嵌
BZOJ4976: [Lydsy1708月赛]宝石镶嵌 https://lydsy.com/JudgeOnline/problem.php?id=4976 分析: 本来是从\(k\le 100\)这里 ...
- ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)
Problem Description In Land waterless, water is a very limited resource. People always fight for the ...
- bzoj 3916: friends 瞎搞
题目: 有三个好朋友喜欢在一起玩游戏,A君写下一个字符串S,B君将其复制一遍得到T,C君在T的任意位置(包括首尾)插入一个字符得到U.现在你得到了U,请你找出S. 题解: 发现字符串的长度一定为奇数. ...
- 【Sublime Text 3】编译环境
1.默认编译时总会出现错误 报错: [Finished in 1.3s with exit code 1][cmd: ['gcc', 'V:\\Cynthia\\01.c']][dir: V:\Cyn ...