348. Design Tic-Tac-Toe
package LeetCode_348 /** * 348. Design Tic-Tac-Toe * (Lock by leetcode) * https://www.lintcode.com/problem/design-tic-tac-toe/description * Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the following rules: 1. A move is guaranteed to be valid and is placed on an empty block. 2. Once a winning condition is reached, no more moves is allowed. 3. A player who succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game. * */ class Solution { var matrix: Array<IntArray>? = null var rows: IntArray? = null var cols: IntArray? = null var dia1 = 0 var dia2 = 0 var n = 0 fun TicTacToe(n: Int) { this.n = n matrix = Array(n, { IntArray(n) }) rows = IntArray(n) cols = IntArray(n) } fun move2(row: Int, col: Int, player: Int): Int { val value = if (player == 1) 1 else -1 rows!![row] += value cols!![col] += value //diagonal from top left to right bottom if (row == col) { dia1++ } //diagonal from top right to left bottom if (col == n - row - 1) { dia2++ } if (Math.abs(rows!![row]) == n || Math.abs(cols!![col]) == n || Math.abs(dia1) == n || Math.abs(dia2) == n) { return player } return 0 } /** 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. */ fun move(row: Int, col: Int, player: Int): Int { /* * solution 1:Time complexity: O(n*n), Space complexity:O(n*n) * */ val localMatrix = matrix localMatrix!![row][col] = player //check row var win = true for (i in localMatrix.indices) { if (localMatrix[row][i] != player) { win = false break } } if (win) { return player } //check columns win = true for (i in localMatrix.indices) { if (localMatrix[i][col] != player) { win = false break } } if (win) { return player } //check back diagonal win = true for (i in localMatrix.indices) { if (localMatrix[i][i] != player) { win = false break } } if (win) { return player } //check forward diagonal win = true for (i in localMatrix.indices) { if (localMatrix[i][localMatrix.size - i - 1] != player) { win = false break } } if (win) { return player } return 0 } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)