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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 3085๋ฒˆ - ์‚ฌํƒ•๊ฒŒ์ž„ (์˜ค๋‹ต๋…ธํŠธๅฟ…)

๊ฐ์ž ๐Ÿฅ” 2022. 4. 15. 19:17
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ๋งํฌ

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

 

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

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

www.acmicpc.net

 

ํ’€์ด

  • ์ขŒ์šฐ/์ƒํ•˜ ๋กœ ์ธ์ ‘ํ•œ ์‚ฌํƒ•์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

ํ‘œ[1]

  • ํ‘œ[1]๊ณผ ๊ฐ™์ด ์‚ฌํƒ•์ด ์ฃผ์–ด์กŒ๋‹ค. ์—ฌ๊ธฐ์„œ [0][0] ์‚ฌํƒ•๊ณผ [0][1] ์‚ฌํƒ•์„ ๋ฐ”๊พธ์–ด๋ณด์ž.

ํ‘œ[2]

  • ํ‘œ[2] ์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ํ–‰๋ผ๋ฆฌ ์ธ์ ‘ํ•œ ์‚ฌํƒ•์˜ ์ตœ๊ณ  ๊ฐœ์ˆ˜๋Š” 2, ์—ด๋ผ๋ฆฌ ์ธ์ ‘ํ•œ ์‚ฌํƒ•์˜ ๊ฐœ์ˆ˜๋Š” 3์ด ๋œ๋‹ค.
  • ๋‹ค์‹œ ํ‘œ[1]๋กœ ๋Œ์•„๊ฐ€์„œ ํ‘œ[1]์—์„œ [0][0] ์‚ฌํƒ•๊ณผ [1][0] ์‚ฌํƒ•์„ ๋ฐ”๊พธ์–ด๋ณด์ž.

ํ‘œ[3]

  • ํ‘œ3์„ ๋ณด๋ฉด, ํ–‰๋ผ๋ฆฌ ์ธ์ ‘ํ•œ ์‚ฌํƒ• ์ตœ๊ณ  ๊ฐœ์ˆ˜๋Š” 2๊ฐœ, ์—ด๋ผ๋ฆฌ ์ธ์ ‘ํ•œ ์‚ฌํƒ• ์ตœ๊ณ  ๊ฐœ์ˆ˜๋Š” 1๊ฐœ ์ด๋‹ค.

์ด ์œ„์˜ ๊ณผ์ •์„ for ๋ฌธ์„ ํ†ตํ•ด์„œ ๋ฐ˜๋ณตํ•ด์ฃผ์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ธ์ ‘ํ•œ'์ตœ๋Œ€์˜ ์‚ฌํƒ• ๊ฐœ์ˆ˜'๋ฅผ ๊ตฌํ•˜๋Š” func์„ ๋งŒ๋“ค์—ˆ๋‹ค.

โ–ถ swapAt()

์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ์„œ๋กœ ๋ฐ”๊พธ์–ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค. ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พธ์–ด์ค„ ๋•Œ, ๋ณต์žกํ•˜๊ฒŒ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ  ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ธ๋ฑ์Šค ์ž๋ฆฌ์˜ ๊ฐ’๋“ค์˜ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด swapAt๊ด„ํ˜ธ ์•ˆ์— ์ธ๋ฑ์Šค ๋‘๊ฐœ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด, ๊ทธ ๋‘๊ฐœ์˜ ๊ฐ’์ด ๋ฐ”๋€Œ์–ด ์ถœ๋ ฅ๋œ๋‹ค.

var i = ["A", "B", "C"]

i.swapAt(1, 2)
print(i)
// prints ["A", "C", "B"]

https://developer.apple.com/documentation/swift/array/2893281-swapat

 

Apple Developer Documentation

 

developer.apple.com

 

๋ฌธ์ œ ํ’€์ด ์ฝ”๋“œ

  • ๋จผ์ € ํ–‰๋ผ๋ฆฌ, ์—ด๋ผ๋ฆฌ ์ธ์ ‘ํ•œ ์ตœ๊ณ  ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. 
  • ์–ด์ฐจํ”ผ ์ตœ์ข… result ๋Š” ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด result ์—  ๊ณ„์† max ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ดˆ๊ธฐํ™” ์‹œ์ผœ์ค€๋‹ค.
func row_count() {
    for i in 0..<n {
        var count = 1
        for j in 0..<n-1 {
            if input[i][j] == input[i][j+1] {
                count += 1
            } else {
                result = max(result, count)
                count = 1
            }
            result = max(result,count)
        }
    }
}

func col_count() {
    for i in 0..<n {
        var count = 1
        for j in 0..<n-1 {
            if input[j][i] == input[j+1][i] {
                count += 1
            } else {
                result = max(result, count)
                count = 1
            }
            result = max(result, count)
        }
    }
}
  • ๊ทธ ๋‹ค์Œ, main ์ฝ”๋“œ๋ฅผ ์ง ๋‹ค.
  • 2์ฐจ์› ๋ฐฐ์—ด์ด๊ธฐ ๋•Œ๋ฌธ์— for๋ฌธ ๋‘๋ฒˆ์„ ์ด์šฉํ–ˆ๊ณ , ํ–‰๋ผ๋ฆฌ ๋ฐ”๊ฟ”์„œ result๋ฅผ ๊ตฌํ•ด์ฃผ๊ณ , ์—ด๋ผ๋ฆฌ ๋ฐ”๊พธ์–ด result๋ฅผ ๊ตฌํ•ด์ฃผ์—ˆ๋‹ค.
  • ๊ทธ ๊ฒฐ๊ณผ ์ตœ์ข…์ ์œผ๋กœ result๋ฅผ ์ถœ๋ ฅํ•ด์ค€๋‹ค.
for i in 0..<n {
    for j in 0..<n-1 {
    
        // ์ž๋ฆฌ ๋ฐ”๊ฟ”์„œ result ๊ฐฑ์‹ ํ•ด์ฃผ๊ณ 
        input[i].swapAt(j,j+1)
        row_count()
        col_count()
        //๋‹ค์‹œ ์ž๋ฆฌ ๋ฐ”๊ฟ”์คŒ
        input[i].swapAt(j,j+1)
        
        //์—ด๋ผ๋ฆฌ ์ž๋ฆฌ ๋ฐ”๊ฟ€๊ฑด๋ฐ, ๊ฐ’์„ temp ์— ์ €์žฅํ•ด๋’€๋‹ค๊ฐ€ ๊ฐ’์„ ์ค˜์•ผํ•ด.
        var temp = input[j][i]
        input[j][i] = input[j+1][i]
        input[j+1][i] = temp
        
        row_count()
        col_count()
        
        // ์›๋ž˜์ž๋ฆฌ๋กœ ๋Œ๋ ค
        temp = input[j][i]
        input[j][i] = input[j+1][i]
        input[j+1][i] = temp
     
    }
}

print(result)

 

ํšŒ๊ณ 

  • ์ฒ˜์Œํ‘ธ๋Š” ์œ ํ˜€ํ˜•์ด๋ผ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ์ง€ ๋ชฐ๋ผ์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ’€์—ˆ๋‹ค.
  • ์ถ”ํ›„ ๋‹ค์‹œ ์˜ค๋‹ต๋…ธํŠธ๋กœ ๋‹ค๋ฃฐ ์˜ˆ์ •์ด๋‹ค!!
  • ์™„์ „ํƒ์ƒ‰์ด ์ง„์งœ ํ•˜๋‚˜ํ•˜๋‚˜ ๋‹ค ํ•ด๋ณด๋Š” ๋ฐฉ์‹์ด๊ตฌ๋‚˜!

 

 

๋ฐ˜์‘ํ˜•