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步,鬼可以两步且可以通过墙.问男孩女孩是否可以在鬼抓住他们之前会合? 注意:每秒开始鬼先移动,然后两人开始移动. 思路:以男孩和 ...
随机推荐
- jzoj5928
tj:題解裡公式是錯的 我們可以考慮每一個節點[a,a+2^b-1]對答案的貢獻 則當這個節點是左兒子時,貢獻為2^b 是右兒子時,貢獻為2n−a−2b+12^n-a-2^b+12n−a−2b+1 左 ...
- 文件操作(FILE)与常用文件操作函数
文件 1.文件基本概念 C程序把文件分为ASCII文件和二进制文件,ASCII文件又称文本文件,二进制文件和文本文件(也称ASCII码文件)二进制文件中,数值型数据是以二进制形式存储的, 而在文本文件 ...
- JS弹出对话框函数alert(),confirm(),prompt()
1,警告消息框alert() alert 方法有一个参数,即希望对用户显示的文本字符串.该字符串不是 HTML 格式.该消息框提供了一个“确定”按钮让用户关闭该消息框,并且该消息框是模式对话框,也就是 ...
- 分布式ehcache缓存
今天在这里了记录一下学习ehcache分布式集群的过程. ehcache的三种最为常用集群方式,分别是 RMI.JGroups 以及 EhCache Server . 这里主要讲一下rmi方式. 1. ...
- mybatis源码追踪1——Mapper方法用法解析
Mapper中的方法执行时会构造为org.apache.ibatis.binding.MapperMethod$MethodSignature对象,从该类源码中可以了解如何使用Mapper方法. [支 ...
- 用xshell ssh连接测试服务器时候出的问题
问题还原:用ssh连接测试服务器 给我结结实实报了个错 FBIwarning: ------------------------------------------------------------ ...
- JavaScript函数学习总结(一)---函数定义
博客原文地址:Claiyre的个人博客 如需转载,请在文章开头注明原文地址 在许多传统的OO语言中,对象可以包含数据,还可拥有方法,也就是属于该对象的函数.但在JavaScript中,函数也被认为是一 ...
- 手机端API接口验证及参数签名验证
问题背景: 后端服务对手机APP端开放API,没有基本的校验就是裸奔,别人抓取接口后容易恶意请求,不要求严格的做的安全,但是简单的基础安全屏障是要建立的,再配合HTTPS使用,这样使后端服务尽可能的安 ...
- pods报错修复方法
### Error ``` RuntimeError - [!] Xcodeproj doesn't know about the following attributes {"inputF ...
- docker下 klee第一个测试
被测试的简单函数源文件位于 /klee_src/examples/get_sign 目录下 该源代码分为三个部分 第一个部分为被测试的函数 int get_sign(int x) { if (x = ...