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 ...
随机推荐
- js做全选,用一个checkbox复选框做多个checkbox复选框的全选按钮,有一个复选框未被选择时,全选按钮的checked就为false
用一个checkbox复选框做多个checkbox复选框的全选按钮,有一个复选框未被选择时,全选按钮的checked就为false,当所有checkbox都被选中时,全选按钮也被选中. 详解: 有两种 ...
- javascript的选项卡
主要用的索引值 首先 写三个按钮 <input type="button" > <input type="button" > <i ...
- Spring 创建bean的时机
默认在启动spring容器的时候,spring容器配置文件中的类就已经创建完成对象了 在<bean>中添加属性lazy-init,默认值为false. true 在c ...
- 『重构--改善既有代码的设计』读书笔记----Introduce Local Extension
同Introduce Foreign Method一样,很多时候你不能修改编辑原始类,你需要为这些服务类增加一些额外的函数,但你没有这个权限或者入口.如果你只需要一个或者两个外加函数那么你可以放心的使 ...
- 常见ORACLE错误,及解决方案(遇则即时更新)
1.当登陆时提示“ORA-03113:通信通道的文件结束”时: 解决方案: 需在X:\oraclexe\app\oracle\product\10 ...
- paramiko SSH 模块简单应用。
目的:需要ssh链接到Linux主机,执行telnet 命令,抓回显匹配制定内容. ssh --->执行telnet到本地端口--->执行类似 ls 的命令.匹配命令执行后的特定回显字段. ...
- JS设置Cookie,及COOKIE的限制
在Javascript脚本里,一个cookie 实际就是一个字符串属性.当你读取cookie的值时,就得到一个字符串,里面当前WEB页使用的所有cookies的名称和值.每个cookie除了 name ...
- Trigger model Trigger expr_id in WorkFolow
For example, suppose you want to set a Sale Order into the state "Done" once it has been s ...
- 【转】近百个Android优秀开源项目
近百个Android优秀开源项目 Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多多相当优秀的应用.其中也有许许多多的开发者提供了应用开源项目,贡献出他们的智慧 ...
- Left Mouse Button
FZU:http://acm.fzu.edu.cn/problem.php?pid=1920 题意:叫你玩扫雷游戏,已经告诉你地雷的位置了,问你最少点几次鼠标左键可以赢这盘扫雷 题解:直接DFS,(注 ...