#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define size 10
#define Max 100000
using namespace std;
struct point{
int x,y,now,l;
};
int dx[]={,,-,},dy[]={-,,,};
int map[size][size];
int step[size][size],ti[size][size];
int n,m;
point start;
int x,y,now,l;
bool flag;
void bfs()
{
queue<point> s;
s.push(start);
int i,j;
point next;
while(!s.empty())
{
point pos=s.front();
s.pop();
if(map[pos.x][pos.y]==) {flag=;cout<<pos.l<<endl;break;}
if(pos.now>)
{
for(i=;i<;i++)
{
x=pos.x+dx[i];
y=pos.y+dy[i];
now=pos.now-;
l=pos.l+;
if((x>=&&y>=&&x<n&&y<m&&map[x][y]!=))
{
if(map[x][y]==) {now=;map[x][y]=;}
next.x=x,next.y=y,next.now=now,next.l=l;
s.push(next);
}
}
}
}
}
int main()
{
int T,i,j;
freopen("in.txt","r",stdin);
cin>>T;
while(T--)
{
cin>>n>>m;
for(i=;i<n;i++)
for(j=;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]==) {start.x=i,start.y=j;}
}
for(i=;i<size;i++)
for(j=;j<size;j++)
step[i][j]=Max;
flag=;
start.now=,start.l=;
bfs();
if(!flag) cout<<-<<endl;
}
}

Nightmare(BFS)的更多相关文章

  1. 【HDU - 3085】Nightmare Ⅱ(bfs)

    -->Nightmare Ⅱ 原题太复杂,直接简单的讲中文吧 Descriptions: X表示墙 .表示路 M,G表示两个人 Z表示鬼 M要去找G但是有两个鬼(Z)会阻碍他们,每一轮都是M和G ...

  2. HDU-1072 Nightmare (bfs+贪心)

    Nightmare Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  3. 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 ...

  4. hdoj1072 Nightmare(bfs)

    题目大意: 在迷宫中有一个炸弹,过六个单位时间就会爆炸,要你求一个起点到迷宫的终点的最短距离,迷宫中有时间重置器,当你走到这个格子,炸弹的爆炸时间重新置为0,迷宫中标识为墙壁的格子不能走,到达任意一个 ...

  5. HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)

    HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  6. nyoj 21三个水杯(BFS + 栈)

    题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=21 思想: 看了一下搜索就来写了这题(BFS 找出最短路径 所以用此来进行搜索) 这题在 ...

  7. POJ3279 Catch That Cow(BFS)

    本文来源于:http://blog.csdn.net/svitter 意甲冠军:给你一个数字n, 一个数字k.分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x.求主人找到 ...

  8. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  9. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

随机推荐

  1. [TYVJ] P1025 单数?双数?

    单数?双数? 背景 Background USACO OCT09 1ST   描述 Description Bessie那惨无人道的二年级老师搞了一个有 N (1 <= N <= 100) ...

  2. SGU176 Flow construction

    http://acm.sgu.ru/problem.php?contest=0&problem=176 有源汇上下界最小流,可以选择跟有源汇上下界最大流一样的建图方式,然后用二分去二分t-&g ...

  3. Redhat Enterprise Linux中如何关闭SELinux?

    转自http://www.cnitblog.com/lywaml/archive/2005/06/21/468.html 红帽企业 Linux 4 包括了一个 SELinux 的实现.SELinux ...

  4. windows中.msc文件详解

    msc是Microsoft Management Console的缩写.其实是一种可执行程序类型,可.exe类似.一般可以通过直接双击.msc文件或者在windows的运行中输入相应的文件名来启动. ...

  5. Best Meeting Point 解答

    Question A group of two or more people wants to meet and minimize the total travel distance. You are ...

  6. Divide and Conquer.(Merge Sort) by sixleaves

    algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...

  7. poj2242

                                                        The Circumference of the Circle Time Limit: 1000 ...

  8. 《数据通信与网络》笔记--IPSec

    1.IP层的安全:IPSec IP层安全(IP security,IPsec)是由因特网工程任务组(IETF)设计用来为IP层的分组提供安全的一组协议.IPsec帮助 生成经过鉴别的与安全的IP层的分 ...

  9. c语言所有的errno枚举值含义

    可以通过以下代码,获取所有的错误码信息: #include <string.h> /* for strerror */ #include <errno.h> #include ...

  10. OS快速开发必备

    github:https://github.com/koknine (终于改成以前的了) 当前移动互联网行业太火爆,移动端的需求日益增长,很多开发人员每天都应对着各种需求,作为一名iOS开发人员,对于 ...