成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LeetCode] 348. Design Tic-Tac-Toe

MobService / 3096人閱讀

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 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?

Solution
class TicTacToe {

    private int[] rows;
    private int[] cols;
    private int diagonal;
    private int antiDiagonal;
    private int size;
    /** Initialize your data structure here. */
    public TicTacToe(int n) {
        rows = new int[n];
        cols = new int[n];
        diagonal = 0;
        antiDiagonal = 0;
        size = 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 num = player == 1 ? 1 : -1;
        rows[row] += num;
        cols[col] += num;
        if (row == col) diagonal += num;
        if (row == size-1-col) antiDiagonal += num;
        if (Math.abs(rows[row]) == size ||
           Math.abs(cols[col]) == size ||
           Math.abs(diagonal) == size ||
           Math.abs(antiDiagonal) == size) return player;
        return 0;
    }
}

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

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/72457.html

相關(guān)文章

  • 348. Design Tic-Tac-Toe

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

    betacat 評論0 收藏0
  • 348. Design Tic-Tac-Toe

    摘要:當(dāng)有一行完全只有這兩個中的其中一個人時,的絕對值應(yīng)該等于這個數(shù)列的長度,這樣就不需要每次再掃一遍數(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 b...

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

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

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

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

    Eirunye 評論0 收藏0
  • Leetcode PHP題解--D87 705. Design HashSet

    摘要:題目鏈接題目分析設(shè)計一個哈希類。需要有添加元素函數(shù),判斷元素存在的函數(shù),移除元素函數(shù)。思路這真的沒什么好說的了我把要存的值作為數(shù)組的鍵存儲。最終代碼若覺得本文章對你有用,歡迎用愛發(fā)電資助。 D87 705. Design HashSet 題目鏈接 705. Design HashSet 題目分析 設(shè)計一個哈希類。 需要有add添加元素函數(shù),contains判斷元素存在的函數(shù),remov...

    why_rookie 評論0 收藏0

發(fā)表評論

0條評論

MobService

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<