Swift Data Structure And Algorithm/Brute-Force Algorithm

bingo

youngjaeLee1026 2022. 2. 15. 16:03

1. ๋ฌธ์ œ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-02-15 15 59 09แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-02-15 15 59 18

2. ์ž…์ถœ๋ ฅ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-02-15 15 59 25

3. ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-02-15 15 59 34

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