HDU3085 Nightmare Ⅱ (双向BFS)
联赛前该练什么?DP,树型,状压当然是爆搜啦
双向BFS就是两个普通BFS通过一拼接函数联系,多多判断啦
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define ON_DEBUGG
#ifdef ON_DEBUGG
#define D_e_Line printf("\n-----------\n")
#define D_e(x) std::cout << (#x) << " : " <<x << "\n"
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define Pause() system("pause")
#include <ctime>
#define TIME() fprintf(stderr, "\nTIME : %.3lfms\n", clock() * 1000.0 / CLOCKS_PER_SEC)
#else
#define D_e_Line ;
#define D_e(x) ;
#define FileOpen() ;
#define FilSave ;
#define Pause() ;
#define TIME() ;
#endif
struct ios {
template<typename ATP> ios& operator >> (ATP &x) {
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x *= f;
return *this;
}
}io;
using namespace std;
template<typename ATP> inline ATP Min(ATP a, ATP b) {
return a < b ? a : b;
}
template<typename ATP> inline ATP Max(ATP a, ATP b) {
return a > b ? a : b;
}
#include <queue>
const int N = 1007;
int n, m;
struct Nod {
int x, y;
};
queue<Nod> q[2];
int step, mx, my, gx, gy, zx[5], zy[5], vis[2][N][N];
int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};
char mp[N][N];
inline bool Check(int x, int y) {
return (abs(x - zx[1]) + abs(y - zy[1]) > 2 * step) && (abs(x - zx[2]) + abs(y - zy[2]) > 2 * step);
}
inline bool Valid(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= m && mp[x][y] != 'X';
}
inline bool BFS(int t) { // t = 1 -> man, t = 0 -> woman
int siz = q[t].size();
while(siz--){
int x = q[t].front().x, y = q[t].front().y;
q[t].pop();
if(!Check(x, y)) continue;
R(k,0,3){
int fx = x + dx[k], fy = y + dy[k];
if(!Valid(fx, fy)) continue;
if(vis[t][fx][fy]) continue;
if(!Check(fx, fy)) continue;
vis[t][fx][fy] = true;
if(vis[0][fx][fy] && vis[1][fx][fy]) return true;
q[t].push((Nod){ fx, fy});
}
}
return false;
}
inline int Solve() {
q[0].push((Nod){ mx, my});
q[1].push((Nod){ gx, gy});
step = 0;
while(!q[0].empty() || !q[1].empty()){
++step;
R(i,1,3){
if(BFS(0)) return step; // man move three times
}
if(BFS(1)) return step; // woman move
}
return -1;
}
int main() {
//FileOpen();
int Tasks;
io >> Tasks;
while(Tasks--){
io >> n >> m;
R(i,1,n){
scanf("%s", mp[i] + 1);
}
int nmIdx = 0; // night mare
while(!q[0].empty()) q[0].pop();
while(!q[1].empty()) q[1].pop();
Fill(vis, 0);
R(i,1,n){
R(j,1,m){
if(mp[i][j] == 'M'){
mx = i, my = j;
vis[0][i][j] = true;
}
else if(mp[i][j] == 'G'){
gx = i, gy = j;
vis[1][i][j] = true;
}
else if(mp[i][j] == 'Z'){
zx[++nmIdx] = i, zy[nmIdx] = j;
}
}
}
printf("%d\n", Solve());
}
return 0;
}
HDU3085 Nightmare Ⅱ (双向BFS)的更多相关文章
- HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU3085(双向BFS+曼哈顿距离)题解
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- HDU3085(KB2-G 双向bfs)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Nightmare Ⅱ(双向BFS)
Problem Description Last night, little erriyue had a horrible nightmare. He dreamed that he and his ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- 【HDU3085】nightmare2 双向BFS
对于搜索树分支很多且有明确起点和终点的情况时,可以采用双向搜索来减小搜索树的大小. 对于双向BFS来说,与单向最大的不同是双向BFS需要按层扩展,表示可能到达的区域.而单向BFS则是按照单个节点进行扩 ...
- HDU 3085 Nightmare Ⅱ(双向BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...
随机推荐
- CentOS 7.9 安装 zookeeper-3.7.0
1. 下载 Zookeeper Zookeeper 官网 使用 wget 下载: wget https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper ...
- C++编码规范(本人自定义)
C++编码规范 1.变量名用camelCase命名法(即lowerCamelCase,小驼峰拼写法)命名. 即小写字母开头,如果变量名是复合词,第二个单词的首字母大写. 举例: int digitsC ...
- 搭建自己的个人web项目指南 ---(一)服务器购买与基础配置 | windows连接到自己的云服务器
(一)服务器购买与基础配置 | windows连接到自己的云服务器 一.服务器选购指南 厂商选择 目前市面上提供服务器租用的厂商很多,比较知名的还是阿里云和腾讯云,两家的稳定性都非常不错,小伙伴们可以 ...
- SpringCloud 客户端负载均衡:Ribbon
目录 Ribbon 介绍 开启客户端负载均衡,简化 RestTemplate 调用 负载均衡策略 Ribbon 介绍 Ribbon 是 Netflix 提供的一个基于 Http 和 TCP 的客户端负 ...
- 6月6日,HTTP/3 正式发布了!
经过了多年的努力,在 6 月 6 号,IETF (互联网工程任务小组) 正式发布了 HTTP/3 的 RFC, 这是超文本传输协议(HTTP)的第三个主要版本,完整的 RFC 超过了 20000 字, ...
- vue 使用npm install安装依赖失败 【问题分析与解决】
1 进入项目根目录,先通过 npm install 命令安装项目所需依赖,再通过 vue ui 命令打开 Vue Cli 提供的图形化界面,选择项目所在文件夹将项目导入. 出现问题 npm insta ...
- word-制作三线表
找一个表格或插入一个表格, 找到 [设计] [新建表格样式] [将格式应用于: 整个表格] 点击"框线设置"按钮,在弹出的下拉菜单中分别选择 [上框线] 和 [下框线],然后分别设 ...
- Java递归实现评论多级回复
最近工作需要做一个评论功能,除了展示评论之外,还需要展示评论回复,评论的回复的回复,这里就用到了递归实现评论的多级回复. 评论实体 数据库存储字段: id 评论id.parent_id 回复评论id. ...
- python写个前端,这不是轻轻松松~
前端除了用js++css+html,还有没有其它办法?其实python也可以 1. 安装与基本流程 Python学习交流Q群:660193417### 安装 PyWebIO 和其他的第三方库一样使用p ...
- npm运行出错npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree
npm运行出错npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree 场景复现: 使用vue CLI创建项 ...