https://school.programmers.co.kr/learn/courses/30/lessons/84021
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก์ Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
1. ์ ๊ทํํ๋ ์์ด๋์ด
2. ํ์ ํ ๋ ์ขํ ์ด๋ป๊ฒ ๋๋์ง
์ด ๋ ๋ถ๋ถ์์ ๊ฐ์ ๋ชป ์ก์์ gptํํ ๋ฌผ์ด๋ดค๋ค
์ ๋๋ก ํผ์ ํ์ด๋ด๊ธฐ๋ฅผ ๊ฑฐ์ 1์๊ฐ 30๋ถ ๊ฑธ๋ ธ๋ค.. ๋๋ฌด ์ด๋ ค์
def dfs(x, y, board, value, path):
board[x][y] = 2 # ๋ฐฉ๋ฌธ ํ์
path += [(x,y)]
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if 0 <= nx < len(board) and 0 <= ny < len(board):
if board[nx][ny] == value:
dfs(nx, ny, board, value, path)
def normalize(list_n):
min_x, min_y = 100, 100
for x, y in list_n:
min_x = min(x,min_x)
min_y = min(y,min_y)
list_new = [(x-min_x, y-min_y) for x,y in list_n]
return sorted(list_new)
def move(list_a, list_b):
# normalization
norm_list_a, norm_list_b = normalize(list_a), normalize(list_b)
return (norm_list_a == norm_list_b)
def rotate(list_a, list_b):
# normalization
norm_list_a, norm_list_b = normalize(list_a), normalize(list_b)
# 90๋ ํ์ : (x, y) -> (y, -x)
# 180๋ ํ์ :(x, y) -> (-x, -y)
# 270๋ ํ์ :(x, y) -> (-y, x)
norm_list_b_90 = []
norm_list_b_180 = []
norm_list_b_270 = []
for (x, y) in norm_list_b:
norm_list_b_90.append((y,-x))
norm_list_b_180.append((-x, -y))
norm_list_b_270.append((-y, x))
norm_list_b_90 = normalize(norm_list_b_90)
norm_list_b_180 = normalize(norm_list_b_180)
norm_list_b_270 = normalize(norm_list_b_270)
return (norm_list_a == norm_list_b_90 or norm_list_a == norm_list_b_180 or norm_list_a == norm_list_b_270)
def find_answer(gb_list, table_list):
answer = 0
visited_gb = [False]*len(gb_list)
visited_t = [False]*len(table_list)
for idx_gb, gb in enumerate(gb_list):
for idx_t, t in enumerate(table_list):
if len(gb) == len(t):
if (move(gb,t) or rotate(gb,t)) and (not visited_gb[idx_gb] and not visited_t[idx_t]): # ํํ์ด๋์ด ๊ฐ๋ฅํ๊ฑฐ๋ ํ์ ์ด ๊ฐ๋ฅํ ๋, ๊ทธ๋ฆฌ๊ณ ๋ฐฉ๋ฌธ x
answer += len(gb)
visited_gb[idx_gb] = True; visited_t[idx_t] = True
break
return answer
def solution(game_board, table):
# ๊ฒ์๋ณด๋์ ๋น์นธ(0)์ table์ ์นธ(1)์ ๋ฃ๋ ๊ณผ์
# ์์ ๋์ผํ๊ฒ ์ฑ์ธ ์๋ ์๊ณ
# ์กฐ๊ฐ์ ์ฌ๋ฌ๊ฐ๋ก ์ฑ์ธ ์๋ ์์ <-- ์ด๊ฑฐ๊น์ง ๊ตฌํ ๋ชปํ๊ฒ ์
# ์ด๋ค ๋ฐฉ๋ฒ์ด ๋ ๋์์ง๋.. ์ค์ ๋ช์นธ์ ์ฑ์ ๋์ง ๊ฒฐ๊ณผ์ ๋ฐ๋ผ ๋ค๋ฅผ ๊ฒ ๊ฐ์
# ์ฒ์์ ํด์ผ ํ ์ผ์, (dfs ์ ๊ทผ)
# game board >> 0 ๊ทธ๋ฃน ์ฐพ๊ธฐ
# table >> 1 ๊ทธ๋ฃน ์ฐพ๊ธฐ
# ์ผ๋ฐ์ ์ผ๋ก game board์ ๊ทธ๋ฃน์ด table ๊ทธ๋ฃน๋ณด๋ค ๊ฐ๊ฑฐ๋ ๋ง์ ๊ฒ์ผ๋ก ์์
gb_list = []
for i in range(len(game_board)):
for j in range(len(game_board)):
if game_board[i][j] == 0:
path = []
dfs(i,j,game_board, 0, path)
gb_list.append(path)
table_list = []
for i in range(len(table)):
for j in range(len(table)):
if table[i][j] == 1:
path = []
dfs(i,j,table, 1, path)
table_list.append(path)
# ์ฑ์ธ ์ ์๋์ง ์ด๋ป๊ฒ ํ๋จํ ๊ฒ์ธ๊ฐ
# ๋จ์ํ ํํ์ด๋์ด๋ฉด ๋งค์ฐ ์ฌ์์ง,, ๊ทธ ์์น๋ค์ ๋์ผํ ๊ฐ์ผ๋ก x์ถ y์ถ ํํ์ด๋ ํ์ ๋ ๋๋ฌํ๋์ง ํ์ธํ๋ฉด ๋๋๊น
# ๊ทผ๋ฐ ์ฐ๋ฆฌ์ ๋ฌธ์ ๋ ํ์ ์ด ๋๋ค๋ ์ฌ์ค,, ๊ทธ ์ขํ๋ฅผ ์ด๋ป๊ฒ ๊ตฌํํ ๊ฒ์ธ๊ฐ
return find_answer(gb_list, table_list)'๐ Study > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ํ๋ก๊ทธ๋๋จธ์ค Lv1 | 2021 KAKAO BLIND RECUITMENT | ์ ๊ท ์์ด๋ ์ถ๊ฐ (0) | 2026.04.04 |
|---|---|
| ํ๋ก๊ทธ๋๋จธ์ค Lv2 | ์ ๋ ฌ | H-index (0) | 2026.04.03 |
| ํ๋ก๊ทธ๋๋จธ์ค Lv3 | dfs/bfs | ์์ดํ ์ค๊ธฐ (0) | 2026.04.01 |
| ํ๋ก๊ทธ๋๋จธ์ค Lv2 | dfs/bfs | ๊ฒ์ ๋งต ์ต๋จ๊ฑฐ๋ฆฌ (0) | 2026.04.01 |
| ํ๋ก๊ทธ๋๋จธ์ค Lv1 | ์์ ํ์ | ๋ชจ์๊ณ ์ฌ (0) | 2026.03.31 |