LeetCode 348. Design Tic-Tac-Toe
原题链接在这里:https://leetcode.com/problems/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?
题解:
If the player wants to win after placing on current coordinate.
Either the entire row or column is this player's number.
If current coordinate is on diagonal or anti-diagonal, and entire diagonal is this player's number, player could win.
Thus have a row array, col array to track target.
d1, d2 to track diagonal and anti-diagonal. Since there is only one diagonal and one anti-diagonal.
Have player one add +1, and player two add -1.
When it hits +5, player one wins. When it hits -5, player two wins.
TIme Complexity: move, O(1).
Space: O(n).
AC Java:
class TicTacToe {
int [] r;
int [] c;
int d1;
int d2;
int n; /** Initialize your data structure here. */
public TicTacToe(int n) {
r = new int[n];
c = new int[n];
d1 = 0;
d2 = 0;
this.n = n;
} /** 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 toAdd = player == 1 ? 1 : -1;
int target = player == 1 ? n : -n; if(row == col){
d1 += toAdd;
if(d1 == target){
return player;
}
} if(row + col + 1 == n){
d2 += toAdd;
if(d2 == target){
return player;
}
} r[row] += toAdd;
c[col] += toAdd;
if(r[row] == target || c[col] == target){
return player;
} 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);
*/
LeetCode 348. Design Tic-Tac-Toe的更多相关文章
- 【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 ...
- 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= ...
- 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 ...
- LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...
- [LeetCode] 348. 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 ...
- [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算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...
随机推荐
- 2018-2019-2 20162329 《网络对抗技术》Exp8: Web基础
目录 Exp8: Web基础 一. 基础问题回答 1. 什么是表单 2. 浏览器可以解析运行什么语言. 3. WebServer支持哪些动态语言 二. 实验过程 1. Web前端HTML 2..Web ...
- python3 四舍五入(0.5可以进1)
今天做了一个题要求四舍五入,然后找了一个方法:round()可以四舍五入, 试了试1.5--->2 试了试0.5--->0 !!!! 找了几个方法说可以的: # 方法一: from _ ...
- 【开源监控】Grafana介绍与安装
Grafana介绍与安装 Grafana介绍 场景:由于业务场景,有多个组织机构.需要在某个组织结构下,完成对本机构下的系统的实时监控以及可视化展示.底层已经用zabbix对监控指标做了数据的采集. ...
- mac上使用Sequel Pro工具SSH连接数据库
今天在使用Mac上的Sequel Pro连接线上的数据库时,一直报ssh通道连接失败.但是同样的公钥在另一台机器就可以,真是奇怪. 通过查找日志发现有一个关键字"key_load_publi ...
- SpringIOC源码解析(下)
注意,看完这篇文章需要很长很长很长时间... 本篇文章是SpringIOC源码解析(上)的续集,上一篇文章介绍了使用XML的方式启动Spring,然后追踪了BeanFactory容器的创建.配置文件的 ...
- 微信小程序环境配置和开发!!
1.登陆微信公众平台小程序,下载 普通小程序开发者工具.或者 小游戏开发者工具. 2.新建项目需要填以下几点,然后初始demo如下,注意rpx是分成750份的单位. 3.点击预览,用微信扫描二维码,代 ...
- 基于wince系统开发的 SQLServe Mobile数据库应用
SQLServe Mobile数据库以前的版本是SQLServe CE,现在最新的是3.5的版本,最近用vs2008做了一个小程序,是对SQLServe Mobile数据库读写数据. 注:SQLSer ...
- 大规模定制模式之于MES的三点思考
大规模定制(Mass Custermization) ,其目标是大规模生产定制化产品,并且在效率.质量(一致性)等指标方面与大规模批量生产等齐. 这是一种理想或者追求,其提出的背景是目前越发普遍的多品 ...
- 【Java_基础】Java中强制类型转换
首先,狭义上的强制类型转换指的是引用类型,且是父类向子类转换,这种转换只牵扯到引用名义类型的转换,具体的对象内存没有发生一点变化. 而基本类型的转换与此不同,其数据确实发生了变化.如果是基本类型和其包 ...
- Linux shell while循环语句
for :明确循环次数 while :不确定循环换次数 while循环 (1) while CONDITION:do statement statement < ...