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

lc1036-逃离大迷宫

题目描述

  • 原题

示例

输入:blocked = [[0,1],[1,0]], source = [0,0], target = [0,2]
输出:false
输入:blocked = [], source = [0,0], target = [999999,999999]
输出:true

题解

  • 思路:bfs
  • golang 写得不好,先放 cpp 吧
class Solution {
public:unordered_set<string> s;int n = 1e6;int m;int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};bool isEscapePossible(vector<vector<int>>& blocked, vector<int>& source, vector<int>& target) {m = blocked.size();for (auto &b: blocked) s.insert(get(b));return bfs(source, target) && bfs(target, source);}string get(vector<int> p) {return to_string(p[0]) + ' ' + to_string(p[1]);}bool bfs(vector<int> &source, vector<int> &target) {auto st = s;queue<vector<int>> q;q.push(source);st.insert(get(source));int cnt = 1;while (q.size()) {auto t = q.front(); q.pop();for (int d = 0; d < 4; d ++ ) {int x = t[0] + dx[d], y = t[1] + dy[d];if (0 <= x && x < n && 0 <= y && y < n &&!st.count(get({x, y}))) {cnt ++ ;if (cnt * 2 > m * (m - 1)) return true;if (target[0] == x && target[1] == y) return true;q.push({x, y});st.insert(get({x, y}));}}}return false;}
};
http://www.hskmm.com/?act=detail&tid=17481

相关文章:

  • 9.25学习笔记
  • 新学期每日总结(第4天)
  • VSCode 升级 C++支持版本
  • 第四天
  • 25.9.25
  • 在electron-vite使用ShadCN
  • 每日博客(补)
  • 如何使用极限网关实现 Elasticsearch 集群迁移至 Easysearch
  • 文档抽取技术:实现金融保险业务流程自动化
  • 算法作业
  • C#学习3
  • 9-23
  • 9-26
  • Ubuntu Uninstall App
  • 20250925
  • 题解:P2662 牛场围栏
  • day11 课程(学员管理系统案例)
  • c语言初步学习
  • jmeter函数
  • 【网络编程】UDP 编程实战:从套接字到聊天室多场景计划构建
  • AC自动机在线版本(alert命中报警)
  • week1 homework
  • Windows 10 C盘占用释放 - tfel
  • 运算符
  • 科学计算方法--矩阵分析记录
  • window.addEventListener(message,()={})中的回调函数无故被一直触发的问题 - broky
  • python+pillow+Image实现图片压缩到指定大小
  • 页面卡顿问题分析与解决方案总结复盘
  • Say 题选记(9.21 - 9.27)
  • 9月25日