在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns. The graph is given as follows: 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 Additionally, it is not allowed for the Cat to travel to the Hole (node 0.) Then, the game can end in 3 ways:
Given a 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开始并先行,Cat从节点2开始然后变为第二个,并且节点0处有一个孔。 在每个玩家的回合中,他们必须沿着图表的一个边缘行进,以满足它们的位置。例如,如果鼠标位于节点 此外,Cat不允许移动到孔(节点0)。 然后,游戏可以以3种方式结束:
给定a 例1: 输入:[[2,5],[3],[0,4,5],[1,4,5],[2,3],[0,2,3]]
输出:0
说明:
4- --3 --- 1
| |
2 --- 5
\ / \
0
注意:
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 }
|
请发表评论