Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
the-maze-iii.cpp 2.40 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2017-03-04 22:41 +08:00 . Update the-maze-iii.cpp
// Time: O(max(r, c) * wlogw)
// Space: O(w^2)
class Solution {
public:
string findShortestWay(vector<vector<int>>& maze, vector<int>& ball, vector<int>& hole) {
static const unordered_map<string, vector<int>> dirs = {{"u", {-1, 0}}, {"r", {0, 1}},
{"l", {0, -1}}, {"d", {1, 0}}};
priority_queue<node, vector<node>, greater<node>> heap;
unordered_set<int> visited;
heap.emplace(0, make_pair("", ball));
while (!heap.empty()) {
int dist = 0;
string path;
vector<int> node;
tie(dist, lvalue(tie(path, node))) = heap.top();
heap.pop();
if (visited.count(hash(maze, node))) {
continue;
}
if (node[0] == hole[0] &&
node[1] == hole[1]) {
return path;
}
visited.emplace(hash(maze, node));
for (const auto& kvp : dirs) {
int neighbor_dist = 0;
string dir;
vector<int> neighbor;
tie(neighbor_dist, lvalue(tie(dir, neighbor))) = findNeighbor(maze, hole, node, kvp);
heap.emplace(dist + neighbor_dist, make_pair(path + dir, neighbor));
}
}
return "impossible";
}
private:
using node = pair<int, pair<string, vector<int>>>;
node findNeighbor(const vector<vector<int>>& maze, const vector<int>& hole,
const vector<int>& node, const pair<string, vector<int>>& kvp) {
string dir;
vector<int> vec;
tie(dir, vec) = kvp;
vector<int> cur_node = node;
int dist = 0;
while (0 <= cur_node[0] + vec[0] && cur_node[0] + vec[0] < maze.size() &&
0 <= cur_node[1] + vec[1] && cur_node[1] + vec[1] < maze[0].size() &&
!maze[cur_node[0] + vec[0]][cur_node[1] + vec[1]]) {
cur_node[0] += vec[0];
cur_node[1] += vec[1];
++dist;
if (cur_node[0] == hole[0] &&
cur_node[1] == hole[1]) {
break;
}
}
return {dist, {dir, cur_node}};
}
int hash(const vector<vector<int>>& maze, const vector<int>& node) {
return node[0] * maze[0].size() + node[1];
}
template <class T>
constexpr T &lvalue(T &&v) {
return v;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助