Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cat-and-mouse.py 1.30 KB
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 2018-10-12 01:33 +08:00 . remove sensitive question description
# Time: O(n^3)
# Space: O(n^2)
class Solution(object):
def catMouseGame(self, graph):
"""
:type graph: List[List[int]]
:rtype: int
"""
HOLE, MOUSE_START, CAT_START = range(3)
DRAW, MOUSE, CAT = range(3)
def move(graph, lookup, i, other_i, is_mouse_turn):
key = (i, other_i, is_mouse_turn)
if key in lookup:
return lookup[key]
lookup[key] = DRAW
if is_mouse_turn:
skip, target, win, lose = other_i, HOLE, MOUSE, CAT
else:
skip, target, win, lose = HOLE, other_i, CAT, MOUSE
for nei in graph[i]:
if nei == target:
result = win
break
else:
result = lose
for nei in graph[i]:
if nei == skip:
continue
tmp = move(graph, lookup, other_i, nei, not is_mouse_turn)
if tmp == win:
result = win
break
if tmp == DRAW:
result = DRAW
lookup[key] = result
return result
return move(graph, {}, MOUSE_START, CAT_START, True)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助