๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 4396 - ์ง€๋ขฐ

by YWTechIT 2021. 5. 20.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 4396 - ์ง€๋ขฐ

๋ฐฑ์ค€ 4396 - ์ง€๋ขฐ


โšก๏ธ ๋‚˜์˜ ํ’€์ด

์ƒ๋‹นํžˆ ๊ณ ์ƒํ•œ ๋ฌธ์ œ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ๋งžํžˆ๊ธฐ ์œ„ํ•ด ์ •๋‹ต์„ ์–ผ๋งˆ๋‚˜ ์ œ์ถœํ–ˆ๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค. ๋‚œ์ด๋„๊ฐ€ ์‹ค๋ฒ„ 5 ์˜€๊ธฐ ๋•Œ๋ฌธ์— ์‰ฝ๊ฒŒ ํ’€ ์ค„ ์•Œ์•˜๋Š”๋ฐ ํฐ ์ฝ” ๋‹ค์ณค๋‹ค.

 

 

์ฒ˜์Œ ์งฐ๋˜ ์ฝ”๋“œ๋Š” ์ ‘๊ทผ ์ž์ฒด๋ฅผ ์ž˜ ๋ชปํ–ˆ๋‹ค. ์ด๋•Œ๊นŒ์ง€ n*n ํ–‰๋ ฌ์„ ์ดˆ๊ธฐํ™” ์‹œํ‚ฌ๋•Œ ๋ฌด์กฐ๊ฑด 0์œผ๋กœ ํ–ˆ๋Š”๋ฐ, ๊ผญ ๊ทธ๋ ‡๊ฒŒ ํ•  ํ•„์š” ์—†๋‹ค๋Š” ์ƒ๊ฐ์ด ์ด ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ๋– ์˜ฌ๋ž๋‹ค. ์ด ๋ฌธ์ œ์— ๋‹ค๋ฅธ ๋ชจ๋“  ์ง€์ ์ด ์˜จ์ (.)์œผ๋กœ ํ‘œ์‹œ๋˜์–ด์•ผํ•˜๋Š” ์กฐ๊ฑด์—์„œ result์˜ ์ดˆ๊ธฐ๊ฐ’์„ .์œผ๋กœ ํ•ด์ฃผ๋ฉด ๋์—ˆ๋‹ค.

 

๋˜, ๋ฌธ์ œ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ๋Š”๋ฐ, ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ์นธ์ด ์—ด๋ ธ๋‹ค๋ฉด ์ง€๋ขฐ๊ฐ€ ์žˆ์œผ๋ฉด์„œ ์—ด๋ฆฐ ์นธ๋งŒ ๋ณ„ํ‘œ(*)๋กœ ํ‘œ์‹œํ•˜๋Š”๊ฒƒ์ด ์•„๋‹ˆ๊ณ , ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ๋ชจ๋“  ์นธ์ด ๋ณ„ํ‘œ(*)๋กœ ์ฒ˜๋ฆฌ๋˜์–ด์•ผํ•œ๋‹ค. ๋„์ €ํžˆ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ์งˆ๋ฌธ๊นŒ์ง€ ๋‚จ๊ฒผ๋Š”๋ฐ ๋‹ค๋ฅธ ๋ถ„์ด ์นœ์ ˆํ•˜๊ฒŒ ์•Œ๋ ค์ฃผ์…”์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค. ๊ฒฐ๊ตญ, 2์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ํ•˜๋‚˜ ๋” ์„ ์–ธํ•ด์คฌ๋‹ค.(์ „์ฒด๋Š” ์ด 4์ค‘ ๋ฐ˜๋ณต๋ฌธ์ด ๋œ๋‹ค.)

 

์ดํ•ด๊ฐ€ ์•ˆ ๋˜๋ฉด ๋‹ค์Œ ์‚ฌ์ง„์„ ๋ณด์ž. ๋‘ ๊ฐœ์˜ T.C(ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค)์ธ๋ฐ, ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๊ฒ ๋Š”๊ฐ€?

 

 

23๋ฒˆ์งธ ์ฝ”๋“œ๋„ if์™€ elif ์ค‘ ์–ด๋–ค ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ• ์ง€ ํ—ท๊ฐˆ๋ ธ๋Š”๋ฐ, ๊ฒฐ๋ก ์ ์œผ๋กœ ๋‘˜ ๋‹ค ์‚ฌ์šฉํ•ด๋„ ์ƒ๊ด€์—†๋‹ค. ์ž˜ ์ดํ•ด๊ฐ€ ์•ˆ ๋œ๋‹ค๋ฉด ์ €๋ฒˆ์— ์˜ฌ๋ ธ๋˜ ๊ธ€(/125)์„ ์ฐธ๊ณ ํ•˜์ž.

 

  1. ์ดˆ๊ธฐ ๊ฐ’์„.(์˜จ์ )์œผ๋กœ ์„ ์–ธํ•œ๋‹ค.
  2. ์ง€๋ขฐ๊ฐ€ ์—†์œผ๋ฉด์„œ ์—ด๋ฆฐ ์นธ์€ 0๊ณผ 8 ์‚ฌ์ด์— ์ˆซ์ž๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
  3. ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ์นธ์ด ์—ด๋ ธ๋‹ค๋ฉด ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ๋ชจ๋“  ์นธ์ด ๋ณ„ํ‘œ(*)๋กœ ํ‘œ์‹œ๋˜์–ด์•ผ ํ•œ๋‹ค.
  4. ๋‹ค๋ฅธ ๋ชจ๋“  ์ง€์ ์€ ์˜จ์ (.)์ด์–ด์•ผ ํ•œ๋‹ค.(1๋ฒˆ์—์„œ ์ ์šฉ ์™„๋ฃŒ)

 

๋ฌธ์ œ์—์„œ T.C๊ฐ€ ํ•œ ๊ฐœ๋ฐ–์— ์—†์–ด ๋ช‡ ๊ฐ€์ง€๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ์ œ์ผ ํ•˜๋‹จ์„ ์ฐธ๊ณ ํ•˜์ž.

n = int(input())
mine = [list(input()) for _ in range(n)]
board = [list(input()) for _ in range(n)]
result = [['.'] * n for _ in range(n)]
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

for x in range(n):
    for y in range(n):
        if mine[x][y] == '.' and board[x][y] == 'x':    # ์ง€๋ขฐ๊ฐ€ ์—†์œผ๋ฉด์„œ ์—ด๋ฆฐ ์นธ
            cnt = 0
            for i in range(8):
                nx = x + dx[i]
                ny = y + dy[i]

                if nx < 0 or ny < 0 or nx >= n or ny >= n:
                    continue

                if mine[nx][ny] == '*':
                    cnt += 1
            result[x][y] = cnt    # 19๋ฒˆ์˜ ์กฐ๊ฑด ์ ˆ์„ ํ†ต๊ณผํ•˜์ง€ ์•Š์•„๋„ cnt ์ ์šฉ

        if mine[x][y] == '*' and board[x][y] == 'x':    # ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ์นธ์ด ์—ด๋ ธ๋‹ค๋ฉด
            for a in range(n):
                for b in range(n):
                    if mine[a][b] == '*':    # ์ฒซ๋ฒˆ์งธ ์ž…๋ ฅ ์ค‘ ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ์นธ์ด๋ฉด
                        result[a][b] = '*'    # ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ๋ชจ๋“  ์นธ์„ ๋ณ„ํ‘œ๋กœ ํ‘œ์‹œ
                        
for i in range(n):
    for j in range(n):
        print(result[i][j], end='')
    print()
'''
์ž…๋ ฅ

8
...**..*
......*.
....*...
........
........
.....*..
...**.*.
.....*..
xxxx....
....xxxx
xxxx....
....xxxx
xxxx....
....xxxx
xxxx....
....xxxx

2
*.
.*
.x
x.

2
*.
.*
..
.x
'''

'''
์ถœ๋ ฅ
001**..*
....33*2
0001*...
....1100
0000....
....3*21
001**.*.
....3*21

.2
2.

*.
.*
'''
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€