[Swift]LeetCode348. 设计井字棋游戏 $ Design Tic-Tac-Toe

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

You may assume the following rules:

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

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|
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

原文地址:https://www.cnblogs.com/strengthen/p/10740257.html

时间: 2024-10-08 01:36:07

[Swift]LeetCode348. 设计井字棋游戏 $ Design Tic-Tac-Toe的相关文章

[LeetCode] Design Tic-Tac-Toe 设计井字棋游戏

Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the following rules: 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

[CareerCup] 17.2 Tic Tac Toe 井字棋游戏

17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏,有下面几点需要考虑: 1. 判断是否能赢hasWon函数是调用一次还是多次,如果是多次,我们可能为了优化而需要加入一些预处理. 2. 井字棋游戏通常是3x3的大小,我们是否想要实现NxN的大小? 3. 我们需要在代码紧凑,执行速度和代码清晰之间做出选择. #include <iostream> #

人工智能博弈树算法做的井字棋游戏

不会输,超碉!井字棋这个游戏真是太无聊啦! 算法大概就是,有一个给状况进行估价的函数,深搜每种状况,假设每个人都按对自己最有利的方式走(假设玩家也是不傻),最后让电脑走出最有利的一步. 代码: 1 //#pragma comment(linker, "/STACK:102400000,102400000") 2 #include<cstdio> 3 #include<cmath> 4 #include<iostream> 5 #include<

井字棋游戏

#include "Board.h" Board::Board(){    size = 10;    s = new char[9];    set();                           //对于棋盘的初始化 } char Board::getS(int i){ return s[i];} int Board::getSize(){ return size;} Board::Board(Board &c){    size = c.size;    s =

井字棋

题目描述 对于一个给定的井字棋棋盘,请设计一个高效算法判断当前玩家是否获胜. 给定一个二维数组board,代表当前棋盘,其中元素为1的代表是当前玩家的棋子,为0表示没有棋子,为-1代表是对方玩家的棋子. 测试样例: [[1,0,1],[1,-1,-1],[1,-1,0]] 返回:true //判断每行每列每个对角线上的值是否都为1 class Board { public: bool checkWon(vector<vector<int> > board) { int len =

井字棋游戏升级版 - TopTicTacToe项目 简介

一.游戏简介 井字棋是一款世界闻名的游戏,不用我说,你一定知道它的游戏规则. 这款游戏简单易学,玩起来很有意思,不过已经证明出这款游戏如果两个玩家都足够聪明的话, 是很容易无法分出胜负的,即我们得到的结果是平局. 我们的项目,就是井字棋游戏的升级版!游戏有九个小棋盘,每个棋盘构成了整体布局的一部分,要想获得游戏的胜利,你要把握整个局势才行! 二.亮点 创新 传统的井字棋只有九个格,玩法简单,但是变化也相当有限.初玩者很容易被这新颖的游戏吸引住,但是玩了一段时间后,很容易摸出规律,很轻松达到不败的

井字棋的最优策略竟是先占角!

http://www.guokr.com/article/4754/ 井字棋可能是最简单的棋类游戏了,它简单到了成年人之间玩几乎总是平局的地步.因此,这个游戏貌似最多只能哄哄小孩子.不过,对井字棋游戏中所有可能的情况进行一番细致的分析,你会发现一个你或许不会料到的惊人结论——先手的最优策略不是稳坐正中央,而是先占一个角! 几年前,果壳网小编曾经自己动手写过一个和人下井字棋的电脑程序,运行之后却发现电脑先走时总爱把第一步棋下在角上:检查程序代码许久后才意识到,电脑程序可能并没有问题.人们往往有一个

[游戏学习22] MFC 井字棋 双人对战

>_<:太多啦,感觉用英语说的太慢啦,没想到一年做的东西竟然这么多.....接下来要加速啦! >_<:注意这里必须用MFC和前面的Win32不一样啦! >_<:这也是第一次出现MFC游戏,其框架和逻辑的写法和Win32有很大的区别,建议先看一下MFC的基础再理解代码: >_<:TicTac.h 1 #define EX 1 //该点左鼠标 2 #define OH 2 //该点右鼠标 3 4 class CMyApp : public CWinApp 5 {

C++井字棋游戏,DOS界面版

据说有一个能保证不败的算法.明天看看先再写个PVC版的. 正题.今天无聊写了个井字棋游戏,顺便逐渐让自己习惯良好的代码风格,放上来给新手学习学习. jzq2.cpp /* N字棋游戏PVP版,DOS版 本棋盘可扩充,仅仅需调整检測条件就可以,其它接口不需改变. 非人机对战型.PVP类型; @author:天下无双 @date:2014-5-25 @version:1.0 */ #include <iostream> #include <string> #define INVALID