1. ๋ฌธ์


2. ์ ์ถ๋ ฅ

3. ์ ์ถ๋ ฅ ์์

4. ๋ฌธ์ ์ค๊ณ
์ฌํ์์ ๋ฒํธ๋ฅผ ์ ๋ ฅ ๋ฐ์ ๋ ๋ง๋ค ๊ฐ์ ์ฆ๊ฐ ์์ผ์ฃผ๊ณ , ํด๋น ๋ฒํธ๋ฅผ ๋น๊ณ ํ์์ ์ง์ด ํ ๋๊ฐ์ , ๋ฐ๋๊ฐ์ , ๊ฐ๋ก, ์ธ๋ก ๋ณ๋ก ๋น๊ณ ์ ๊ฐ์๋ฅผ ์์ ํ์ ํ์ฌ ๊ทธ ์๊ฐ 3์ด์ ์ผ ๊ฒฝ์ฐ ๊ฒ์ฌ๋ฅผ ๋ฉ์ถ๊ณ ๋ต์ ์ถ๋ ฅํจ.
5. ์ ์ฒด ์ฝ๋
//MARK: - Bingo
//MARK: - Frameworks
import Foundation
//MARK: - Types
struct Bingo {
//MARK: - Properties
var row: Int
var column: Int
var bingoBoard: Array<Array<Int>>
//MARK: - Initializer
init() {
self.row = 5
self.column = 5
self.bingoBoard = []
}
}
//MARK: - Functions
func removeNumber(_ board: inout Array<Array<Int>>, _ toRemoveNumber: Int) -> Void {
for i in 0..<board.count {
for j in 0..<board[i].count {
board[i][j] = board[i][j] == toRemoveNumber ? 0 : board[i][j]
}
}
}
func checkDiagonal(_ board: inout Array<Array<Int>>) -> Int {
var zeroCount: Int = 0
for i in 0..<board.count {
zeroCount += board[i][i] == 0 ? 1 : 0
}
return zeroCount == 5 ? 1 : 0
}
func checkDiagonalTwo(_ board: inout Array<Array<Int>>) -> Int {
var zeroCount: Int = 0
for i in 0..<board.count {
zeroCount += board[i][(board.count - 1) - i] == 0 ? 1 : 0
}
return zeroCount == 5 ? 1 : 0
}
func checkRow(_ board: inout Array<Array<Int>>, _ bingoCount: Int) -> Int {
var result: Int = bingoCount
for i in 0..<board.count {
var zeroCount: Int = 0
for j in 0..<board[i].count {
zeroCount += board[i][j] == 0 ? 1 : 0
}
if result >= 3 {
break
}
if zeroCount == 5 {
result += 1
}
}
return result
}
func checkColumn(_ board: inout Array<Array<Int>>, _ bingoCount: Int) -> Int {
var result: Int = bingoCount
for i in 0..<board.count {
var zeroCount: Int = 0
for j in 0..<board[i].count {
zeroCount += board[j][i] == 0 ? 1 : 0
}
if result >= 3 {
break
}
if zeroCount == 5 {
result += 1
}
}
return result
}
func solution() -> Void {
//MARK: - input
var bingo: Bingo = Bingo()
var callNumberCount: Int = 0
for _ in 0..<bingo.row {
guard let input = readLine()?.components(separatedBy: " ") else { return }
bingo.bingoBoard.append(input.map{ Int($0) ?? 0 })
}
//MARK: - process & output
for _ in 0..<bingo.row {
guard let inputCallNumber = readLine()?.components(separatedBy: " ") else { return }
let callNumbers = inputCallNumber.map{ Int($0) ?? 0 }
for callNumber in callNumbers {
var bingoCount: Int = 0
callNumberCount += 1
//์ซ์๋ฅผ ์ง์
removeNumber(&bingo.bingoBoard, callNumber)
//๋น๊ณ ๊ฒ์ฌ(๋๊ฐ์ -> ๋ฐ๋๊ฐ์ -> ๊ฐ๋ก -> ์ธ๋ก)
bingoCount += checkDiagonal(&bingo.bingoBoard)
bingoCount += checkDiagonalTwo(&bingo.bingoBoard)
bingoCount = checkRow(&bingo.bingoBoard, bingoCount)
if bingoCount >= 3 {
print("\n\n\(callNumberCount)")
return
}
bingoCount = checkColumn(&bingo.bingoBoard, bingoCount)
if bingoCount >= 3 {
print("\n\n\(callNumberCount)")
return
}
}
}
}
solution()
์ ์ฒด์ฝ๋๋ ์ฌ๊ธฐ์์ ํ์ธํ ์ ์์ต๋๋ค.
'Swift Data Structure And Algorithm > Brute-Force Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| baseBall game (0) | 2022.02.20 |
|---|---|
| tetris (0) | 2022.02.19 |
| ํ๋ ฌ ๋ค์ง๊ธฐ (0) | 2022.01.29 |
| ํ๋ ฌ ๋ค์ง๊ธฐ 2 (0) | 2022.01.29 |
| ์์ ๊พธ๋ฏธ๊ธฐ (0) | 2022.01.29 |