problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::escape_a_large_maze::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static bool isEscapePossible (vector< vector< int > > &blocked, vector< int > &source, vector< int > &target)
 
static unsigned int search (unordered_set< point, point_hash > &block_set, point *source, point *target, unsigned int limit)
 在迷宫中以source为起点搜索 更多...
 

详细描述

在文件 leetcode.h385 行定义.

成员函数说明

◆ isEscapePossible()

bool leetcode::escape_a_large_maze::Solution::isEscapePossible ( vector< vector< int > > &  blocked,
vector< int > &  source,
vector< int > &  target 
)
static

在文件 leetcode.cpp850 行定义.

850 {
851 const int limit = blocked.size() * (blocked.size() - 1) / 2;
852 if(blocked.empty()) {
853 return true;
854 }
855 auto *p_source = new point(source[0], source[1], 0, nullptr);
856 auto *p_target = new point(target[0], target[1], 0, nullptr);
857 auto blocked_set_source = unordered_set<point, point_hash>();
858 auto blocked_set_target = unordered_set<point, point_hash>();
859 for(auto block: blocked) {
860 blocked_set_source.insert(point(block[0], block[1], 0, nullptr));
861 blocked_set_target.insert(point(block[0], block[1], 0, nullptr));
862 }
863 const unsigned int source_status = search(blocked_set_source, p_source, p_target, limit);
864 if(source_status == 0) {
865 return false;
866 }
867 if(source_status == 1) {
868 return true;
869 }
870 const unsigned int target_status = search(blocked_set_target, p_target, p_source, limit);
871 delete p_source;
872 delete p_target;
873 return target_status != 0;
874 }
static unsigned int search(unordered_set< point, point_hash > &block_set, point *source, point *target, unsigned int limit)
在迷宫中以source为起点搜索
Definition: leetcode.cpp:876

引用了 search().

◆ search()

unsigned int leetcode::escape_a_large_maze::Solution::search ( unordered_set< point, point_hash > &  block_set,
point source,
point target,
unsigned int  limit 
)
static

在迷宫中以source为起点搜索

参数
block_set阻塞点集
source起点
target终点
limit搜索步数上限
返回
0表示无法抵达 1表示可以抵达 2表示达到搜索步数上限

在文件 leetcode.cpp876 行定义.

876 {
877 auto pq = priority_queue<point>();
878 pq.push(point(source->x, source->y, 0, target));
879 int count = 0;
880 while(!pq.empty()) {
881 if(count > limit || pq.size() > limit) {
882 return 2;//不包围
883 }
884 count++;
885 const auto p = pq.top();
886 pq.pop();
887 point nexts[] = {point(p.x + 1, p.y, p.distance + 1, target), point(p.x - 1, p.y, p.distance + 1, target), point(p.x, p.y + 1, p.distance + 1, target), point(p.x, p.y - 1, p.distance + 1, target)};
888 for(auto next: nexts) {
889 if(0 <= next.x && next.x < 1000000 && 0 <= next.y && next.y < 1000000 && !block_set.contains(next)) {
890 if(next.x == target->x && next.y == target->y) {
891 return 1;//连通
892 }
893 pq.push(next);
894 block_set.insert(next);
895 }
896 }
897 }
898 return 0;//不连通
899 }

引用了 leetcode::escape_a_large_maze::point::x , 以及 leetcode::escape_a_large_maze::point::y.

被这些函数引用 isEscapePossible().


该类的文档由以下文件生成: