当前位置: 首页 > news >正文

【图论】CF——B. Chamber of Secrets (0-1BFS)

链接:https://codeforces.com/problemset/problem/173/B

题目:

思路:

初识01BFS

什么是 01 BFS 呢?通常的 BFS 为一步权值为 1,而某些题需要的不是走到步数,而是某种操作数,如花费一个操作可以走 k 步,而不花费只能走 1 步,通常我们使用双端队列可插队的性质来进行代码的编写,具体的对于不花费,那么就插入到前面,而对于花费则插入到最后

本题中操作为 “四射”,所以按照上面描述很快能写出来代码

代码:

#include <iostream>
#include <algorithm>
#include<cstring>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <memory>
#include <complex>
using namespace std;
#define int long long
#define yes cout << "Yes\n"
#define no cout << "No\n"
pair<int, int> dir[4] = { {-1,0},{0,1},{1,0},{0,-1} };
int use[1005][1005];
int dis[1005][1005][4];
deque<int> q;void af(int x,int y,int d,int ans)
{if (ans < dis[y][x][d]){dis[y][x][d] = ans;q.push_front(d);q.push_front(y);q.push_front(x);}
}
void ab(int x, int y, int d, int ans)
{if (ans < dis[y][x][d]){dis[y][x][d] = ans;q.push_back(x);q.push_back(y);q.push_back(d);}
}
void solve()
{int n, m;cin >> n >> m;vector<string> mp(n);for (int i = 0; i < n; i++){cin >> mp[i];}for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){for (int k = 0; k < 4; k++){dis[i][j][k] = 1e18;}}}af(m - 1, n - 1, 0, 0);while (!q.empty()){int x = q[0];int y = q[1];int d = q[2];q.pop_front();q.pop_front();q.pop_front();int ans = dis[y][x][d];int nx = x + dir[d].first;int ny = y + dir[d].second;if (nx >= 0 && nx < m && ny >=0 && ny < n){af(nx, ny, d, ans);}if (mp[y][x] == '#'){for (int i = 0; i < 4; i++){if (i != d)ab(x, y, i, ans + 1);}}}if (dis[0][0][0] != 1e18)cout << dis[0][0][0] << endl;elsecout << "-1\n";
}signed main()
{ios::sync_with_stdio(false);cin.tie(nullptr);int t = 1;while (t--){solve();}return 0;
}

http://www.lryc.cn/news/595217.html

相关文章:

  • 文本数据分析
  • 本地部署Dify、Docker重装
  • neuronxcc包介绍及示例代码
  • 【Java学习|黑马笔记|Day19】方法引用、异常(try...catch、自定义异常)及其练习
  • seata at使用
  • 深度学习 -- 梯度计算及上下文控制
  • 7月21日总结
  • registry-ui docker搭建私有仓库的一些问题笔记
  • 服务器后台崩溃的原因
  • 使用Langchain调用模型上下文协议 (MCP)服务
  • 【未限制消息消费导致数据库CPU告警问题排查及解决方案】
  • WEB前端登陆页面(复习)
  • 随笔20250721 PostgreSQL实体类生成器
  • Elasticsearch X-Pack安全功能未启用的解决方案
  • OpenEuler 22.03 系统上安装配置gitlab runner
  • 笔试——Day14
  • 【PTA数据结构 | C语言版】求单源最短路的Dijkstra算法
  • 打造自己的 Jar 文件分析工具:类名匹配 + 二进制搜索 + 日志输出全搞定
  • Laravel 后台登录 403 Forbidden 错误深度解决方案-优雅草卓伊凡|泡泡龙
  • PHP实战:从原理到落地,解锁Web开发密码
  • 【HarmonyOS】ArkTS语法详细解析
  • Valgrind Cachegrind 全解析:用缓存效率,换系统流畅!
  • NISP-PTE基础实操——代码审计
  • Near Cache
  • 嵌入式学习-土堆目标检测(1)-day26
  • 低代码平台能否完全取代传统前端开发
  • Apache Ignite Binary Object 调优
  • OpenCV计算机视觉实战(16)——图像分割技术
  • 有关Maven的个人笔记总结
  • 【PTA数据结构 | C语言版】双连通分量