亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

348. Design Tic-Tac-Toe

zhkai / 2779人閱讀

摘要:當(dāng)有一行完全只有這兩個(gè)中的其中一個(gè)人時(shí),的絕對(duì)值應(yīng)該等于這個(gè)數(shù)列的長(zhǎng)度,這樣就不需要每次再掃一遍數(shù)組。

題目:
Design a Tic-tac-toe game that is played between two players on a n x n grid.

You may assume the following rules:

A move is guaranteed to be valid and is placed on an empty block.
Once a winning condition is reached, no more moves is allowed.
A player who succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game.
Example:

Given n = 3, assume that player 1 is "X" and player 2 is "O" in the board.

TicTacToe toe = new TicTacToe(3);

toe.move(0, 0, 1); -> Returns 0 (no one wins)
|X| | |
| | | |    // Player 1 makes a move at (0, 0).
| | | |

toe.move(0, 2, 2); -> Returns 0 (no one wins)
|X| |O|
| | | |    // Player 2 makes a move at (0, 2).
| | | |

toe.move(2, 2, 1); -> Returns 0 (no one wins)
|X| |O|
| | | |    // Player 1 makes a move at (2, 2).
| | |X|

toe.move(1, 1, 2); -> Returns 0 (no one wins)
|X| |O|
| |O| |    // Player 2 makes a move at (1, 1).
| | |X|

toe.move(2, 0, 1); -> Returns 0 (no one wins)
|X| |O|
| |O| |    // Player 1 makes a move at (2, 0).
|X| |X|

toe.move(1, 0, 2); -> Returns 0 (no one wins)
|X| |O|
|O|O| |    // Player 2 makes a move at (1, 0).
|X| |X|

toe.move(2, 1, 1); -> Returns 1 (player 1 wins)
|X| |O|
|O|O| |    // Player 1 makes a move at (2, 1).
|X|X|X|

Follow up:
Could you do better than O(n2) per move() operation?

Hint:

Could you trade extra space such that move() operation can be done in O(1)?
You need two arrays: int rows[n], int cols[n], plus two variables: diagonal, anti_diagonal.

解答:
一開始其實(shí)就想到了hint, 作出了下面的解法:

public class TicTacToe {
    int[][] grid;
    int n;
    /** Initialize your data structure here. */
    public TicTacToe(int n) {
        grid = new int[n][n];
        this.n = n;
    }
    
    public boolean check(int row, int col, int len) {
        boolean hori = true, verti = true, diag1 = true, diag2 = true;
        //check horizontal
        for (int i = 0; i < len - 1; i++) {
            if (grid[row][i] != grid[row][i + 1]) {
                hori = false;
            }
        }
        //check vertical
        for (int j = 0; j < len - 1; j++) {
            if (grid[j][col] != grid[j + 1][col]) {
                verti = false;
            }
        }
        //check diagonals
        if (row == col) {
            for (int i = 0; i < len - 1; i++) {
                if (grid[i][i] != grid[i + 1][i + 1]) {
                    diag1 = false;
                }
            }
        } else {
            diag1 = false;
        }
        
        if (row + col == len - 1) {
            for (int i = 0; i < len - 1; i++) {
                if (grid[i][len - 1 - i] != grid[i + 1][len - 2 - i]) {
                    diag2 = false;
                }
            }
        } else {
            diag2 = false;
        }
        return hori || verti || diag1 || diag2;
    }
    
    /** 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) {
        grid[row][col] = player;
        if (check(row, col, n)) return player;
        return 0;
    }
}

這個(gè)解法冗余在check行個(gè)列的時(shí)候,每一次都要再掃一遍這一行這一列,所以如果只有兩個(gè)player,可以把這兩個(gè)player記作1, -1。當(dāng)有一行完全只有這兩個(gè)player中的其中一個(gè)人時(shí),sum的絕對(duì)值應(yīng)該等于這個(gè)數(shù)列的長(zhǎng)度,這樣就不需要每次再掃一遍數(shù)組。代碼如下:

public class TicTacToe {
    int[] rows, cols;
    int diagonal, antiDiagonal;
    int len;
    /** Initialize your data structure here. */
    public TicTacToe(int n) {
        rows = new int[n];
        cols = new int[n];
        this.len = 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) {
        //Take player 1 and 2 as value of 1 and -1;
        //Every time we only do adding, dont need to re-scan the whole line
        int toAdd = player == 1 ? 1 : -1;
        rows[row] += toAdd;
        cols[col] += toAdd;
        if (row == col) diagonal += toAdd;
        if (row == len - 1 - col) antiDiagonal += toAdd;
        if (Math.abs(rows[row]) == len || Math.abs(cols[col]) == len || Math.abs(diagonal) == len || Math.abs(antiDiagonal) == len) {
            return player;
        }
        return 0;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/64930.html

相關(guān)文章

  • 348. Design Tic-Tac-Toe

    摘要:題目鏈接這道題找是否有贏的方法和相似,稍微簡(jiǎn)化了。統(tǒng)計(jì)行列和兩個(gè)對(duì)角線的情況,兩個(gè)分別用和來記。然后判斷是否有一個(gè)人贏只需要的復(fù)雜度。當(dāng)然這么做的前提是假設(shè)所有的都是的,棋盤一個(gè)地方已經(jīng)被占用了,就不能走那個(gè)地方了。 348. Design Tic-Tac-Toe 題目鏈接:https://leetcode.com/problems... 這道題找是否有player贏的方法和N-Que...

    betacat 評(píng)論0 收藏0
  • [LeetCode] 348. Design Tic-Tac-Toe

    Problem Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the following rules: A move is guaranteed to be valid and is placed on an empty block.Once a winnin...

    MobService 評(píng)論0 收藏0
  • Minimax 和 Alpha-beta 剪枝算法簡(jiǎn)介,以及以此實(shí)現(xiàn)的井字棋游戲(Tic-tac-t

    摘要:我們?cè)谇拔闹锌紤]的那張圖就來自這篇文章,之后我們會(huì)用剪枝算法來改進(jìn)之前的解決方案。剪枝算法的實(shí)現(xiàn)接下來討論如何修改前面實(shí)現(xiàn)的算法,使其變?yōu)榧糁λ惴ā,F(xiàn)在我們已經(jīng)有了現(xiàn)成的和剪枝算法,只要加上一點(diǎn)兒細(xì)節(jié)就能完成這個(gè)游戲了。 前段時(shí)間用 React 寫了個(gè)2048 游戲來練練手,準(zhǔn)備用來回顧下 React 相關(guān)的各種技術(shù),以及試驗(yàn)一下新技術(shù)。在寫這個(gè)2048的過程中,我考慮是否可以在其中加...

    wemall 評(píng)論0 收藏0
  • Minimax 和 Alpha-beta 剪枝算法簡(jiǎn)介,以及以此實(shí)現(xiàn)的井字棋游戲(Tic-tac-t

    摘要:我們?cè)谇拔闹锌紤]的那張圖就來自這篇文章,之后我們會(huì)用剪枝算法來改進(jìn)之前的解決方案。剪枝算法的實(shí)現(xiàn)接下來討論如何修改前面實(shí)現(xiàn)的算法,使其變?yōu)榧糁λ惴ā,F(xiàn)在我們已經(jīng)有了現(xiàn)成的和剪枝算法,只要加上一點(diǎn)兒細(xì)節(jié)就能完成這個(gè)游戲了。 前段時(shí)間用 React 寫了個(gè)2048 游戲來練練手,準(zhǔn)備用來回顧下 React 相關(guān)的各種技術(shù),以及試驗(yàn)一下新技術(shù)。在寫這個(gè)2048的過程中,我考慮是否可以在其中加...

    Eirunye 評(píng)論0 收藏0
  • 一次詭異的docker錯(cuò)誤調(diào)試

    摘要:源自小伙伴的求助,雖然沒能定位到最終的原因,調(diào)試的過程也比較有意思緣起小伙伴求助我,同一個(gè)鏡像在測(cè)試機(jī)器上可以運(yùn)行,在阿里云上運(yùn)行提示用戶不存在。 源自小伙伴的求助,雖然沒能定位到最終的原因,調(diào)試的過程也比較有意思 緣起 小伙伴求助我,同一個(gè)docker鏡像在測(cè)試機(jī)器上可以運(yùn)行,在阿里云上運(yùn)行提示用戶不存在。 在阿里云上運(yùn)行提示如下: # docker run --rm -it ima...

    Jeff 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<