码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] 913. Cat and Mouse 猫和老鼠

时间:2019-09-13 01:29:43      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:持续更新   sam   nod   The   sum   nat   github   amp   problem   



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.



Github 同步地址:

https://github.com/grandyang/leetcode/issues/913



参考资料:

https://leetcode.com/problems/cat-and-mouse/



LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 913. Cat and Mouse 猫和老鼠

标签:持续更新   sam   nod   The   sum   nat   github   amp   problem   

原文地址:https://www.cnblogs.com/grandyang/p/11515655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!