ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Kotlin ๐Ÿฌ ๋ฐฑ์ค€ 12๋‹จ๊ณ„ :: 1018 ๋ฒˆ
    2023. 6. 24. 23:46
    ๋ฐ˜์‘ํ˜•

    ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ

    ๋ฌธ์ œ   |

      ์ง€๋ฏผ์ด๋Š” ์ž์‹ ์˜ ์ €ํƒ์—์„œ MN๊ฐœ์˜ ๋‹จ์œ„ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋Š” M×N ํฌ๊ธฐ์˜ ๋ณด๋“œ๋ฅผ ์ฐพ์•˜๋‹ค. ์–ด๋–ค ์ •์‚ฌ๊ฐํ˜•์€ ๊ฒ€์€์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ๊ณ , ๋‚˜๋จธ์ง€๋Š” ํฐ์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ๋‹ค. ์ง€๋ฏผ์ด๋Š” ์ด ๋ณด๋“œ๋ฅผ ์ž˜๋ผ์„œ 8×8 ํฌ๊ธฐ์˜ ์ฒด์ŠคํŒ์œผ๋กœ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค.

      ์ฒด์ŠคํŒ์€ ๊ฒ€์€์ƒ‰๊ณผ ํฐ์ƒ‰์ด ๋ฒˆ๊ฐˆ์•„์„œ ์น ํ•ด์ ธ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๊ตฌ์ฒด์ ์œผ๋กœ, ๊ฐ ์นธ์ด ๊ฒ€์€์ƒ‰๊ณผ ํฐ์ƒ‰ ์ค‘ ํ•˜๋‚˜๋กœ ์ƒ‰์น ๋˜์–ด ์žˆ๊ณ , ๋ณ€์„ ๊ณต์œ ํ•˜๋Š” ๋‘ ๊ฐœ์˜ ์‚ฌ๊ฐํ˜•์€ ๋‹ค๋ฅธ ์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ์ •์˜๋ฅผ ๋”ฐ๋ฅด๋ฉด ์ฒด์ŠคํŒ์„ ์ƒ‰์น ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๋‘ ๊ฐ€์ง€๋ฟ์ด๋‹ค. ํ•˜๋‚˜๋Š” ๋งจ ์™ผ์ชฝ ์œ„ ์นธ์ด ํฐ์ƒ‰์ธ ๊ฒฝ์šฐ, ํ•˜๋‚˜๋Š” ๊ฒ€์€์ƒ‰์ธ ๊ฒฝ์šฐ์ด๋‹ค.

     ๋ณด๋“œ๊ฐ€ ์ฒด์ŠคํŒ์ฒ˜๋Ÿผ ์น ํ•ด์ ธ ์žˆ๋‹ค๋Š” ๋ณด์žฅ์ด ์—†์–ด์„œ, ์ง€๋ฏผ์ด๋Š” 8×8 ํฌ๊ธฐ์˜ ์ฒด์ŠคํŒ์œผ๋กœ ์ž˜๋ผ๋‚ธ ํ›„์— ๋ช‡ ๊ฐœ์˜ ์ •์‚ฌ๊ฐํ˜•์„ ๋‹ค์‹œ ์น ํ•ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. ๋‹น์—ฐํžˆ 8*8 ํฌ๊ธฐ๋Š” ์•„๋ฌด๋ฐ์„œ๋‚˜ ๊ณจ๋ผ๋„ ๋œ๋‹ค. ์ง€๋ฏผ์ด๊ฐ€ ๋‹ค์‹œ ์น ํ•ด์•ผ ํ•˜๋Š” ์ •์‚ฌ๊ฐํ˜•์˜ ์ตœ์†Œ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

     

     

    ํ’€์ด  |

      ์น ํ•ด์•ผํ•˜๋Š” ์นธ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑ(check() ํ•จ์ˆ˜)ํ•œ๋‹ค. ์ดํ›„ ์ฒด์ŠคํŒ์„ 8 x 8 ํฌ๊ธฐ๋กœ ๋‚˜๋ˆ„์–ด ์‹œ์ž‘์ด 'W'์ผ๋•Œ, 'B'์ผ๋•Œ์˜ ์น ํ•ด์•ผํ•˜๋Š” ์นธ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ชจ๋‘ ์ €์žฅํ•œ ํ›„ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

     

     

    ๋‹ต์•ˆ  |

    import java.util.Scanner
    
    fun main() = with(Scanner(System.`in`)) {
        val n = nextInt()
        val m = nextInt()
        
        val board = mutableListOf<String>()
        repeat(n) {
            board.add(next())
        }
        
        fun check(x: Int, y: Int, z: List<Char>): Int {
        	var count = 0
        	for (i in 0 until 8) {
            	for (j in 0 until 8) {
                	val currentChar = board[i + x][j + y]
                	if ((i % 2 == 0 && j % 2 == 0 && currentChar == z[0]) ||
                        (i % 2 == 0 && j % 2 != 0 && currentChar == z[1]) ||
                        (i % 2 != 0 && j % 2 == 0 && currentChar == z[1]) ||
                        (i % 2 != 0 && j % 2 != 0 && currentChar == z[0])
                	) {
                        continue
                	}
                	count += 1
                }
            }
        
            return count
        }
        
        val result = mutableListOf<Int>()
        for (i in 0 until (n - 7)) {
            for (j in 0 until (m - 7)) {
                result.add(check(i, j, listOf('W', 'B')))
                result.add(check(i, j, listOf('B', 'W')))
            }
        }
    
        println(result.minOrNull())
    }
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.