913. Cat and Mouse

A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns.

The graph is given as follows: graph[a] is a list of all nodes b such that ab is an edge of the graph.

Mouse starts at node 1 and goes first, Cat starts at node 2 and goes second, and there is a Hole at node 0.

During each player's turn, they must travel along one edge of the graph that meets where they are.  For example, if the Mouse is at node 1, it must travel to any node in graph[1].

Additionally, it is not allowed for the Cat to travel to the Hole (node 0.)

Then, the game can end in 3 ways:

  • If ever the Cat occupies the same node as the Mouse, the Cat wins.
  • If ever the Mouse reaches the Hole, the Mouse wins.
  • If ever a position is repeated (ie. the players are in the same position as a previous turn, and it is the same player's turn to move), the game is a draw.

Given a graph, and assuming both players play optimally, return 1 if the game is won by Mouse, 2 if the game is won by Cat, and 0 if the game is a draw.

 

Example 1:

Input: [[2,5],[3],[0,4,5],[1,4,5],[2,3],[0,2,3]]
Output: 0
Explanation:
4---3---1
|   |
2---5
 \ /
  0

 

Note:

  1. 3 <= graph.length <= 50
  2. It is guaranteed that graph[1] is non-empty.
  3. It is guaranteed that graph[2] contains a non-zero element. 

思路:模拟一遍游戏,

1. 注意返回值,如果你设定的返回是一个数,就照着一个数的思想去想

2. 在求某个状态的时候,先把这个状态设置为draw,这样可以处理环的问题

https://leetcode.com/problems/cat-and-mouse/discuss/175892/recursion-+-memorization-84ms-Python

class Solution(object):
    def catMouseGame(self, graph):
        """
        :type graph: List[List[int]]
        :rtype: int
        """
        mouse,cat={},{}
        
        def mouse_move(i,j):
            if (i,j) in mouse: return mouse[(i,j)]
            mouse[(i,j)]=0
            
            canDraw = False
            for t in graph[i]:
                if t==0:
                    mouse[(i,j)]=1
                    return 1
                if t==j: continue
                tmp = cat_move(t,j)
                if tmp==1:
                    mouse[(i,j)]=1
                    return 1
                if tmp==0: canDraw=True
            res = 0 if canDraw else 2
            mouse[(i,j)]=res
            return res
                
        def cat_move(i,j):
            if (i,j) in cat: return cat[(i,j)]
            cat[(i,j)]=0 # handle draw case
            
            canDraw = False
            for t in graph[i]:
                # case1
                if t==j:
                    cat[(i,j)]=2
                    return 2
                # case2
                if t==0: continue
                # case3
                tmp = mouse_move(i,t)
                if tmp==2:
                    cat[(i,j)]=2
                    return 2
                if tmp==0: canDraw=True
            res = 0 if canDraw else 1
            cat[(i,j)]=res
            return res
        
        return mouse_move(1,2)
        
        
s=Solution()
print(s.catMouseGame([[2,5],[3],[0,4,5],[1,4,5],[2,3],[0,2,3]]))
            

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值