HDU3085(KB2-G 双向bfs)
Nightmare Ⅱ
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2196 Accepted Submission(s): 572
Problem Description
You may suppose that little erriyue and his girl friend can move in 4 directions. In each second, little erriyue can move 3 steps and his girl friend can move 1 step. The ghosts are evil, every second they will divide into several parts to occupy the grids within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they will die.
Note: the new ghosts also can devide as the original ghost.
Input
Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800)
The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’ denotes an empty place, all can walk on.
‘X’ denotes a wall, only people can’t walk on.
‘M’ denotes little erriyue
‘G’ denotes the girl friend.
‘Z’ denotes the ghosts.
It is guaranteed that will contain exactly one letter M, one letter G and two letters Z.
Output
Sample Input
Sample Output
被读入卡了超时,按行读快,一个一个读超时
//2017-03-08
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue> using namespace std; struct node
{
int x, y;
void setNode(int x, int y)
{
this->x = x;
this->y = y;
}
};
int n, m, zx[], zy[], ans, TIME;
char grid[][];
bool vis[][][], ok;
int dx[] = {, , , -};
int dy[] = {, -, , };
queue<node> q[]; bool judge(int x, int y)
{
if(grid[x][y] == 'X')return false;
for(int i = ; i < ; i++)
if((abs(x-zx[i])+abs(y-zy[i]))<=*TIME)
return false;
return true;
} bool bfs(int id)
{
int x, y, nx, ny, s;
node tmp;
s = q[id].size();
while(s--)
{
x = q[id].front().x;
y = q[id].front().y;
q[id].pop();
if(!judge(x, y))continue;
for(int i = ; i < ; i++)
{
nx = x + dx[i];
ny = y + dy[i];
if(nx>=&&nx<n&&ny>=&&ny<m&&judge(nx, ny)&&!vis[id][nx][ny])
{
if(vis[id^][nx][ny]){
ok = true;
return true;
}
vis[id][nx][ny] = ;
tmp.setNode(nx, ny);
q[id].push(tmp);
}
}
}
return false;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
node tmp;
while(!q[].empty())q[].pop();
while(!q[].empty())q[].pop();
memset(vis, , sizeof(vis));
scanf("%d%d", &n, &m);
getchar();
for(int i = ; i < n; i++)//被读入卡了超时,按行读快,一个一个读超时
scanf("%s", grid[i]);
int cnt = ;
for(int i = ; i < n; i++)
{
for(int j = ; j < m; j++)
{
if(grid[i][j] == 'Z'){
zx[cnt] = i;
zy[cnt] = j;
cnt++;
}
if(grid[i][j] == 'M'){
tmp.setNode(i, j);
vis[][i][j] = ;
q[].push(tmp);
}
if(grid[i][j] == 'G'){
tmp.setNode(i, j);
vis[][i][j] = ;
q[].push(tmp);
}
}
}
ok = false;
TIME = ;
while(!q[].empty() || !q[].empty())
{
TIME++;
if(bfs())break;
if(bfs())break;
if(bfs())break;
if(bfs())break;
}
if(ok)printf("%d\n", TIME);
else printf("-1\n");
}
return ;
}
HDU3085(KB2-G 双向bfs)的更多相关文章
- 【HDU3085】nightmare2 双向BFS
对于搜索树分支很多且有明确起点和终点的情况时,可以采用双向搜索来减小搜索树的大小. 对于双向BFS来说,与单向最大的不同是双向BFS需要按层扩展,表示可能到达的区域.而单向BFS则是按照单个节点进行扩 ...
- HDU3085(双向BFS+曼哈顿距离)题解
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...
- HDU3085 Nightmare Ⅱ (双向BFS)
联赛前该练什么?DP,树型,状压当然是爆搜啦 双向BFS就是两个普通BFS通过一拼接函数联系,多多判断啦 #include <iostream> #include <cstdio&g ...
- POJ1915Knight Moves(单向BFS + 双向BFS)
题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- BFS、双向BFS和A*
BFS.双向BFS和A* Table of Contents 1. BFS 2. 双向BFS 3. A*算法 光说不练是无用的.我们从广为人知的POJ 2243这道题谈起:题目大意:给定一个起点和一个 ...
- HDU - 3085 双向BFS + 技巧处理 [kuangbin带你飞]专题二
题意:有两只鬼,一个男孩女孩被困在迷宫中,男孩每秒可以走三步,女孩只能1步,鬼可以两步且可以通过墙.问男孩女孩是否可以在鬼抓住他们之前会合? 注意:每秒开始鬼先移动,然后两人开始移动. 思路:以男孩和 ...
随机推荐
- SharedFile System Master Slave(共享文件系统)做ActiveMQ集群
WINDOWS环境下:http://www.apache.org/dyn/closer.cgi?path=/activemq/apache-activemq/5.9.0/apache-activemq ...
- Sublime Text 格式化JSON-pretty json
1.安装install package 按control + `,打开命令输入框 输入一下命令: import urllib2,os; pf='Package Control.sublime-pack ...
- grub覆盖mbr引导系统
grub覆盖mbr引导系统 0.个人PC,WIN 7 + Kali,easybcd 不起作用,需要制作 kali 安装盘 PS:推荐使用 universal usb installer 制作. 方案一 ...
- 非对齐访问(unaligned accesses)
从CPU角度看内存访问对齐 结构体成员非对齐访问所带来的思考 ARM体系中存储系统非对齐的存储访问操作 什么是cache line? cache line就是处理器从RAM load/store数据到 ...
- Jmeter 多台机器产生负载及问题解决方法
JMeter 使用多台机器产生负载的操作步骤如下: 关于linux环境运行jmeter,分布式测试 见 http://www.51testing.com/html/55/383255-847895.h ...
- 如何在for循环中使用多线程
import java.util.concurrent.Executor;import java.util.concurrent.Executors; public class Test {priva ...
- day 47 Django 4的简单应用 创建简单的图书管理 (单表的增删改查)
前情提要 Django 已经学了大半.. 很多东西已经能够使用在生产环境当中 一:模糊查询 二:单表删除 三:单表修改 四:图书管理 图书管理操作 视图结构 A:路由层 A :配置路由文件 参数解 ...
- 【bzoj4240】 有趣的家庭菜园 树状数组
这一题最终要构造的序列显然是一个单峰序列 首先有一个结论:一个序列通过交换相邻的元素,进行排序,最少的交换次数为该序列的逆序对个数 (该结论很久之前打表意外发现的,没想到用上了.....) 考虑如何构 ...
- POJ 2242
#include <iostream> #include <cmath> #include <iomanip> using namespace std; #defi ...
- django 高级
1.使用form: django的form提供了统一的结构化的后台验证机制,错误信息,也容易展现在前台界面上.由于python的面向对象,使得编写html也能够代码复用. a.多个field 综合验证 ...