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 ...
随机推荐
- oracle中所有关于时间日期的问题总结
select current_date as 当前会话时间,sysdate as 系统时间, systimestamp as 系统详细时间 from dual;
- ios-pch文件的手动添加
Xcode6添加pch文件 前言:Xcode6中不在为开发者自动创建pch文件,在pch文件中我们可以添加一些琐碎的宏定义,在项目中任何地方都可以引用,加快了编译的速度 Xcode6之后的版本都是需要 ...
- 极端气候频现 五款开发天气预报应用的API
http://www.csdn.net/article/2014-02-07/2818322-weather-forecast-api-for-developing-apps
- SpringMVC4+thymeleaf3的一个简单实例(篇三:页面参数获取)
本篇将通过示例介绍页面参数是如何传递到后台的.我们继续沿用之前搭好的程序结构,如果你不知道,请参照前两篇.为方便跳转页面,我们在首页以及zoolist.html页面都加上彼此地址的链接:首页: zoo ...
- (java)从零开始之--异常处理(以文件拷贝为例)
开发过程中避免不了对异常的处理,但是异常的处理又不能乱throw 下面是简单的抛异常处理 public static void CopyFile(String souFile,String dirFi ...
- 【POJ1442】【Treap】Black Box
Description Our Black Box represents a primitive database. It can save an integer array and has a sp ...
- skip-grant-tables的作用
skip-grant-tables:非常有用的mysql启动参数(不启动grant-tables授权表) skip-grant-tables:非常有用的mysql启动参数 介绍一个非常有用的mys ...
- Oracle数据库之视图与索引
Oracle数据库之视图与索引 1. 视图简介 视图是基于一个表或多个表或视图的逻辑表,本身不包含数据,通过它可以对表里面的数据进行查询和修改. 视图基于的表称为基表,视图是存储在数据字典里的一条SE ...
- SVN更新失败,提示locked
使用SVN更新资源时,提示locked,解决方案如下: 首先找到是哪个文件不能进行更新/提交,在本地工作区间中找到这个文件对应的目录,目录里面会有.svn文件夹,这个文件夹默认是隐藏的,需要设置文件夹 ...
- php里 \r\n换行问题
<?php echo "hello"; echo "\r\n"; echo "world"; ?> 在浏览器输出的是hello ...