[Swift]LeetCode794. 有效的井字遊戲 | Valid Tic-Tac-Toe State

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

A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game.git

The board is a 3 x 3 array, and consists of characters " ""X", and "O".  The " " character represents an empty square.github

Here are the rules of Tic-Tac-Toe:數組

  • Players take turns placing characters into empty squares (" ").
  • The first player always places "X" characters, while the second player always places "O" characters.
  • "X" and "O" characters are always placed into empty squares, never filled ones.
  • The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal.
  • The game also ends if all squares are non-empty.
  • No more moves can be played if the game is over.
Example 1:
Input: board = ["O  ", "   ", "   "]
Output: false
Explanation: The first player always plays "X".

Example 2:
Input: board = ["XOX", " X ", "   "]
Output: false
Explanation: Players take turns making moves.

Example 3:
Input: board = ["XXX", "   ", "OOO"]
Output: false

Example 4:
Input: board = ["XOX", "O O", "XOX"]
Output: true

Note:微信

  • board is a length-3 array of strings, where each string board[i] has length 3.
  • Each board[i][j] is a character in the set {" ", "X", "O"}.

用字符串數組做爲井字遊戲的遊戲板 board。當且僅當在井字遊戲過程當中,玩家有可能將字符放置成遊戲板所顯示的狀態時,才返回 true。函數

該遊戲板是一個 3 x 3 數組,由字符 " ""X" 和 "O" 組成。字符 " " 表明一個空位。this

如下是井字遊戲的規則:spa

  • 玩家輪流將字符放入空位(" ")中。
  • 第一個玩家老是放字符 「X」,且第二個玩家老是放字符 「O」。
  • 「X」 和 「O」 只容許放置在空位中,不容許對已放有字符的位置進行填充。
  • 當有 3 個相同(且非空)的字符填充任何行、列或對角線時,遊戲結束。
  • 當全部位置非空時,也算爲遊戲結束。
  • 若是遊戲結束,玩家不容許再放置字符。
示例 1:
輸入: board = ["O  ", "   ", "   "]
輸出: false
解釋: 第一個玩家老是放置「X」。

示例 2:
輸入: board = ["XOX", " X ", "   "]
輸出: false
解釋: 玩家應該是輪流放置的。

示例 3:
輸入: board = ["XXX", "   ", "OOO"]
輸出: false

示例 4:
輸入: board = ["XOX", "O O", "XOX"]
輸出: true

說明:code

  • 遊戲板 board 是長度爲 3 的字符串數組,其中每一個字符串 board[i] 的長度爲 3。
  •  board[i][j] 是集合 {" ", "X", "O"} 中的一個字符。

Runtime: 8 ms
Memory Usage: 19.9 MB
 1 class Solution {
 2     func validTicTacToe(_ board: [String]) -> Bool {
 3         var xwin:Bool = false
 4         var owin:Bool = false
 5         var row:[Int] = [Int](repeating:0,count:3)
 6         var col:[Int] = [Int](repeating:0,count:3)
 7         var diag:Int = 0
 8         var antidiag:Int = 0
 9         var turns:Int = 0
10         for i in 0..<3
11         {
12             for j in 0..<3
13             {
14                 if board[i][j] == "X"
15                 {
16                     row[i] += 1
17                     col[j] += 1
18                     turns += 1
19                     if i == j {diag += 1}
20                     if i + j == 2 {antidiag += 1}
21                 }
22                 else if board[i][j] == "O"
23                 {
24                     row[i] -= 1
25                     col[j] -= 1
26                     turns -= 1
27                     if i == j {diag -= 1}
28                     if i + j == 2 {antidiag -= 1}
29                 }                
30             }            
31         }
32         xwin = row[0] == 3 || row[1] == 3 || row[2] == 3 ||
33                col[0] == 3 || col[1] == 3 || col[2] == 3 ||
34                diag == 3 || antidiag == 3
35         owin = row[0] == -3 || row[1] == -3 || row[2] == -3 ||
36                col[0] == -3 || col[1] == -3 || col[2] == -3 ||
37                diag == -3 || antidiag == -3
38         if (xwin && turns == 0) || (owin && turns == 1) {return false}
39         return (turns == 0 || turns == 1) && (!xwin || !owin)
40     }
41 }
42 
43 //String擴展
44 extension String {        
45     //subscript函數能夠檢索數組中的值
46     //直接按照索引方式截取指定索引的字符
47     subscript (_ i: Int) -> Character {
48         //讀取字符
49         get {return self[index(startIndex, offsetBy: i)]}
50     }
51 }

8mshtm

 1 class Solution {
 2 
 3     func validTicTacToe(_ board: [String]) -> Bool {
 4         var xW = 0
 5         var oW = 0
 6         var numX = 0
 7         var numO = 0
 8 
 9         for i in 0..<board.count {
10             for j in 0..<board[i].count {
11                 checkSet(board[i][j], &numX, &numO)
12             }
13             if board[0][i]==board[1][i] && board[1][i]==board[2][i]{
14                 checkSet(board[0][i], &xW, &oW)
15             }
16 
17             if board[i][0]==board[i][1] && board[i][1]==board[i][2]{
18                 checkSet(board[i][0], &xW, &oW)
19             }
20         }
21 
22         if board[0][0]==board[1][1] && board[1][1]==board[2][2]{
23             checkSet(board[1][1], &xW, &oW);
24         }
25 
26         if board[0][2]==board[1][1] && board[1][1]==board[2][0]{
27             checkSet(board[1][1], &xW, &oW)
28         }
29         return (numX==numO && xW==0) || (numX == numO+1 && oW==0)
30     }
31 
32     func checkSet(_ v: Character, _ x: inout Int, _ o:inout Int) {
33         if v == "X"{
34             x += 1
35         }
36         else if v=="O" {
37             o += 1
38         }
39     }
40 }
41 
42 extension String{
43     subscript(index:Int)->Character{
44         return self[self.index(self.startIndex, offsetBy:index)]
45     }
46 }
相關文章
相關標籤/搜索