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

You may assume the following rules:

  1. A move is guaranteed to be valid and is placed on an empty block.
  2. Once a winning condition is reached, no more moves is allowed.
  3. A player who succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game.
class TicTacToe {

    /** Initialize your data structure here. */

    int[] rows;
int[] cols;
int diagnal;
int antidiagnal;
int size;
public TicTacToe(int n) {
this.rows = new int[n];
this.cols = new int[n];
this.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 toAdd = player == 1 ? 1 : -1;
rows[row] += toAdd;
cols[col] += toAdd;
if (row == col) {
diagnal += toAdd;
}
if (row + col + 1 == size) {
antidiagnal += toAdd;
}
// need to use Math.abs to consider player2
if (Math.abs(rows[row]) == size || Math.abs(cols[col]) == size || Math.abs(diagnal) == size || Math.abs(antidiagnal) == 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);
*/

[LC] 348. Design Tic-Tac-Toe的更多相关文章

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

  2. POJ 2361 Tic Tac Toe

    题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...

  3. 【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 ...

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

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

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  6. Epic - Tic Tac Toe

    N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...

  7. python 井字棋(Tic Tac Toe)

    说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...

  8. ACM-Team Tic Tac Toe

    我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...

  9. LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game

    地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...

随机推荐

  1. pandas dataframe取差集:删掉已存在的数据,保留未插入的数据

    适用场景: 插入数据到mysql中,中途中断,导致部分数据未插入成功.避免下次插入时插入了重复的数据. 思路: 1.读取已插入的数据, 2.读取全部数据(包含已插入和未插入的), 3.将已插入的数据添 ...

  2. jenkins+saltstack+pipeline 部署springcloud 多模块jar包

    在jenkins上安装salt-master, pipeline{ agent{       node{               label 'master'               cust ...

  3. VC6.0 The value of ESP was not properly saved across a function call 错误解决方法

    调用DLL函数,出现错误 Run-Time Check Failure #0 - The value of ESP was not properly saved across a function c ...

  4. import datetime

    import datetimenow = datetime.datetime.now()print('当前时间:',now) 当前时间: 2019-11-21 11:11:58.093122

  5. Java - 记录String中intern()方法的学习与理解

    intern()方法:把堆中的引用丢入常量池中,然后返回这个引用.当常量池中已经存在这个引用,就直接返回这个引用.(jdk1.8) 由于jdk1.7中将字符串常量池改为存放在堆中,因此intern() ...

  6. [kuangbin 带你飞] DP专题——HDU - 1024

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  7. 88.QuerySet API使用详解:get_or_create和bulk_create方法

    get_or_create 根据某个条件进行查找,如果找到了匹配的数据就会返回这条数据,如果没有找到匹配到的数据,就会创建一个.示例代码如下: from django.http import Http ...

  8. ImageTag,图片左上侧有一个小标签

    这实现思路其实很简单 需求:1. 图片比原来的<div>大,需要切割图片.2. 图片左上角显示标签 解决思路: 1. 把照片设置成div的backgroundImage,然后用CSS3切割 ...

  9. 基于Token的身份验证

    最近了解下基于 Token 的身份验证,跟大伙分享下.很多大型网站也都在用,比如 Facebook,Twitter,Google+,Github 等等,比起传统的身份验证方法,Token 扩展性更强, ...

  10. Java之同步代码块处理继承Thread类的线程安全问题

    package com.atguigu.java; /** *//** * 使用同步代码块解决继承Thread类的方式的线程安全问题 * * 例子:创建三个窗口卖票,总票数为100张.使用继承Thre ...