2021. 8. 3. 23:56ใ๐ฑ Algorithm
๐ ์ ๊ทผ
์ฐ์ ์ธ๊ฐ์ง ํจ์๋ก ๋๋์ด ์ ๊ทผํ์ต๋๋ค.
1) ๊ฐ matrix๋ฅผ ์ํ
2) ์ํ๋ฅผ ํ๋ฉด์ ๋๊ธฐ์ค์ ๋ฒ์ ๋ด๋ถ๋ง ํ์ํ๋๋ก
3) ๊ฑฐ๋ฆฌ๋๊ธฐ๋ฅผ ์ง์ผฐ๋์ง -> main ์ด ๋๋ ์๊ณ ๋ฆฌ์ฆ
์ฝ๋
1) ๊ฐ matrix๋ฅผ ์ํ: solution(places)
def solution(places):
answer = []
entire_matrix = [(i, j) for i in range(5) for j in range(5)]
for place in places:
for r, c in entire_matrix:
if place[r][c] == 'P' and not check(r, c, place):
answer.append(0)
break
else:
answer.append(1)
return answer
๋๊ธฐ์ค์ ๊ฐ ์์น๋ฅผ ์ขํ๋ก ๊ฐ๋ ๋ฐฐ์ด์ ์์ฑํฉ๋๋ค : entire_matrix
# entire_matrix
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4)]
๊ทธ๋ฆฌ๊ณ for ๋ฌธ์ ํตํด ์ฐจ๋ก๋๋ก places ๋ฅผ ์ํํฉ๋๋ค.
entire_matrix์ ์์๋ฅผ ์ขํ๋ก ์ฌ์ฉํ์ฌ, P๊ฐ ์กด์ฌํ๋ฉด์, ๊ฑฐ๋ฆฌ๋๊ธฐ๋ฅผ ์งํค๋์ง ์ฌ๋ถ๋ฅผ ํ์ธํฉ๋๋ค.
๊ฑฐ๋ฆฌ๋๊ธฐ์ ์ฌ๋ถ๋ฅผ ํ์ธํ๋ ํจ์๋ check()๋ก ๋ช ๋ช ํ์์ต๋๋ค.
for-else ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ, for ๋ฌธ์ ๋ชจ๋ ํต๊ณผํ๋ฉด ๊ฑฐ๋ฆฌ๋๊ธฐ๋ฅผ ์ ๋๋ก ์ง์ผฐ๋ค๋ ๋ป์ด๋ฏ๋ก else ๊ตฌ๋ฌธ์์ append(1)์ ํฉ๋๋ค.
2) ๋๊ธฐ์ค์ ๋ฒ์ ๋ด๋ถ๋ง ํ์: in_range()
def in_range(x, y):
SIZE = 5
return -1 < x < SIZE and -1 < y < SIZE
3) ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธ: check()
์ผ์ด์ค ๋ณ๋ก ๋๋์ด ์๊ฐํด์ผํฉ๋๋ค.
1๏ธโฃ P ๋ฅผ ๊ธฐ์ค์ผ๋ก, ๋์๋จ๋ถ ํ์นธ์ฉ์ ๋ฒ์ โก๏ธ ์ด ๋ฒ์์ ๋ ๋ค๋ฅธ P ๊ฐ ์๋ค๋ฉด ๋ฌด์กฐ๊ฑด False
2๏ธโฃ P ๋ฅผ ๊ธฐ์ค์ผ๋ก, ๋๊ฐ์ ์ผ๋ก ํ์นธ ๋ฒ์
3๏ธโฃ P ๋ฅผ ๊ธฐ์ค์ผ๋ก, ๋์๋จ๋ถ ๋์นธ์ฉ์ ๋ฒ์ โก๏ธ ๋ ์นธ ๋จ์ด์ง ๊ณณ์ P ๊ฐ ์์ ๋, ๊ธฐ์ค์ ๊ณผ์ ์ฌ์ด์ "X"๊ฐ ์๋ค๋ฉด False
def check(r, c, p):
#1
dist = [(1, 0), (0, 1), (-1, 0), (0, -1)]
for dx, dy in dist:
nx, ny = r + dx, c + dy
if in_range(nx, ny) and p[nx][ny] == 'P':
return False
#2
dist = [(-1, -1), (-1, 1), (1, -1), (1, 1)]
for dx, dy in dist:
nx, ny = r + dx, c + dy
if in_range(nx, ny) and p[nx][ny] == "P" and (p[r][ny] != "X" or p[nx][c] != "X"):
return False
#3
dist = [(2, 0), (0, 2), (-2, 0), (0, -2)]
for dx, dy in dist:
nx, ny = r + dx, c + dy
if in_range(nx, ny) and p[nx][ny] == "P" and p[r + dx // 2][c + dy // 2] != "X":
return False
return True
๋ชจ๋ ์ผ์ด์ค๋ฅผ ๋ค ํต๊ณผํ๋ฉด ๋ง์ง๋ง์ True ๋ฅผ ๋ฆฌํดํฉ๋๋ค. ์ด๋ ์ธ๊ทผ ๋ฒ์ ๋ด์์ ๊ฑฐ๋ฆฌ๋๊ธฐ๊ฐ ์ ์ง์ผ์ก์์ ์๋ฏธํฉ๋๋ค.
๐ ์ ์ฒด ์ฝ๋
def in_range(x, y):
SIZE = 5
return -1 < x < SIZE and -1 < y < SIZE
def check(r, c, p):
dist = [(1, 0), (0, 1), (-1, 0), (0, -1)]
for dx, dy in dist:
nx, ny = r + dx, c + dy
if in_range(nx, ny) and p[nx][ny] == 'P':
return False
dist = [(-1, -1), (-1, 1), (1, -1), (1, 1)]
for dx, dy in dist:
nx, ny = r + dx, c + dy
if in_range(nx, ny) and p[nx][ny] == "P" and (p[r][ny] != "X" or p[nx][c] != "X"):
return False
dist = [(2, 0), (0, 2), (-2, 0), (0, -2)]
for dx, dy in dist:
nx, ny = r + dx, c + dy
if in_range(nx, ny) and p[nx][ny] == "P" and p[r + dx // 2][c + dy // 2] != "X":
return False
return True
def solution(places):
answer = []
entire_matrix = [(i, j) for i in range(5) for j in range(5)]
for place in places:
for r, c in entire_matrix:
if place[r][c] == 'P' and not check(r, c, place):
answer.append(0)
break
else:
answer.append(1)
return answer
'๐ฑ Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค [lv2] ๋ฐฉ๋ฌธ๊ธธ์ด ํ์ด์ฌ (0) | 2021.08.29 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค [lv2] ๋ฉ๋ด ๋ฆฌ๋ด์ผ ํ์ด์ฌ (0) | 2021.08.29 |
ํ๋ก๊ทธ๋๋จธ์ค [lv2] ๋ฐฐ๋ฌ ํ์ด์ฌ (0) | 2021.07.28 |
ํ๋ก๊ทธ๋๋จธ์ค [lv2] ํ์ผ๋ช ์ ๋ ฌ ํ์ด์ฌ (0) | 2021.07.25 |
ํ๋ก๊ทธ๋๋จธ์ค [lv1] ์ ๊ท์์ด๋์ถ์ฒ (0) | 2021.07.22 |