博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZOJ 1649 Rescue(有敌人迷宫BFS)
阅读量:7078 次
发布时间:2019-06-28

本文共 1641 字,大约阅读时间需要 5 分钟。

题意 求迷宫中从a的位置到r的位置须要的最少时间  经过'.'方格须要1s  经过‘x’方格须要两秒  '#'表示墙

因为有1s和2s两种情况  须要在基础迷宫bfs上加些推断

令到达每一个点的时间初始为无穷大  当从一个点到达该点用的时间比他本来的时间小时  更新这个点的时间并将这个点入队  扫描全然图就得到答案咯

#include
#include
#include
using namespace std;const int N = 205;char mat[N][N];int time[N][N], sx, sy;int dx[4] = {0, 0, -1, 1};int dy[4] = { -1, 1, 0, 0};struct grid{ int x, y; grid(int xx = 0, int yy = 0): x(xx), y(yy) {}};void bfs(){ memset(time, 0x3f, sizeof(time)); time[sx][sy] = 0; queue
g; g.push(grid(sx, sy)); while(!g.empty()) { grid cur = g.front(); g.pop(); int cx = cur.x, cy = cur.y, ct = time[cx][cy]; for(int i = 0; i < 4; ++i) { int nx = cx + dx[i], ny = cy + dy[i]; if(mat[nx][ny] && mat[nx][ny] != '#') { int tt = ct + 1; if(mat[cx][cy] == 'x') ++tt; if(tt < time[nx][ny]) { time[nx][ny] = tt; g.push(grid(nx, ny)); } } } }}int main(){ int n, m, ex, ey; while (~scanf("%d%d", &n, &m)) { memset(mat, 0, sizeof(mat)); for(int i = 1; i <= n; ++i) scanf("%s", mat[i] + 1); for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) if(mat[i][j] == 'a') sx = i, sy = j; else if(mat[i][j] == 'r') ex = i, ey = j; bfs(); if(time[ex][ey] != time[0][0]) printf("%d\n", time[ex][ey]); else printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0;}

转载地址:http://wycml.baihongyu.com/

你可能感兴趣的文章
HTTP协议:看个新闻原来这么麻烦
查看>>
服务平台化,知乎 HBase 实践
查看>>
2015年度最流行PHP框架调查结果出炉,Laravel居首
查看>>
WebAssembly得到了所有浏览器的支持
查看>>
Naresh Jain:合作的阴暗面
查看>>
为什么已有Elasticsearch,我们还要重造实时分析引擎AresDB?
查看>>
阿里巴巴收购以色列VR公司,大厂死磕VR为哪般?
查看>>
使用Xamarin实现跨平台移动应用开发
查看>>
webpack-dev-server 热替换配置详解
查看>>
随机森林算法4种实现方法对比测试:DolphinDB速度最快,XGBoost表现最差
查看>>
架构设计复杂度的6个来源
查看>>
如何成功地在亚洲植入敏捷和DevOps
查看>>
银行建中台跟阿里建中台有什么不同?
查看>>
实现AGI还要多久?Hinton与AlphaGo之父这样回答
查看>>
Atlassian的Stash数据中心为Git提供了高可用性及可伸缩性
查看>>
Adaptive Execution让Spark SQL更高效更好用
查看>>
Swift 烧脑体操(五)- Monad
查看>>
中国在两年内赶超美国AI?李开复:不一定
查看>>
OpsRamp推出AIOps推理引擎
查看>>
C#未来新特性:静态委托和函数指针
查看>>