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步,鬼可以两步且可以通过墙.问男孩女孩是否可以在鬼抓住他们之前会合? 注意:每秒开始鬼先移动,然后两人开始移动. 思路:以男孩和 ...
随机推荐
- python 将json格式的数据写入csv格式的文件中
# coding=utf-8 import json import csv # 重新进行配置读写数据时的默认编码 import sys reload(sys) sys.setdefaultencodi ...
- ansj分词史上最详细教程
最近的项目需要使用到分词技术.本着不重复造轮子的原则,使用了ansj_seg来进行分词.本文结合博主使用经过,教大家用最快的速度上手使用ansj分词. 1.给ansj来个硬广 项目的github地址: ...
- Elasticsearch java API (23)查询 DSL Geo查询
地理查询编辑 Elasticsearch支持两种类型的地理数据: geo_point纬度/经度对字段的支持,和 geo_shape领域,支持点.线.圆.多边形.多等. 这组查询: geo_shape ...
- 如何修改静态IP地址和动态IP地址
打开控制面板,一般在电脑的菜单栏能找到,win8和win10可以使用快捷键(win键+X键),找不到的朋友可以搜索一下. 进入到网络和共享中心,点击更改适配器设置. 这里显示的是电脑所以的网络 ...
- day 49 数据分析, 数据聚合 F 查询 Q 查询
6.聚合查询和分组查询 1.聚合查询aggregate 我们先通过一个例子来感受一下吧. 1 2 3 # 计算所有图书的平均价格 books = models.Book.objects.aggrega ...
- JIRA Rest JAVA Client API实现问题管理及自定义字段(原创)
JIRA是一个缺陷跟踪管理系统,被广泛应用于缺陷跟踪.客户服务.需求收集.流程审批.任务跟踪.项目跟踪和敏捷管理等工作领域,当我们需要把第三方业务系统集成进来时,可以调用他的API. JIRA本身的A ...
- python3模块: uuid
一. 简介 UUID是128位的全局唯一标识符,通常由32字节的字母串表示.它可以保证时间和空间的唯一性,也称为GUID. 全称为:UUID--Universally Unique IDentifie ...
- Python封装
什么是封装 在程序设计中,封装(Encapsulation)是对具体对象的一种抽象,即将某些部分隐藏起来,在程序外部看不到,其 含义是其他程序无法调用. 要了解封装,离不开“私有化”,就是将类或者是函 ...
- CS231n学习笔记-图像分类笔记(下篇)
原文地址:智能单元 K-Nearest Neighbor分类器 大家可能注意到了,为什么只用最相似的一张图片的标签来作为测试图像的标签呢?这不是很奇怪吗!是的,使用K-Nearest Neighbor ...
- start with connect by prior
start with connect by prior的使用: select … from tablename start with 条件1 connect by 条件2 where 条件3; sta ...