348. Design Tic-Tac-Toe
提示给的太直白了。。
比如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的更多相关文章
- 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 的网格上玩井字棋. 井字棋游戏的规则如下: ...
 - 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 ...
 
随机推荐
- IOS开发效率之为Xcode添加常用的代码片段
			
IOS开发效率之为Xcode添加常用的代码片段 原文地址:http://blog.csdn.net/pingchangtan367/article/details/30041285 tableview ...
 - Delphi动态创建组件,并释放内存
			
开发所用delphi版本是xe2,效果图如下: 代码如下: ---------------------------------------------------------------------- ...
 - this的一个作用   当前对象
			
class Person{ String name="小花"; int age=19; void eat(){ System.out.println("在吃饭" ...
 - SGU 101.Domino (欧拉路)
			
时间限制: 0.5 sec 空间限制: 4096 KB 描述 多米诺骨牌,一种用小的方的木块或其他材料,每个都被一些点在面上标记,这些木块通常被称为骨牌.每个骨牌的面都被一条线分成两个 方形,两边 ...
 - SGU 224.Little Queens
			
时间限制:0.75s 空间限制:6M 题意 n*n(n<=10)的棋盘,求出放置m(m<=n*n)个皇后的方案数. Solution: 状态压缩+位运算 搜索. 首先我们从上往下逐行放置 ...
 - 【HDU2815】【拓展BSGS】Mod Tree
			
Problem Description The picture indicates a tree, every node has 2 children. The depth of the nod ...
 - javascript——可以判断值的类型的函数
			
function classof(o){ return Object.prototype.toString.call(0).slice(8,-1); } Function.prototype.getN ...
 - Cocos2dx开发(1)——Win8.1下 NDK r10 环境搭建
			
内容简要:仅讲述NDK在Windows环境下搭建方法,至于NDK何物一概不属于本文内容,老鸟或已有环境的跳过. 笔者已安装的环境: vs2013企业版.谷歌官网adt 22.3.0(推荐)省得自己ec ...
 - 升级10.10 Yosemite 后,cocoapods 出现错误(解决方案)
			
RSMacBook-Pro:~ RS$ pod search jsonkit /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/li ...
 - web版扫雷小游戏(一)
			
作为一名程序猿,平时的爱好也不多,说起游戏,我不太喜欢大型的网游,因为太耗时间,偶尔玩玩经典的单机小游戏,比如windows下自带的游戏扫雷(秀一下,高级下最高纪录110s). 现阶段正在致力于web ...