Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 3085๋ฒˆ - ์‚ฌํƒ•๊ฒŒ์ž„ (๋‘๋ฒˆ์งธ ํ’€์ด)

๊ฐ์ž ๐Ÿฅ” 2022. 12. 28. 16:24
๋ฐ˜์‘ํ˜•

๐Ÿ”– TIL - swapAt(_ i: ,_  j: )

๋ฐฐ์—ด ๋‚ด๋ถ€์—์„œ ๋‘ ๊ฐœ์˜ ์›์†Œ์˜ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊ฟ”์•ผํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค. ๊ทธ๋Ÿด๋•Œ ๋ฌผ๋ก  ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ๋„ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 
temp๋ผ๋Š” ์ž„์‹œ ๋ณ€์ˆ˜์— ํ˜„์žฌ ์œ„์น˜์˜ ๊ฐ’์„ ์ €์žฅํ•ด๋‘” ํ›„, ๋ฐ”๋€ ์ž๋ฆฌ์— temp๋ฅผ ๋„ฃ์–ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

var array = [1, 2, 3]
for i in 0..<2 {ใ„ท
	var temp = array[i]
    array[i] = array[i+1]
    array[i+1] = temp
}

๋ฌผ๋ก , 2์ฐจ์› ๋ฐฐ์—ด์—์„œ ๋‹ค๋ฅธ ๋ฐฐ์—ด๋ผ๋ฆฌ์˜ ๊ฐ’ ๋ฐ”๊ฟˆ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค. ํ•˜์ง€๋งŒ, ํ•œ ๋ฐฐ์—ด ์•ˆ์—์„œ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พธ๋Š” ๋ฐฉ์‹์€ ๊ตณ์ด ์ด๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š๊ณ  swapAt() ์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์“ฐ๋ฉด ๊ฐ„๋‹จํ•˜๋‹ค.

var array = [1,2,3]
array.swayAt(1, 2)

์ด๋ ‡๊ฒŒ ํ•˜๊ฒŒ ๋˜๋ฉด, index๊ฐ€ 1์ธ ๊ฒƒ๊ณผ 2์ธ๊ฒƒ์˜ ์œ„์น˜๊ฐ€ ๋ฐ”๋€๋‹ค. ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” 2์™€ 3์˜ ์œ„์น˜๊ฐ€ ๋ฐ”๋€Œ๊ฒ ์ง€? 

 

๐ŸŸ  ๋ฌธ์ œ

https://www.acmicpc.net/problem/3085

 

3085๋ฒˆ: ์‚ฌํƒ• ๊ฒŒ์ž„

์˜ˆ์ œ 3์˜ ๊ฒฝ์šฐ 4๋ฒˆ ํ–‰์˜ Y์™€ C๋ฅผ ๋ฐ”๊พธ๋ฉด ์‚ฌํƒ• ๋„ค ๊ฐœ๋ฅผ ๋จน์„ ์ˆ˜ ์žˆ๋‹ค.

www.acmicpc.net

 

๐ŸŸ  ๋‚˜์˜ ํ’€์ด

์•„๋‹ˆ ์™œ,, ๋„๋Œ€์ฒด ์™œ ์ด๋Ÿฐ ๋‘๋ ค์›€์ด ์ƒ๊ฒผ๋Š”์ง€ ๋ชจ๋ฅด๊ฒŸ๋Š”๋ฐ, ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๋ฉด ์•ˆ๋œ๋‹ค๋Š” ์ด์ƒํ•œ ๊ฐ•๋ฐ•์ด ์ƒ๊ฒผ๋‹ค. ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ๊ฒฐ์ฝ” ๊ธธ์ด์™€ ๊ด€๋ จ๋˜๋Š”๊ฒŒ ์•„๋‹Œ๋ฐ ์™œ ์ด๋Ÿฐ ๋‘๋ ค์›€์ด ์ƒ๊ฒผ์„๊นŒ ์–ด์•„์•„ ์•„์•„์•„ ๋จธ๋ฆฌ์•„ํŒŒ! ์ผ๋‹จ ํ’€์–ด๋ณด์ž.

๋†€๋ž๊ฒŒ๋„ ์ฒ˜์Œ ํ•œ ํ’€์ด์™€ ๋˜ ๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ์ž‘์„ฑ๋˜์—ˆ๋‹ค. (์ฒซ๋ฒˆ์งธ ํ’€์ด)

๋จผ์ €, ์˜ˆ์‹œ๋ฅผ 2์ฐจ์› ๋ฐฐ์—ด๋กœ ์ž…๋ ฅ ๋ฐ›์„ ๊ฒƒ์ด๋‹ค. ์˜ˆ์ œ1๋ฒˆ์„ ๋ณด๋ฉด, ํ•ด๋‹น ์ž…๋ ฅ์€ [[C, C, P], [C, C, P], [P, P, C]] ์ด๋ ‡๊ฒŒ ์ž…๋ ฅ์ด ๋  ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์„œ ํ–‰ ๋ฐฉํ–ฅ์ด๋“ , ์—ด ๋ฐฉํ–ฅ์ด๋“  ์ž๋ฆฌ๋ฅผ ํ•œ๋ฒˆ๋งŒ ๋ฐ”๊พธ๊ณ , ์ธ์ ‘ํ•œ ๊ฒƒ๋“ค์˜ max ๊ฐ’์„ ์ฐพ์•„์ฃผ๋ฉด ๋œ๋‹ค. ๋ณต์žกํ•˜์ง€ ์•Š์•˜๋˜๊ฒŒ, ์ธ์ ‘ํ•œ ๋†ˆ๋“ค์„ ์„ธ์–ด์ค„ ๋•Œ๋„ ํ–‰๋ฐฉํ–ฅ๊ณผ ์—ด๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ์ง„ํ–‰๋˜์–ด ๊ฐฏ์ˆ˜๊ฐ€ ์„ธ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ„๋‹จํ–ˆ๋‹ค.

  • for๋ฌธ์„ ์ด์šฉํ•ด์„œ ์ฐจ๋ก€๋Œ€๋กœ ํ–‰๋ผ๋ฆฌ ๋ฐ”๊ฟ”์ค€๋‹ค. (ํ˜„์žฌ ๊ธฐ์ค€ ์˜ค๋ฅธ์ชฝ ์˜†์—์žˆ๋Š” ์‚ฌํƒ•๊ณผ ์ž๋ฆฌ ๋ฐ”๊ฟ”์คŒ)
  • ํ–‰๋ผ๋ฆฌ ์ธ์ ‘ํ•œ ์• ๋“ค์˜ ๊ฐฏ์ˆ˜๋ฅผ ์„ธ์–ด๋ณธ๋‹ค. 
  • ์—ด๋ผ๋ฆฌ ์ธ์ ‘ํ•œ ์• ๋“ค์˜ ๊ฐฏ์ˆ˜๋ฅผ ์„ธ์–ด๋ณธ๋‹ค.
  • ๋‹ค์‹œ ์ž๋ฆฌ๋ฅผ ์›๋ž˜๋Œ€๋กœ ๋Œ๋ ค์ค€๋‹ค
  • ๋‹ค์‹œ ์—ด๋ผ๋ฆฌ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊ฟ”์ค€๋‹ค. (์œ„์•„๋ž˜ ์œ„์น˜ํ•œ ์‚ฌํƒ•๋“ค์˜ ์œ„์น˜๋ฅผ ๋ฐ”๊ฟ”์คŒ)
  • ํ–‰๋ผ๋ฆฌ ์ธ์ ‘ํ•œ ์• ๋“ค์˜ ๊ฐฏ์ˆ˜๋ฅผ ์„ธ์–ด๋ณธ๋‹ค.
  • ์—ด๋ผ๋ฆฌ ์ธ์ ‘ํ•œ ์• ๋“ค์˜ ๊ฐฏ์ˆ˜๋ฅผ ์„ธ์–ด๋ณธ๋‹ค.
  • ๋‹ค์‹œ ์›๋ž˜๋Œ€๋กœ ์ž๋ฆฌ๋ฅผ ๋Œ๋ ค๋†”์ค€๋‹ค.
  • (๋ฐ˜๋ณต)

์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋‹ค!

๐ŸŸ  ์ •๋‹ต์ฝ”๋“œ

๊ฐฏ์ˆ˜๋ฅผ ์„ธ์–ด์ฃผ๋Š” ํ•จ์ˆ˜๋Š” rowCount(), colCount()๋กœ ํ•ด์„œ ๋”ฐ๋กœ ๊ตฌํ˜„ํ•ด์คฌ๋‹ค.

import Foundation

private func solution() {
    var result = [Int]()
    let n = Int(readLine()!)!
    var candy = [Array<Character>]()
    for _ in 0..<n {
        candy.append(Array(readLine()!))
    }

    for i in 0..<n {
        for j in 0..<n-1 {

            // ํ–‰๋ผ๋ฆฌ ๋ฐ”๊ฟˆ
            candy[i].swapAt(j, j+1)
            result.append(colCount(n: n, candy: candy))
            result.append(rowCount(n: n, candy: candy))
            candy[i].swapAt(j, j+1)

            // ์—ด๋ฐ”๊ฟˆ
            var temp = candy[j][i]
            candy[j][i] = candy[j+1][i]
            candy[j+1][i] = temp
            result.append(colCount(n: n, candy: candy))
            result.append(rowCount(n: n, candy: candy))
            temp = candy[j][i]
            candy[j][i] = candy[j+1][i]
            candy[j+1][i] = temp
        }
    }
    print(result.max()!)
}

private func colCount(n: Int, candy: [Array<Character>]) -> Int {
    var result = -999
    for i in 0..<n {
        var count = 1
        for j in 0..<n-1 {
            if candy[i][j] == candy[i][j+1] {
                count += 1
            } else {
                result = max(result, count)
                count = 1
            }
        }
        result = max(result, count)
    }
    return result
}

private func rowCount(n: Int, candy: [Array<Character>]) -> Int {
    var result = -999
    for i in 0..<n {
        var count = 1
        for j in 0..<n-1 {
            if candy[j][i] == candy[j+1][i] {
                count += 1
            } else {
                result = max(result, count)
                count = 1
            }
        }
        result = max(result, count)
    }
    return result
}

solution()

 

 

๐Ÿ’ฌ ๋ฒˆ์™ธ

๊ทธ๋ƒฅ colCount์™€ rowCount๊ฐ€ ๊ณ ์ž‘ i์™€ j ์˜ ์œ„์น˜๋งŒ ๋‹ค๋ฅธ๋ฐ, ์ฝ”๋“œ๊ฐ€ ์ค‘๋ณต๋˜์–ด ๋‹ค๋ฅธ ํ•จ์ˆ˜๋กœ ์กด์žฌํ•˜๋Š”๊ฒŒ ๋งˆ์Œ์— ์•ˆ๋“ค์—ˆ๋‹ค. enum์„ ํ†ตํ•ด์„œ type์„ ๋ถ„๋ฆฌํ•ด์ฃผ๊ณ , ํ•˜๋‚˜์˜ function์œผ๋กœ ํ•ฉ์น˜๋ฉด ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹์•„์ง€์ง€ ์•Š์„๊นŒ ์ƒ๊ฐ๋˜์–ด, ๊ทธ๋ƒฅ,, ๊ทธ๋ ‡๊ฒŒ ๋‹ค์‹œ ํ•ด์คฌ๋‹ค..

import Foundation

private enum type {
    case colCount
    case rowCount
}

private func solution() {
    var result = [Int]()
    let n = Int(readLine()!)!
    var candy = [Array<Character>]()
    for _ in 0..<n {
        candy.append(Array(readLine()!))
    }

    for i in 0..<n {
        for j in 0..<n-1 {

            // ํ–‰๋ผ๋ฆฌ ๋ฐ”๊ฟˆ
            candy[i].swapAt(j, j+1)
            result.append(countCloseCandy(type: type.rowCount, n: n, candy: candy))
            result.append(countCloseCandy(type: type.colCount, n: n, candy: candy))
            candy[i].swapAt(j, j+1)

            // ์—ด๋ฐ”๊ฟˆ
            var temp = candy[j][i]
            candy[j][i] = candy[j+1][i]
            candy[j+1][i] = temp
            result.append(countCloseCandy(type: type.rowCount, n: n, candy: candy))
            result.append(countCloseCandy(type: type.colCount, n: n, candy: candy))
            temp = candy[j][i]
            candy[j][i] = candy[j+1][i]
            candy[j+1][i] = temp
        }
    }
    print(result.max()!)
}

private func countCloseCandy(type: type, n: Int, candy: [Array<Character>]) -> Int {
    var result = -999
    for i in 0..<n {
        var count = 1
        for j in 0..<n-1 {
            switch type {
            case .colCount:
                if candy[i][j] == candy[i][j+1] {
                    count += 1
                } else {
                    result = max(result, count)
                    count = 1
                }
            case .rowCount:
                if candy[j][i] == candy[j+1][i] {
                    count += 1
                } else {
                    result = max(result, count)
                    count = 1
                }
            }
        }
        result = max(result, count)
    }
    return result
}

solution()

 

์ง„์งœ ๋!

๋ฐ˜์‘ํ˜•