[Swift]LeetCode913.貓與老鼠 | Cat and Mouse

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-nzpxraef-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

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

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

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.)spa

Then, the game can end in 3 ways:code

  • 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.htm

Example 1:blog

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. 

一個無向上的遊戲由兩個玩家組成,鼠標和貓,他們交替輪流。

該圖以下給出:graph[a]是全部節點的列表,b例如ab圖的邊緣。

鼠標從節點1開始並先行,Cat從節點2開始而後變爲第二個,而且節點0處有一個孔。

在每一個玩家的回合中,他們必須沿着圖表的一個邊緣行進,以知足它們的位置。例如,若是鼠標位於節點1,則它必須前往任何節點graph[1]

此外,Cat不容許移動到孔(節點0)。

而後,遊戲能夠以3種方式結束:

  • 若是Cat佔用與鼠標相同的節點,則Cat獲勝。
  • 若是鼠標到達了洞,鼠標就會獲勝。
  • 若是一個位置被重複(即,玩家處於與前一回合相同的位置,而且輪到同一個玩家),則該遊戲是平局。

給定a graph,並假設兩個玩家都玩得最佳,1 若是遊戲是由鼠標贏得,2 若是遊戲是由Cat贏得,而且0 遊戲是平局,則返回。

例1:

輸入:[[2,5],[3],[0,4,5],[1,4,5],[2,3],[0,2,3]] 
輸出:0 說明:  4- --3 --- 1 | | 2 --- 5  \ / \   0

注意:

  1. 3 <= graph.length <= 50
  2. 保證graph[1]非空。
  3. 保證graph[2]包含非零元素。 

140ms
 1 class Solution {
 2     func catMouseGame(_ graph: [[Int]]) -> Int {
 3         var n:Int = graph.count
 4         var win:[[Int]] = [[Int]](repeating: [Int](repeating: 0, count: n*n), count: 2)
 5         //mc
 6         for i in 0..<n
 7         {
 8             win[0][i] = 1
 9             win[1][i] = 1
10         }
11         for i in 0..<n
12         {
13             win[0][i*n+i] = 2
14             win[1][i*n+i] = 2
15         }
16         
17         while(true)
18         {
19             var anew:Bool = false
20             for m in 0..<n
21             {
22                 inner:
23                 for c in 1..<n
24                 {
25                     if win[0][m*n+c] == 0
26                     {
27                         var und:Bool = false
28                         for e in graph[m]
29                         {
30                             if win[1][e*n+c] == 1
31                             {
32                                 win[0][m*n+c] = 1
33                                 anew = true
34                                 continue inner
35                             }
36                             if win[1][e*n+c] == 0
37                             {
38                                     und = true
39                             }
40                         }
41                         if !und
42                         {
43                             win[0][m*n+c] = 2
44                             anew = true
45                         }
46                     }
47                 }
48             }
49             for c in 1..<n
50             {
51                 inner:
52                 for m in 0..<n
53                 {
54                     if win[1][m*n+c] == 0
55                     {
56                         var und:Bool = false
57                          for e in graph[c]
58                         {
59                             if e == 0 {continue}
60                             if win[0][m*n+e] == 2
61                             {
62                                 win[1][m*n+c] = 2
63                                 anew = true
64                                 continue inner
65                             }
66                             if win[0][m*n+e] == 0
67                             {
68                                 und = true
69                             }
70                         }
71                         if !und
72                         {
73                              win[1][m*n+c] = 1
74                             anew = true
75                         }
76                     }
77                 }
78             }
79             if !anew {break}
80         }
81         return win[0][1*n+2]
82     }
83 }
相關文章
相關標籤/搜索