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.
看了hint之后想到:既然用数组的话,一行一列都是一个element来代表,估计这个element是要用sum了,那么,能不能用sum来代表一行,使它有且只有一种可能,全部是player1完成的/全部是Player2;所以想到了是Player1就+1,是player2就-1,看最后sum是不是n,或者-n;n的情况只有一种情况这一行全是player1。因为说了不会有invalid move, 所以情况是唯一的
public class TicTacToe {
int[] rows;
int[] cols;
int diagonal;
int anti_diagonal;
int size;
/** Initialize your data structure here. */
public TicTacToe(int n) {
rows = new int[n];
cols = new int[n];
diagonal = 0;
anti_diagonal = 0;
size = 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 change = (player==1? 1 : -1);
rows[row] += change;
cols[col] += change;
if (row == col) diagonal += change;
if (row + col == size-1) anti_diagonal += change;
if (Math.abs(rows[row])==size || Math.abs(cols[col])==size || Math.abs(diagonal)==size || Math.abs(anti_diagonal)==size)
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);
*/
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 ...
随机推荐
- 如何让win10实现关机确认-暂没确认
为了实现关机时有提示确认,防止不小心触碰后不提示就关机了.本人安装有360软件小助手,发生过此事多次. 1.网上找到 http://zhidao.baidu.com/link?url=dYB0fl2S ...
- HTML—marquee
滚动标签 支持的属性: 1.align 2.behavior: alternate: 表示在两端之间来回滚动.scroll: 表示由一端滚动到另一端,会重复.slide: 表示由一端滚动到另一端,不 ...
- JS 点击弹出图片/ 仿QQ商城点击左右滚动幻灯片/ 相册模块,点击弹出图片,并左右滚动幻灯片
1, 点击弹出图片 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- javascript面向对象(三):非构造函数的继承
本文来自阮一峰 这个系列的第一部分介绍了"封装",第二部分介绍了使用构造函数实现"继承". 今天是最后一个部分,介绍不使用构造函数实现"继承" ...
- BSBuDeJie_02
一 左边的类别数据 1 模型 和 字典中的数据对应 /* id */ @property (nonatomic, assign) NSInteger *id; /* 总数 */ @property ( ...
- 那些年一起用过的iOS开发利器[4月2号更新]
4月2号新增Runscope. Runscope 这是一家专注于API工具开发的公司,其创始人John Sheehan曾就职于IFTTT和Twilio.Runscope是一款集调试.测试于一身的网络服 ...
- Ecshop:后台添加新功能栏目以及管理权限设置
一.添加菜单项 打开 /admin/includes/inc_menu.php文件(后台框架左边菜单),在最后添加一行如下: $modules['17_other_menu']['sns_list'] ...
- [转]SQL Server 存储过程 一些常用用法(事物、异常捕捉、循环)
最新更新请访问: http://denghejun.github.io Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中 ...
- 微信支付(20140923更新)商户支付密钥key的生成与设置
微信支付(0923更新)商户支付密钥key的生成与设置 说明:新版微信支付,用户必须授权登录才能支付.需要商家自己设置商户号支付密钥. 设置商户号支付密钥方法如下: 1. 申请通过审核后,打开微信发来 ...
- c++代码中,使用svn版本号作为程序版本号的实现方法
1.编写版本模板文件 #ifndef _VERSIONSVN_H_#define _VERSIONSVN_H_#define VER_REVISIONSVN $WCREV$#endif //!_VER ...