Design Tic-Tac-Toe 解答
Question
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.
Answer
对于move操作,简单粗暴的方法是遍历整个二维数组,对每一行每一列以及对角线检查。时间,空间复杂度均为O(n^2)
我们可以单独考虑行,列,对角。
对于一行row[i],如果这一行中有棋手1下过,则+1。如果有棋手2下过,则-1。所以如果这一行全为棋手1下的,row[i]=n。棋手1获胜。
Similar case for col and diagonal, anti diagonal。
Time complexity O(1) Space complexity O(n)
public class TicTacToe { private int[] row;
private int[] col;
private int diagonal;
private int anti_diagonal;
private int size;
/** Initialize your data structure here. */
public TicTacToe(int n) {
size = n;
row = new int[n];
col = new int[n];
Arrays.fill(row, 0);
Arrays.fill(col, 0);
diagonal = 0;
anti_diagonal = 0;
} /** Player {player} makes a move at ({row}, {col}).
@param row The row of the board.
@param col The column of the board.
@param player The player, can be either 1 or 2.
@return The current winning condition, can be either:
0: No one wins.
1: Player 1 wins.
2: Player 2 wins. */
public int move(int row, int col, int player) {
int change = player == 1 ? 1 : -1;
this.row[row] += change;
this.col[col] += change;
if (row == col) {
diagonal += change;
}
if (row == (size - col - 1)) {
anti_diagonal += change;
}
if (this.row[row] == size || this.col[col] == size || diagonal == size || anti_diagonal == size) {
return 1;
}
if (this.row[row] == -size || this.col[col] == -size || diagonal == -size || anti_diagonal == -size) {
return 2;
}
return 0;
}
} /**
* Your TicTacToe object will be instantiated and called as such:
* TicTacToe obj = new TicTacToe(n);
* int param_1 = obj.move(row,col,player);
*/
Design Tic-Tac-Toe 解答的更多相关文章
- Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy
1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...
- POJ 2361 Tic Tac Toe
题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...
- 【leetcode】1275. Find Winner on a Tic Tac Toe Game
题目如下: Tic-tac-toe is played by two players A and B on a 3 x 3 grid. Here are the rules of Tic-Tac-To ...
- 2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe
题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- Epic - Tic Tac Toe
N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...
- python 井字棋(Tic Tac Toe)
说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...
- ACM-Team Tic Tac Toe
我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...
- LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...
- [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 fol ...
随机推荐
- VB.NET 数组的定义 动态使用 多维数组
我们都知道在全部程序设计语言中数组都是一个非常重要的概念,数组的作用是同意程序猿用同一个名称来引用多个变量,因此採用数组索引来区分这些变量.非常多情况下利用数组索引来设置一个循环,这样就能够高效地处理 ...
- Android -- getSystemService
Android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监听是否有SD卡安装及移除,ClipboardServic ...
- java中说明书/开发文档如何编写?
由于在java开发时我们得到的或者给别人的文件一般都是class文件,不会给出源文件,故编写一个简洁易懂的说明书是必须的. ps: @param int[] arr 会有警告,可以删掉 int []. ...
- ASP.NET-FineUI开发实践-1
用.Net开发3年,主要接触资产管理,生产流程,质量追溯,.Net在这方面还是很靠谱的.2月低接触FineUI,那时版本是3.0+,第一眼让我想起了ExtJs,其实就是用.Net封装了ExtJs让AS ...
- 《CSS网站布局实录》学习笔记(六)
第六章 CSS高级应用与技巧 6.1 id与class 6.1.1 什么是id id是XHTML元素的一个属性,用于标识对象名称.无论是class还是id,都是XHTML所支持的公共属性,并且也是其核 ...
- 手机Web网站,设置拒绝电脑访问
最近一段时间,都在使用Jquery-Mobile + MVC做手机Web,有一些心得.体会 下面介绍如何拒绝电脑访问手机网站 电脑的浏览器,跟手机的浏览器内核不一样,这是我设置拒绝访问的思路. 下面是 ...
- maven发布时在不同的环境使用不同的配置文件
在开发时,不同的环境总会使用到不同的配置.如本地,测试,预发布,发布等环境,像数据库这些都要使用到不同的配置.如果手动改的话肯定会十分的麻烦. 还好maven提供的功能能够帮我们解决这个问题. 我们通 ...
- HTML5 canvas 绘制五星红旗
这个例子并不是自己写的,在网上找的案列,仿照写的,,,自己真的公布董这些算法,看完这个例子还是有一点模糊,,, 如果谁看的比较明白,指点一下,,,多谢!!!! <!doctype html> ...
- 关于js闭包杂记
闭包:一个函数oneF里return了另一个函数innerF,然后在oneF外面运行了函数innerF,如果innerF里有用到在oneF里定义的变量,则此时依然可以引用到, 但是变量值不是定义函数i ...
- Graphics2D 中文乱码
今天遇到了一个乱码问题,合成的小票图片上的中文全部变成了口口口,后来在网上查了资料,发现是Graphics2D用了宋体字,而linux服务器上没有对应的字体库. 把本地的字体库上传上去就解决了. 本地 ...