hdu1072(bfs)
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int a[10][10];
int state_time[10][10];
int f[4][2]={0,1,0,-1,1,0,-1,0};
struct state
{
int x,y;
int time;
int count;
};
int main()
{
int t,n,m;
cin>>t;
while(t--)
{
memset(state_time,-1,sizeof(state_time));
memset(a,0,sizeof(a));
cin>>n>>m;
int sx,sy;
int min=99909990;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
cin>>a[i][j];
if(a[i][j]==2)
{sx=i;sy=j;}
// if(a[i][j]==3)
//{ lx=i;ly=j;}
}
state start;
start.x=sx;start.y=sy;start.count=0;start.time=6;
queue<state>q;
q.push(start);
while(!q.empty())
{
state cur=q.front();q.pop();
if(state_time[cur.x][cur.y]>=cur.time)continue;
//cout<<cur.x<<" "<<cur.y<<endl;
if(state_time[cur.x][cur.y]<cur.time)
{state_time[cur.x][cur.y]=cur.time;} if(cur.time==0)continue;
if(a[cur.x][cur.y]==3)
{
if(cur.count<min)min=cur.count;
continue;
}
if(a[cur.x][cur.y]==4)
{
cur.time=6;
}
for(int i=0;i<4;i++)
{
state zijiedian(cur);
zijiedian.count++;
zijiedian.time--;
zijiedian.x+=f[i][0];
zijiedian.y+=f[i][1];
if(a[zijiedian.x][zijiedian.y]==0)continue;
q.push(zijiedian);
}
}
if(min==99909990)cout<<-1<<endl;
else cout<<min<<endl; }
return 0;
}
hdu1072(bfs)的更多相关文章
- HDU-1072 Nightmare (bfs+贪心)
Nightmare Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- 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
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1072/ 题意:逃离迷宫,路中可能有炸弹,总时间是6个单位,在有炸弹的位置,如果到达的时刻时间大于0,则恢复到6时 ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
随机推荐
- NSString 与NSMutableString的区别
NSString 与NSMutableString的区别 Suppose You have a code like this NSString *s = [[NSString alloc] ...
- zabbix server端配置
# wget http://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/2.0.6/zabbix-2.0.6.tar. ...
- vba,自定义公式,农历互转公历,excel ,wps
'vba 模块内容如下 自定义公式 '公历转农历模块 '原创:互联网 '修正: '// 农历数据定义 // '先以 H2B 函数还原成长度为 18 的字符串,其定义如下: '前12个字节代表1-12月 ...
- DAX:New and returning customers
The New and Returning Customers pattern dynamically calculates the number of customers with certain ...
- Git理论知识补充
转自: http://www.cnblogs.com/hnrainll/archive/2012/11/13/2768003.html 对于任何一个文件,在 Git 内都只有三种状态:已提交(comm ...
- how to get many stars on Github?
some key points: 1: make a beautiful README file2: use some GIF (google some tools to convert videos ...
- mkdir与makedirs
mkdir创建的是一级目录 makedirs可以创建多级目录 mkdir -p可以递归创建目录
- Spring Data Redis入门示例:字符串操作(六)
Spring Data Redis对字符串的操作,封装在了ValueOperations和BoundValueOperations中,在集成好了SPD之后,在需要的地方引入: // 注入模板操作实例 ...
- 下载GitHub上的dnSpy源码
一.方法 下载GitHub上项目的方法,目前我知道的有四种: 1.用svn软件checkout下载 2.安装git,然后git命令下载 3.直接下载项目压缩包 4.安装GitHub的客户端,然后下载项 ...
- vue-cli中添加使用less
在vue-cli中构建的项目是可以使用less的,但是查看package.json可以发现,并没有less相关的插件,所以我们需要自行安装. 第一步:安装 npm install less less- ...