You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package class34;
public class Problem_0348_DesignTicTacToe {
class TicTacToe {
private int[][] rows;
private int[][] cols;
private int[] leftUp;
private int[] rightUp;
private boolean[][] matrix;
private int N;
public TicTacToe(int n) {
// rows[a][1] : 1这个人在a行上下了几个
// rows[b][2] : 2这个人在b行上下了几个
rows = new int[n][3]; //0 1 2
cols = new int[n][3];
// leftUp[2] = 7 : 2这个人在左对角线上下了7个
leftUp = new int[3];
// rightUp[1] = 9 : 1这个人在右对角线上下了9个
rightUp = new int[3];
matrix = new boolean[n][n];
N = n;
}
public int move(int row, int col, int player) {
if (matrix[row][col]) {
return 0;
}
matrix[row][col] = true;
rows[row][player]++;
cols[col][player]++;
if (row == col) {
leftUp[player]++;
}
if (row + col == N - 1) {
rightUp[player]++;
}
if (rows[row][player] == N || cols[col][player] == N || leftUp[player] == N || rightUp[player] == N) {
return player;
}
return 0;
}
}
}