[Swift]LeetCode348. 設計井字棋遊戲 $ Design Tic-Tac-Toe

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

Design a Tic-tac-toe game that is played between two players on a n x n grid.git

You may assume the following rules:github

A move is guaranteed to be valid and is placed on an empty block.
Once a winning condition is reached, no more moves is allowed.
A player who succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game.
Example:
Given n = 3, assume that player 1 is "X" and player 2 is "O" in the board.數組

TicTacToe toe = new TicTacToe(3);微信

toe.move(0, 0, 1); -> Returns 0 (no one wins)
|X| | |
| | | | // Player 1 makes a move at (0, 0).
| | | |測試

toe.move(0, 2, 2); -> Returns 0 (no one wins)
|X| |O|
| | | | // Player 2 makes a move at (0, 2).
| | | |spa

toe.move(2, 2, 1); -> Returns 0 (no one wins)
|X| |O|
| | | | // Player 1 makes a move at (2, 2).
| | |X|設計

toe.move(1, 1, 2); -> Returns 0 (no one wins)
|X| |O|
| |O| | // Player 2 makes a move at (1, 1).
| | |X|code

toe.move(2, 0, 1); -> Returns 0 (no one wins)
|X| |O|
| |O| | // Player 1 makes a move at (2, 0).
|X| |X|htm

toe.move(1, 0, 2); -> Returns 0 (no one wins)
|X| |O|
|O|O| | // Player 2 makes a move at (1, 0).
|X| |X|

toe.move(2, 1, 1); -> Returns 1 (player 1 wins)
|X| |O|
|O|O| | // Player 1 makes a move at (2, 1).
|X|X|X|
Follow up:
Could you do better than O(n2) per move() operation?

Hint:

Could you trade extra space such that move() operation can be done in O(1)?
You need two arrays: int rows[n], int cols[n], plus two variables: diagonal, anti_diagonal.


設計一個井字遊戲,在N x N網格上的兩個玩家之間進行。 

您能夠假定如下規則: 

移動被保證是有效的,而且被放置在一個空塊上。

一旦達到獲勝條件,就不容許再移動。

在橫排、豎排或斜排中成功放置N個標記的玩家獲勝。

例子:

假設n=3,假設玩家1是「x」,玩家2是「o」。

TicTacToe toe = new TicTacToe(3);

toe.move(0, 0, 1); -> Returns 0 (no one wins)
|X| | |
| | | | // Player 1 makes a move at (0, 0).
| | | |

toe.move(0, 2, 2); -> Returns 0 (no one wins)
|X| |O|
| | | | // Player 2 makes a move at (0, 2).
| | | |

toe.move(2, 2, 1); -> Returns 0 (no one wins)
|X| |O|
| | | | // Player 1 makes a move at (2, 2).
| | |X|

toe.move(1, 1, 2); -> Returns 0 (no one wins)
|X| |O|
| |O| | // Player 2 makes a move at (1, 1).
| | |X|

toe.move(2, 0, 1); -> Returns 0 (no one wins)
|X| |O|
| |O| | // Player 1 makes a move at (2, 0).
|X| |X|

toe.move(1, 0, 2); -> Returns 0 (no one wins)
|X| |O|
|O|O| | // Player 2 makes a move at (1, 0).
|X| |X|

toe.move(2, 1, 1); -> Returns 1 (player 1 wins)
|X| |O|
|O|O| | // Player 1 makes a move at (2, 1).
|X|X|X|

跟進:

每一個move()操做能夠作得比O(n2) 更好嗎?

 提示: 

您是否能夠交換額外的空間,以便在O(1)中執行move()操做?

您須要兩個數組:int rows[n]、int cols[n],加上兩個變量:對角線、反斜線。


Solution:

 1 class TicTacToe {
 2     var diag:Int = 0
 3     var rev_diag:Int = 0
 4     var N:Int = 0
 5     var rows:[Int] = [Int]()
 6     var cols:[Int] = [Int]()
 7     
 8     init(_ n:Int)
 9     {
10         self.rows = [Int](repeating:0,count:n)
11         self.cols = [Int](repeating:0,count:n)
12         self.N = n
13     }
14     
15     func move(_ row:Int,_ col:Int,_ player:Int) -> Int
16     {
17         var add:Int = player == 1 ? 1 : -1
18         rows[row] += add
19         cols[col] += add
20         diag += (row == col ? add : 0)
21         rev_diag += (row == N - col - 1 ? add : 0)
22         if abs(rows[row]) == N || abs(cols[col]) == N || abs(diag) == N || abs(rev_diag) == N
23         {
24             return player
25         }
26         return 0
27     }    
28 }

點擊:Playground測試

 1 var toe:TicTacToe = TicTacToe(3)
 2 
 3 //toe.move(0, 0, 1); -> Returns 0 (no one wins)
 4 print(toe.move(0, 0, 1))
 5 //Print 0
 6 
 7 //toe.move(0, 2, 2); -> Returns 0 (no one wins)
 8 print(toe.move(0, 2, 2))
 9 //Print 0
10 
11 //toe.move(2, 2, 1); -> Returns 0 (no one wins)
12 print(toe.move(2, 2, 1))
13 //Print 0
14 
15 //toe.move(1, 1, 2); -> Returns 0 (no one wins)
16 print(toe.move(1, 1, 2))
17 //Print 0
18 
19 //toe.move(2, 0, 1); -> Returns 0 (no one wins)
20 print(toe.move(2, 0, 1))
21 //Print 0
22 
23 //toe.move(1, 0, 2); -> Returns 0 (no one wins)
24 print(toe.move(1, 0, 2))
25 //Print 0
26 
27 //toe.move(2, 1, 1); -> Returns 1 (player 1 wins)
28 print(toe.move(2, 1, 1))
29 //Print 1
相關文章
相關標籤/搜索