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;
}

时间: 2024-12-08 22:37:33

hdu1072的相关文章

Hdu-1072

题目描述: 首先输入一个N:代表测试数据的个数; 然后每个测试数据的开头第一行输入一个n和一个命令(FIFO或FILO<就是先进先出或先进后出>) 然后是该测试数据的n行,每行包括"IN"加一个数字(代表入栈或入队)或者一个"OUT"(代表出队或出栈) 若已经是空的了遇到"OUT"命令时输出"None": 其实就是c++STL的队列和栈的简单应用: 代码如下: ***************************

hdu1072【bfs可重复走】

大意: 给一个矩阵   有一个六秒之内会爆炸的炸弹  爆炸事件在数值为4的位置会重置为6 0: The area is a wall, Ignatius should not walk on it.1: The area contains nothing, Ignatius can walk on it.2: Ignatius' start position, Ignatius starts his escape from this position.3: The exit of the lab

HDU1072 Nightmare 【BFS】

Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7367    Accepted Submission(s): 3530 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ti

Hdu1072广搜

题意:0不能走,1可以走,2起始位置,3中点,4时间变成6.初始时间为6  走到终点或者4时时间不能为0.问能否走到终点和 到终点的最短距离. 反正时间就是6 ,每个点可以重复走,随便走就行.剪纸 就是 经过4这个点只要经过一次,第二次经过这个点的路程一定比第一次经过的长  并且都重置了时间. #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #inclu

hdu1072 bfs时间优化剪枝

Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9424    Accepted Submission(s): 4551 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a tim

hdu1072(dfs和bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 题意:有一个n*m的矩阵迷宫,2是起点,3是终点,0是墙不能走,1是路,现在有6分钟炸弹要爆炸,没走一步花一分钟,问你是否能到达终点 能,则输出最短时间,不能输出-1.而4是重置时间,将时间变为6分钟,注意的是,不能正好时间为0,到达终点或者重置时间. bfs的思路比较简单,就是从起点开始搜索.遇到终点则就是最短时间,遇到4(重置设备)时间变为6分钟,并将此地方变为墙,因为每个重置设备在保证时

hdu1072 逃离迷宫系列 bfs

题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1072/ 题意:逃离迷宫,路中可能有炸弹,总时间是6个单位,在有炸弹的位置,如果到达的时刻时间大于0,则恢复到6时间,炸弹的位置可以重复到达,求出最终至少需要多少步才能走出迷宫,到达终点.这样的最优化问题和地图相关的,bfs应该足以解决.我们考虑到一个位置可能被多次访问,所以状态参数应该设置一个时间,设置为访问时的剩余时间,因为如果一个位置第一次访问时剩余时间是t,则下一次访问时如果剩余时间还是t的话,走的步数

Nightmare(DFS)

Nightmare    hdu1072 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8871    Accepted Submission(s): 4270 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth