博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu1072
阅读量:4356 次
发布时间:2019-06-07

本文共 1314 字,大约阅读时间需要 4 分钟。

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

转载于:https://www.cnblogs.com/wangkun1993/p/6337661.html

你可能感兴趣的文章
第十六章 数组
查看>>
又是一年毕业季
查看>>
ThinkPHP 3.2.3 Widget 扩展的使用
查看>>
(转)同步异步,阻塞非阻塞 和nginx的IO模型
查看>>
(转) CentOS7.4 + MySQL8.0 + Git + Gogs搭建
查看>>
简单选项卡加圆角
查看>>
ZOJ3741 状压DP Eternal Reality
查看>>
POJ 1741 Tree(树的分治)
查看>>
soritong MP3播放器缓冲区溢出漏洞分析
查看>>
how to istall virtualbox on centos
查看>>
PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大、小写
查看>>
LA 3902 UVA 1267 - Network
查看>>
docker 学习(1)
查看>>
递归--任意字符全排列(第二次写)
查看>>
17.10.24 数据最水的一次考试
查看>>
python_SMTP and POP3
查看>>
lambda匿名函数
查看>>
js常用方法
查看>>
建造者模式
查看>>
Spring入门教程:通过MyEclipse开发第一个Spring项目
查看>>