Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cat-and-mouse.cpp 2.02 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2018-09-30 21:56 +08:00 . Create cat-and-mouse.cpp
// Time: O(n^3)
// Space: O(n^2)
class Solution {
private:
template <typename A, typename B, typename C>
struct TupleHash {
size_t operator()(const tuple<A, B, C>& p) const {
size_t seed = 0;
A a; B b; C c;
tie(a, b, c) = p;
seed ^= std::hash<A>{}(a) + 0x9e3779b9 + (seed<<6) + (seed>>2);
seed ^= std::hash<B>{}(b) + 0x9e3779b9 + (seed<<6) + (seed>>2);
seed ^= std::hash<C>{}(c) + 0x9e3779b9 + (seed<<6) + (seed>>2);
return seed;
}
};
using Lookup = unordered_map<tuple<int, int, bool>, int, TupleHash<int, int, bool>>;
public:
int catMouseGame(vector<vector<int>>& graph) {
Lookup lookup;
return move(graph, &lookup, MOUSE_START, CAT_START, true);
}
private:
enum Location {HOLE, MOUSE_START, CAT_START};
enum Result {DRAW, MOUSE, CAT};
int move(const vector<vector<int>>& graph, Lookup *lookup, int i, int other_i, bool is_mouse_turn) {
const auto& key = make_tuple(i, other_i, is_mouse_turn);
if (lookup->count(key)) {
return (*lookup)[key];
}
(*lookup)[key] = DRAW;
int skip, target, win, lose;
if (is_mouse_turn) {
tie(skip, target, win, lose) = make_tuple(other_i, HOLE, MOUSE, CAT);
} else {
tie(skip, target, win, lose) = make_tuple(HOLE, other_i, CAT, MOUSE);
}
for (const auto& nei : graph[i]) {
if (nei == target) {
(*lookup)[key] = win;
return win;
}
}
int result = lose;
for (const auto& nei : graph[i]) {
if (nei == skip) {
continue;
}
auto tmp = move(graph, lookup, other_i, nei, !is_mouse_turn);
if (tmp == win) {
result = win;
break;
}
if (tmp == DRAW) {
result = DRAW;
}
}
(*lookup)[key] = result;
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助