๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“š Algorithm/9oormthon Challenge

[๊ตฌ๋ฆ„ํ†ค ์ฑŒ๋ฆฐ์ง€ - 9oormthon Challenge] Day 9 ํญํƒ„ ๊ตฌํ˜„ํ•˜๊ธฐ 2 - Python ํŒŒ์ด์ฌ ํ’€์ด

 
๊ตฌ๋ฆ„ํ†ค ์ฑŒ๋ฆฐ์ง€ 9์ผ์ฐจ - ํญํƒ„ ๊ตฌํ˜„ํ•˜๊ธฐ  2

๐Ÿ“œ  ๋ฌธ์ œ 

 

 

1๋ฒˆ๊ณผ ๊ฐ™์€ ๊ทธ๋ฆผ์—์„œ ๋งŒ์•ฝ (2, 2)์— ํญํƒ„์ด ๋–จ์–ด์ง€๋ฉด, ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํƒ์ƒ‰ํ•ด์•ผ ํ•œ๋‹ค. (3, 2)๋Š” #์ด๋ฏ€๋กœ ์•„๋ฌด๊ฒƒ๋„ ๋”ํ•˜์ง€ ์•Š๋Š”๋‹ค. (2, 3)์— ํญํƒ„์ด ๋–จ์–ด์ง€๋ฉด, ์ƒํƒœ๊ฐ€ '@'์ธ ๊ณณ์—๋Š” 2๋งŒํผ ํญํƒ„์˜ ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค. 

 

โœ๏ธ ์ž…๋ ฅ 

 

โœ๏ธ ์ถœ๋ ฅ

 

๐Ÿ’ก ํ’€์ด 

 

๋ฌธ์ œ์—์„œ '์ƒํ•˜์ขŒ์šฐ'๋ฅผ ๋ณด์ž๋งˆ์ž Graph ๊ฐ€ ์ƒ๊ฐ์ด ๋‚ฌ๋‹ค.

๋จผ์ € ํญํƒ„์ด ๋–จ์–ด์ง€๋ฉด ์ƒํ•˜์ขŒ์šฐ๋กœ ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค. ๋งŒ์•ฝ (y, x)์— ํญํƒ„์ด ๋–จ์–ด์ง€๋ฉด ์˜ํ–ฅ์€ (y-1, x), (y, x-1), (y+1, x), (y, x+1)์— ๋ฐ›๋Š”๋‹ค.

์ขŒํ‘œ๋ฅผ ์กฐ๊ธˆ ๋” ์‰ฝ๊ฒŒ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด dx = [0, 1, -1, 0, 0] , dy = [0, 0, 0, 1, -1]๋กœ ํ•˜์˜€๋‹ค. 

#๋Š” ๋ณ€ํ™”๊ฐ€ ์—†๊ณ , '0'์€ +1, '@'์€ +2์ด๋‹ค. cal ํ•จ์ˆ˜์—์„œ ์ขŒํ‘œ๋ฅผ ์กฐ์ •ํ•˜๊ณ  ์˜ํ–ฅ ๋ฐ›๋Š” ์ขŒํ‘œ๋ฅผ ๋”ฐ์ง€๋ฉฐ ํญํƒ„๊ฐ’์„ ๋”ํ•ด์ค€๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค. 

 

N, n = map(int, input().split()) 
area = [input().split() for _ in range(N)]
bomb = [[0] * N for _ in range(N)]  #ํญํƒ„๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด 0์œผ๋กœ ์„ค์ • 

#์ขŒํ‘œ๊ฐ’์„ ์‰ฝ๊ฒŒ ๊ตฌํ•˜๊ธฐ ์œ„ํ•จ
dx = [0, 1, -1, 0, 0]
dy = [0, 0, 0, 1, -1]

def cal (y, x) :
	for i in range(5) :
		ny = y + dy[i]
		nx = x + dx[i]
		if 0 <= nx < N and 0 <= ny < N :
			if area[ny][nx] == '@' : bomb[ny][nx] += 2	#'@'์ด๋ฉด 2๋งŒํผ ์˜ํ–ฅ์„ ๋ฐ›์Œ
			elif area[ny][nx] == '#' : bomb[ny][nx] += 0	#'#'์ด๋ฉด ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š์Œ
			else : bomb[ny][nx] += 1	#'0'์ด๋ฉด 1๋งŒํผ ์˜ํ–ฅ์„ ๋ฐ›์Œ
	
            
for i in range(n) :
    y, x, = map(int, input().split())
    cal(y-1, x-1)
  
max_bomb = -1

for i in bomb:
    if max(i) > max_bomb:
        max_bomb = max(i)

    
print(max_bomb)

 

 

 

728x90