ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ ์๋ฎฌ๋ ์ด์ , ๊ตฌํ, BFS
* 2021 ์ผ์ฑ ์๋ฐ๊ธฐ ์ค์ ๊ณต์ฑ SW ์ญ๋ํ ์คํธ ๋ฌธ์ (SW Aํ)
https://www.acmicpc.net/problem/21609
thinking
1๏ธโฃ ์คํ ํ๋ ์ด -> while๋ฌธ ์ฌ์ฉ
2๏ธโฃ ํฌ๊ธฐ๊ฐ ๊ฐ์ฅ ํฐ ๋ธ๋ก ์ฐพ๊ธฐ -> ๊ฐ๋ฅํ ๋ธ๋ก ๊ทธ๋ฃน์ ๊ฒฝ์ฐ๋ฅผ ๋ชจ๋ ๊ตฌํ ํ, ๋ด๋ฆผ์ฐจ์ ์ํ
ํด์ ์ต๋๋ธ๋ก ๊ตฌํ๊ธฐ
- ์ด๋, ๋ธ๋กํฌ๊ธฐ, ๋ฌด์ง๊ฐํฌ๊ธฐ, ๋ธ๋ก ์ขํ ํ์
- ๋ฌด์ง๊ฐ ๋ธ๋ก(0)์ ์๋์ฒ๋ผ ๋ค๋ฅธ ๋ธ๋ก๊ณผ๋ ์ฐ๊ฒฐ๋ ์ ์๊ธฐ๋๋ฌธ์ ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ํด์ ํด์ค์ผํ๋ค.
3๏ธโฃ ๋ธ๋ก์ ๊ฑฐ+์ ์๋ํ๊ธฐ
- 2๋ฒ์์ ๊ตฌํ ๋ธ๋ก ์ขํ๋ฅผ ์ํํ๋ฉฐ ์ ๊ฑฐ๋ ๋ธ๋ก๊ฐ์ -2 ๋ก ์ค์ ํ์ฌ ๊ตฌ๋ถ์ง์ด์ค
4๏ธโฃ ์ค๋ ฅ
5๏ธโฃ 90ํ์
6๏ธโฃ ์ค๋ ฅ
๐ป TIP ) 90๋ ๋ฐ์๊ณ ํ์ ์ zip์ผ๋ก ๊ตฌํํ๊ธฐ
# a๋ array
a = list(zip(*a))[::-1]
a = [list(s) for s in a]
์ฝ๋
from collections import deque
# ์ธ์ ๋ธ๋ก ์ฐพ๊ธฐ -> ๋ธ๋ก ํฌ๊ธฐ, ๋ฌด์ง๊ฐํฌ๊ธฐ, ๋ธ๋ก์ขํ ๋ฆฌํด
def bfs(x, y, color):
q = deque()
q.append([x, y])
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
block_cnt, rainbow_cnt = 1, 0 # ๋ธ๋ก๊ฐ์, ๋ฌด์ง๊ฐ๋ธ๋ก ๊ฐ์
blocks, rainbows = [[x,y]], [] # ๋ธ๋ก์ขํ ๋ฃ์ ๋ฆฌ์คํธ, ๋ฌด์ง๊ฐ์ขํ ๋ฃ์ ๋ฆฌ์คํธ
while q:
x, y = q.popleft()
for d in range(4):
nx = x + dx[d]
ny = y + dy[d]
# ๋ฒ์ ์์ด๋ฉด์ ๋ฐฉ๋ฌธ ์ํ ์ผ๋ฐ ๋ธ๋ก์ธ ๊ฒฝ์ฐ
if 0 <= nx < N and 0 <= ny < N and not visited[nx][ny] and a[nx][ny] == color:
visited[nx][ny] = 1
q.append([nx, ny])
block_cnt += 1
blocks.append([nx, ny])
# ๋ฒ์ ์์ด๋ฉด์ ๋ฐฉ๋ฌธ ์ํ ๋ฌด์ง๊ฐ ๋ธ๋ก์ธ ๊ฒฝ์ฐ
elif 0 <= nx < N and 0 <= ny < N and not visited[nx][ny] and a[nx][ny] == 0:
visited[nx][ny] = 1
q.append([nx, ny])
block_cnt += 1
rainbow_cnt += 1
rainbows.append([nx, ny])
# ๋ฌด์ง๊ฐ๋ธ๋ก์ ๋ฐฉ๋ฌธ ๋ค์ ํด์
for x,y in rainbows:
visited[x][y] = 0
return [block_cnt, rainbow_cnt, blocks+rainbows]
# ๋ธ๋ก ์ ๊ฑฐ ํจ์
def remove(block):
for x,y in block:
a[x][y] = -2
# ์ค๋ ฅ ํจ์
def gravity(a):
for i in range(N-2, -1, -1): # ๋ฐ์์ ๋ถํฐ ์ฒดํฌ
for j in range(N):
if a[i][j] > -1: # -1์ด ์๋๋ฉด ์๋๋ก ๋ค์ด
r = i
while True:
if 0<=r+1<N and a[r+1][j] == -2: # ๋ค์ํ์ด ์ธ๋ฑ์ค ๋ฒ์ ์์ด๋ฉด์ -2์ด๋ฉด ์๋๋ก ๋ค์ด
a[r+1][j] = a[r][j]
a[r][j] = -2
r += 1
else:
break
# ๋ฐ์๊ณ ํ์ ํจ์
def rot90(a):
new_a = [[0]*N for _ in range(N)]
for i in range(N):
for j in range(N):
new_a[N-1-j][i] = a[i][j]
return new_a
# 0. ๋ฉ์ธ์ฝ๋
N, M = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(N)]
score = 0 # ์ ์
# 1. ์คํ ํ๋ ์ด-> while {2. ํฌ๊ธฐ ๊ฐ์ฅ ํฐ ๋ธ๋ก ์ฐพ๊ธฐ 3. ๋ธ๋ก์ ๊ฑฐ+์ ์๋ํ๊ธฐ 4. ์ค๋ ฅ 5. 90ํ์ 6. ์ค๋ ฅ}
while True:
# 2. ํฌ๊ธฐ ๊ฐ์ฅ ํฐ ๋ธ๋ก ์ฐพ๊ธฐ
visited = [[0] * N for _ in range(N)] # ๋ฐฉ๋ฌธ์ฒดํฌ
blocks = [] # ๊ฐ๋ฅํ ๋ธ๋ก ๊ทธ๋ฃน๋ค ๋ฃ์ ๋ฆฌ์คํธ
for i in range(N):
for j in range(N):
if a[i][j] > 0 and not visited[i][j]: # ์ผ๋ฐ๋ธ๋ก์ด๋ฉด์ ๋ฐฉ๋ฌธ ์ํ์ผ๋ฉด
visited[i][j] = 1 # ๋ฐฉ๋ฌธ
block_info = bfs(i, j, a[i][j]) # ์ธ์ ํ ๋ธ๋ก ์ฐพ๊ธฐ
# block_info = [๋ธ๋กํฌ๊ธฐ, ๋ฌด์ง๊ฐ๋ธ๋ก ๊ฐ์, ๋ธ๋ก์ขํ]
if block_info[0] >= 2:
blocks.append(block_info)
blocks.sort(reverse=True)
# 3. ๋ธ๋ก์ ๊ฑฐ+์ ์๋ํ๊ธฐ
if not blocks:
break
remove(blocks[0][2])
score += blocks[0][0]**2
# 4. ์ค๋ ฅ
gravity(a)
# 5. 90ํ์
a = rot90(a)
# 6. ์ค๋ ฅ
gravity(a)
print(score)
'algorithm > baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 20056. ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด๋ณผ / python ํ์ด์ฌ (2) | 2021.08.05 |
---|---|
[๋ฐฑ์ค] 17406. ๋ฐฐ์ด๋๋ฆฌ๊ธฐ4 / python ํ์ด์ฌ (0) | 2021.07.29 |
[๋ฐฑ์ค] 20057. ๋ง๋ฒ์ฌ ์์ด์ ํ ๋ค์ด๋ / python ํ์ด์ฌ (1) | 2021.07.05 |
[๋ฐฑ์ค] 2579. ๊ณ๋จ์ค๋ฅด๊ธฐ / python ํ์ด์ฌ (0) | 2021.06.30 |
[๋ฐฑ์ค] 2003. ์๋ค์ ํฉ 2 / python ํ์ด์ฌ / ํฌํฌ์ธํฐ, ๊ตฌ๊ฐํฉ (0) | 2021.06.30 |
- ์๊ณ ๋ฆฌ์ฆ
- dfs
- ๋ฐฑ์ค
- 20056 ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด๋ณผ
- ๊ธฐ์ง๊ตญ์ค์น
- ๋ณด์์ผํ
- 2018 ์นด์นด์ค ๊ณต์ฑ
- ํ์ด์ฌ
- 21609 ์์ด ์คํ๊ต
- merge ์๋ฌ
- 2579 ๊ณ๋จ์ค๋ฅด๊ธฐ
- 17406 ๋ฐฐ์ด๋๋ฆฌ๊ธฐ4
- Python
- ์ผ์ฑ๊ธฐ์ถ
- dp
- ๋ธ๋ฃจํธํฌ์ค
- react
- 20057 ๋ง๋ฒ์ฌ ์์ด์ ํ ๋ค์ด๋
- ์ผ์ฑ์ฝํ
- git ๋ฏธ๋ฌ๋ง
- BFS
- ์์ด๋๋ง์๊ธฐ
- merge์๋ฌ
- swea
- ํ๋ก๊ทธ๋๋จธ์ค
- Total
- Today
- Yesterday