双向dfs 男孩女孩每到一个地方标记最小时间 第一次来到对方标记的地方的时间就是答案

代码

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>using namespace std;
int n,m;
char s[805][805];
int gh[3][2];
int dis[2][805][805];
queue<pair<int,int> >bq,gq;
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
bool check(int x,int y,int t)
{return x>=1&&x<=n&&y>=1&&y<=m&&s[x][y]=='.'&&abs(gh[1][0]-x)+abs(gh[1][1]-y)>2*t&&abs(gh[2][0]-x)+abs(gh[2][1]-y)>2*t;
}
int bfs()
{int t=0;while(!bq.empty()&&!gq.empty()){++t;for(int i=1;i<=3;++i)for(int j=1,len=bq.size();j<=len;++j){int x=bq.front().first,y=bq.front().second;bq.pop();if(!check(x,y,t))continue;for(int k=0;k<4;++k){int tx=x+dx[k],ty=y+dy[k];if(!check(tx,ty,t)||dis[0][tx][ty]!=-1)continue;dis[0][tx][ty]=y;if(dis[1][tx][ty]!=-1)return t;bq.push(make_pair(tx,ty));}}for(int i=1,len=gq.size();i<=len;++i){ int x=gq.front().first,y=gq.front().second;gq.pop();if(!check(x,y,t))continue;for(int j=0;j<4;++j){int tx=x+dx[j],ty=y+dy[j];if(!check(tx,ty,t)||dis[1][tx][ty]!=-1)continue;dis[1][tx][ty]=t;if(dis[0][tx][ty]!=-1)return t;gq.push(make_pair(tx,ty));}}}return -1;
}
int main()
{int T;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);int cnt=0;int bx,by,gx,gy;for(int i=1;i<=n;++i){scanf("%s",s[i]+1);for(int j=1;j<=m;++j){if(s[i][j]=='M')bx=i,by=j,s[i][j]='.';else if(s[i][j]=='G')gx=i,gy=j,s[i][j]='.';else if(s[i][j]=='Z'){++cnt;gh[cnt][0]=i,gh[cnt][1]=j;}}}memset(dis,-1,sizeof(dis));while(!bq.empty())bq.pop();while(!gq.empty())gq.pop();bq.push(make_pair(bx,by));gq.push(make_pair(gx,gy));dis[0][bx][by]=dis[1][gx][gy]=0;printf("%d\n",bfs());}return 0;
}

hdoj3085 / acwing177 Nightmare相关推荐

  1. HDU Problem - 3085 Nightmare Ⅱ(双向BFS)

    题目链接 Problem Description Last night, little erriyue had a horrible nightmare. He dreamed that he and ...

  2. node nightmare 网页自动化测试 sample

    安装nightmare 编写1.js 运行 node 1.js var Nightmare = require('nightmare'); var nightmare = Nightmare({ sh ...

  3. Nightmare Ⅱ(BFS)

    题目连接: Nightmare Ⅱ 题目: Last night, little erriyue had a horrible nightmare. He dreamed that he and hi ...

  4. G - Nightmare Ⅱ (双向BFS)

    题目链接:SDTBU-ACM集训队暑期集训---第一讲 - Virtual Judge Last night, little erriyue had a horrible nightmare. He ...

  5. 专题训练二 搜索进阶 HDU - 3085 Nightmare Ⅱ (双向BFS + 曼哈顿距离)

    HDU - 3085 Nightmare Ⅱ (双向BFS + 曼哈顿距离) Problem Description Last night, little erriyue had a horrible ...

  6. HDU 3085 Nightmare Ⅱ【BFS +曼哈顿距离+综合性较强】

    Nightmare Ⅱ Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total S ...

  7. HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...

  8. POJ1984 Navigation Nightmare —— 种类并查集

    题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K T ...

  9. POJ 1984 Navigation Nightmare 【经典带权并查集】

    任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K To ...

  10. HDU3085 Nightmare Ⅱ

    题目: Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were ...

最新文章

  1. torch yolov3梯度发散
  2. 获取keras中间层输出、模型保存与加载
  3. python123编写函数求和_Python基础之函数
  4. 趣图:你能Get到笑点么?
  5. Python-Pandas之两个Dataframe的差异比较
  6. Django(四)数据库
  7. 苹果电脑 默认安装jdk位置_收藏起来你一定用的上!JDK的安装和配置
  8. python定义初始化函数_Python 语言定义的Class的初始化函数的函数名是( )
  9. python大神的程序_6年Python大神总结10个开发技巧,80%的人都不会
  10. java执行查询postgresql得到中文乱码_PostgreSQL 初探
  11. 【leetcode】538. Convert BST to Greater Tree
  12. Mysql内容查找替换
  13. React项目中使用Dplayer播放FLV格式视频教程(不支持ios)
  14. tp6 时间戳查询 自动转换
  15. 【渝粤教育】国家开放大学2019年春季 0553-22T色彩 参考试题
  16. 2017年美团Java程序员开发,看我如何拿到offer
  17. 港科夜闻|罗康锦教授获委任为香港科大工学院院长
  18. 数据挖掘——认识数据
  19. HTTP 2.0 中文版
  20. 操作系统第九次部分作业题答案

热门文章

  1. csgo fps不稳定和服务器,CSGO fps不稳定,求FPS 优化设置
  2. 微软word 2019取消交叉引用
  3. SQLAlchemy 字段、要点
  4. Latex排版学习笔记(4)——Latex插入项目符号和编号
  5. Codeforces Round #540 (Div. 3)--B. Tanya and Candies(前缀和的运用与变化)
  6. 求方程ax^2+bx+c=0的根,用3个函数分别求当b^2-4ac大于0、等于0和小于0时的根,并输出结果。 从主函数输入a, b, c的值。
  7. 火狐受信任站点设置_火狐浏览器如何添加信任站点?添加信任站点的方法说明...
  8. 大屏可视化之适配和布局
  9. 贯入用计算机怎样换算,标准贯入试验应用及其杆径换算研究
  10. 标志位寄存器与CF、OF标志位的区分