[抄题]:

Design a Tic-tac-toe game that is played between two players on a n x n 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.

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|

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么表示数学关系:每一行都不能有2种元素。所以必须要rows[row] == row才行

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 对角线元素也不是直接加一的,也要加addon来表示区别
  2. 反向相加也行,所以最后的结果是绝对值

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

反向相加也行,所以最后的结果是绝对值

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class TicTacToe {
private int[] rows;
private int[] cols;
private int diagnonal;
private int antidiagonal; public TicTacToe(int n) {
int[] rows = new int[n];
int[] cols = new int[n];
} public int move(int row, int col, int player) {
//initialization: add 1 or -1
int addOn = (player == 1) ? 1 : -1; //handle 4 cases
rows[row] += addOn;
cols[col] += addOn; if (row == col) {
diagnonal += addOn;
} if (row + col == cols.length - 1) {
antidiagonal += addOn;
} //exit if element == row
int size = rows.length;
if (Math.abs(rows[row]) == size || Math.abs(cols[col]) == size || Math.abs(diagnonal) == size || Math.abs(antidiagonal) == size)
{
return player;
}
return 0;
}
}

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 的网格上玩井字棋. 井字棋游戏的规则如下: ...

  10. eclipse里打开SWT项目找不到source/design的图形UI设计界面

    因为前天重新装了个新版的eclipse, 结果今天打开一个SWT的项目,突然找不到source/design的图形UI设计的两个切换按钮 我把SWT组件重新装了还是找不到.结果后来发现是因为重装ecl ...

随机推荐

  1. c# android 全局捕获未处理异常

    [Application] public class MyApp : Application { public MyApp(IntPtr javaReference, JniHandleOwnersh ...

  2. c#泛型TryParse类型转换

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...

  3. IO流小笔记

    File file=new File ();括号里面写路径 exists()判断文件是否存在:isfile()是判断已经存在的文件是文件还是目录: mkdir()和createNewFile()区别在 ...

  4. UltraISO 9.7.1.3519注册码

    王涛 7C81-1689-4046-626F redcaps 82C6-3DEF-AB07-0EC0

  5. 【python】dist-packages和site-packages的区别

    一.dist-packages和site-packages的区别 sudo apt-get install 安装的package存放在/usr/lib/python2./dist-packages目录 ...

  6. Redis和Memcache区别,优缺点对比

    1. Redis和Memcache都是将数据存放在内存中,都是内存数据库.不过memcache还可用于缓存其他东西,例如图片.视频等等. 2.Redis不仅仅支持简单的k/v类型的数据,同时还提供li ...

  7. 滑雪(dp)

    问题 H: [例9.24]滑雪 时间限制: 1 Sec  内存限制: 128 MB提交: 21  解决: 13 题目描述 小明喜欢滑雪,因为滑雪的确很刺激,可是为了获得速度,滑的区域必须向下倾斜,当小 ...

  8. 经典算法二分查找循环实现Java版

    二分查找 定义 二分查找(Binary Search)又称折半查找,它是一种效率较高的查找方法. 要求 (1)必须采用顺序存储结构 (2)必须按关键字大小有序排列 查找思路 首先将给定值K,与表中中间 ...

  9. 异步启动solidworks

    两种方法: SldWorks App = new SldWorks(); App.Visible = true; //SldWorks.Application.24是2016 // App = (Sl ...

  10. 作着玩:登录页(纯css,不支持ie9以下)

    支持chrome FireFox 和 IE10+,(IE9也能显示,IE9以下不支持) <style type="text/css"> body{position:re ...