提示给的太直白了。。

比如player 1占据了(0,1),那么row[0]++ col[1]++

表示第一行有1个O,第一列有1个X,假设PLAYER 1最终在第一行连成一排,那最终row[0] == n。

player 2占据了(0,2),那么 row[0]-- col[2]--

如果PLAYER最终在第三列连成一排,那么col[2] == -n

总之选手A,走到(a,b) 那个格所在行列+1,如果是对角线对角线+1,B的话就-1,先到N的就赢了。。。

public class TicTacToe
{ int[] rows;
int[] cols;
int d1;
int d2;
int n; /** Initialize your data structure here. */
public TicTacToe(int n)
{
this.n = n;
rows = new int[n];
cols = new int[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 p = 0;
if(player == 1) p = 1;
else p = -1;
rows[row] += p;
if(rows[row] == n || rows[row] == -n) return player; cols[col] += p;
if(cols[col] == n || cols[col] == -n) return player; if(row == col)
{
d1 += p;
if(d1 == n || d1 == -n) return player;
} if(row+col == n-1)
{
d2 += p;
if(d2 == n || d2 == -n) return player;
}
return 0; } }

没看提示前上头了啊。。和打DOTA一个德性。。

一开始那个办法太难了,非要做对,做了好久,提示的办法10分钟就搞定了,次奥。。

贴个一开始的办法。。

用x*n + y当做KEY,PLAYER当做VALUE来建立MAP。。

然后每次查MAP看有没有人赢。。检查MAP的时候要以当前点为中点,横纵坐标分别各从-N到+N,对角线同时-N到+N,然后一个-N到+N,一个+N到-N。

好蠢……

public class TicTacToe
{ Map<Integer,Integer> map;
int n;
/** Initialize your data structure here. */
public TicTacToe(int n)
{
this.n = n;
map = new HashMap<Integer,Integer>();
} /** 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 key = row*n+col;
map.put(key,player);
if(check(key,player)) return player;
else return 0;
} public boolean check(int k,int p)
{
// horizontal
int i = 0;
int row = k/n;
int col = k%n;
boolean[] check = new boolean[n]; int c = n-1; for(int j = -c; j <= c; j++)
{
int key = (row+j)*n + col;
if(row+j < 0 || row + j >= n) i = 0;
else if(map.containsKey(key) && map.get(key) == p)
{ check[i++] = true;
if(i == n)
{
boolean temp = true;
for(int m = 0; m < n;m++)
{
temp = temp&&check[m];
if(!temp) break;
}
if(temp) return true;
} }
else i = 0; }
i = 0; for(int j = -c; j <= c; j++)
{
int key = row*n + col+j; if(col+j < 0 || col + j >= n) i = 0;
else if(map.containsKey(key) && map.get(key) == p)
{ check[i++] = true;
if(i == n)
{
boolean temp = true;
for(int m = 0; m < n;m++)
{
temp = temp&&check[m];
if(!temp) break;
}
if(temp) return true;
}
}
else i = 0; }
i = 0; for(int j = -c; j <= c; j++)
{
int key = (row+j)*n + col-j;
if(row + j < 0 || col-j < 0 || row + j >= n || col -j >= n) i = 0;
else if(map.containsKey(key) && map.get(key) == p)
{ check[i++] = true;
if(i == n)
{
boolean temp = true;
for(int m = 0; m < n;m++)
{
temp = temp&&check[m];
if(!temp) break;
}
if(temp) return true;
}
}
else i = 0; } i = 0;
for(int j = -c; j <= c; j++)
{
int key = (row-j)*n + col-j;
if(row - j < 0 || col-j < 0 || row - j >= n || col -j >= n) i = 0;
else if(map.containsKey(key) && map.get(key) == p)
{ check[i++] = true;
if(i == n)
{
boolean temp = true;
for(int m = 0; m < n;m++)
{
temp = temp&&check[m];
if(!temp) break;
}
if(temp) return true;
}
}
else i = 0; } return false; /*
["TicTacToe","move","move","move","move","move","move","move"]
[[3],[0,0,1],[0,2,2],[2,2,1],[1,1,2],[2,0,1],[1,0,2],[2,1,1]]
["TicTacToe","move","move","move"]
[[2],[0,0,2],[1,1,1],[0,1,2]]
["TicTacToe","move","move","move"]
[[2],[0,1,1],[1,1,2],[1,0,1]] */ }
} /**
* Your TicTacToe object will be instantiated and called as such:
* TicTacToe obj = new TicTacToe(n);
* int param_1 = obj.move(row,col,player);
*/

438MS 被100%的人干死了。。

空间CHEKC[]是N

MAP是N

早看提示就好了。

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

随机推荐

  1. opencv安装及学习资料

    第一次装时win7+VS2010+opencv3.0,结果不成功,原因解压出来的没有vc10,可能新版本不在支持vc的旧版本了.所以换了VS2013+opencv3.0,比较经典的安装时VS2010+ ...

  2. SGU 186.The Chain

    看懂题就是水题... code #include <iostream> #include <algorithm> using namespace std; int a[110] ...

  3. BIOS中断大全

    BIOS中断大全 BIOS中断:1.显示服务(Video Service——INT 10H)  00H —设置显示器模式0CH —写图形象素01H —设置光标形状0DH —读图形象素02H —设置光标 ...

  4. js+dom开发第十六天

    一.css常用标签及页面布局 1.常用标签 position(定位) z-index(定位多层顺序) background(背景) text-align(针对字符自动左右居中) margin(外边距) ...

  5. UBOOT的多支持性与可裁剪性

    UBOOT功能强大,适用于多种操作系统,多种处理器架构. 在阅读它的源码时,可以看到cpu目录有各种处理器,而board目录有各种开发板.但是,对于一个特定的实验平台,例如TQ2440开发板,它用到的 ...

  6. Hadoop 学习笔记 (八) hadoop2.2.0 测试环境部署 及两种启动方式

    1基本流程步骤1:准备硬件(linux操作系统)步骤2:准备软件安装包,并安装基础软件(主要是JDK)步骤3:修改配置文件步骤4:分发hadoop步骤5:启动服务步骤6:验证是否启动成功!2硬件配置要 ...

  7. 如何把iOS代码编译为Android应用

    新闻 <iPhone 6/6 Plus中国销量曝光:单月销量650万>:据iSuppli Corp.中国研究总监王阳爆料,iPhone 6和iPhone 6 Plus在国内受欢迎的情况大大 ...

  8. 【Java】Java网络编程菜鸟进阶:TCP和套接字入门

    Java网络编程菜鸟进阶:TCP和套接字入门 JDK 提供了对 TCP(Transmission Control Protocol,传输控制协议)和 UDP(User Datagram Protoco ...

  9. Cow Exhibition

    poj2184:http://poj.org/problem?id=2184 题意:给你n头牛,每头牛有一个S值和一个F值,现在的问题是,要你选出其中的一些牛求出S+T的最大值.但是要保证总的s> ...

  10. cf D Bear and Floodlight

    题意:有n个灯,每个灯有一个照亮的角度,现在从点(l,0)走到点(r,0),问这个人若一直被灯照着能最多走多远? 思路:状压dp,然后通过向量旋转求出点(dp[i[,0)与灯的坐标(p[j].x,p[ ...