提示给的太直白了。。

比如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. 取出当前会话的sid等

    select distinct sess.SID     db_sid,                sess.SERIAL# db_serial#,                process. ...

  2. iOS 代码分类

    控件分类: 指示器 (ActivityIndicator) 提醒对话框 (AlertView) 按钮 (Button) 日历 (Calendar) 相机 (Camera) 透明指示层 (HUD) 图像 ...

  3. Swift - 使用CoreLocation实现定位(经纬度、海拔、速度、距离等)

    CoreLocation是iOS中一个提供设备定位的框架.通过这个框架可以实现定位处理,从而获取位置数据,比如经度.纬度.海拔信息等.   1,定位精度的设置 定位服务管理类CLLocationMan ...

  4. VisualStudio2015内置LocalDB

    简直坑爹,我将之前的VS2013的工程迁移到新电脑的VS2015,然后用的本地数据库居然连接报错了,然后我试连了一下本地数据库, 就是Tools-->Connect to Databases-- ...

  5. java——输入流FileInputStream

    写一个简单的程序,实现从电脑中的一个盘里的文件中往程序中输入内容. package com.liaojianya.chapter5; import java.io.FileInputStream; i ...

  6. python中如何使用help命令?

    python下 help()使用方法   查看python所有的modules:help("modules") 单看python所有的modules中包含指定字符串的modules ...

  7. photoshop中rgb与索引模式的区别

    RGB: 是色域最宽广的颜色模式它是一种光色模式不难理解  你的显示器 就是根据这个模式来显示图像的同样 我们在自然界中 看到的所有五颜六色的东西都是吸收了不同颜色的光 而得到的所以这一颜色模式 与我 ...

  8. oc 怎么接收NSSting字符的方法

    ]; //使用一个缓冲区 NSLog(@"请输入一个字符串:"); scanf("%s",buffer); NSString * str = [NSString ...

  9. 从 man 指令起步(info简介)

    前言 小生认为一切指令的学习首先要从帮助入手,深入了解它的功能,即使是在实际项目中我们都离不开它的帮助.因为我们不一定能够记住全部指令的全部的相关功能,因此,查看指令的帮助是我们的不二选择. 正文 下 ...

  10. js数学方法应用

    找出数组中最大的数 var values = [1, 2, 3, 4, 5, 6, 7, 8]; alert(Math.min.apply(Math,values))//8 这个技巧的关键是把 Mat ...