๋ฐ์ํ
๐ค๋ฌธ์ ํด๊ฒฐ
- ์๋ฎฌ๋ ์ด์
- ํ์: *์น๊ตฌ๋ค ๋์ ๋๋ฆฌ๋ฅผ ๋ง๋ ๋ค.
- ๋ฌธ์ ์ ํฌ๊ธฐ๊ฐ ํฌ์ง ์๊ธฐ ๋๋ฌธ์ ์ด์ค ํฌ๋ฌธ์ผ๋ก ๊ต์ค ์ ์ฒด๋ฅผ ํ์
- ์ข์ํ๋ ์ฌ๋ ์์๋ฆฌ์ ๋น์๋ฆฌ ๊ฐ์๋ฅผ ๊ฐ๊ฐ ์ฒดํฌํด์ค๋ค.
- ์ข์ํ๋ ์ฌ๋ ์์๋ฆฌ ์
- ๋น์๋ฆฌ ์
- ์์๋ก ์ฒดํฌํด๊ฐ๋ฉฐ ํฐ ๊ฐ์ ์ ๋ฐ์ดํธ ํด์ค๋ค.
- ์ธ๋ฑ์ค๊ฐ ์์ ๊ฐ๋ถํฐ ํ์ํ๊ธฐ ๋๋ฌธ์ ๊ฐ๋ค๋ฉด ํ๋ฒํธ, ์ด๋ฒํธ๊ฐ ์์ ์๋ฆฌ๊ฐ ์ ํ๋๊ฒ ๋๋ค.
- ๋ค ์ฐพ์์ผ๋ฉด ๊ต์ค ์ ์ฒด๋ฅผ ๋๋ฉฐ ๋ง์กฑ๋ ์กฐ์ฌ
- ๋
๐ป์์ค ์ฝ๋
import sys
input = sys.stdin.readline
def is_in(ix, iy, n):
if 0 <= ix < n and 0 <= iy < n:
return True
return False
N = int(input())
# ํ์: *์ข์ํ๋ ์น๊ตฌ๋ค ๋์
๋๋ฆฌ
like_friends = {}
for _ in range(N ** 2):
student, *friends = list(map(int, input().split()))
like_friends[student] = friends
# ๊ต์ค
classroom = [[0 for _ in range(N)] for _ in range(N)]
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
for stu, likes in like_friends.items():
max_like = 0 # ๋ถ์ด์๋ ์๋ฆฌ์ ์ข์ํ๋ ์ต๋
max_blank = 0 # ๋ถ์ด์๋ ์๋ฆฌ์ ๋น์๋ฆฌ ์ต๋
loc = -1, -1 # ๊ทธ ์์น
for i in range(N):
for j in range(N):
if classroom[i][j] != 0: # ์๋ฆฌ๊ฐ ๋น์ด์๋ ๊ฒฝ์ฐ๋ง
continue
if loc == (-1, -1): # ์ฃผ์! ๋น์๋ฆฌ๋ ์ข์ํ๋ ์น๊ตฌ๊ฐ ์๋ ์๋ฆฌ๊ฐ ๋ชจ๋ 0์ผ ์ ์๋ค.
loc = i, j # ์ฒ์ ๋์ค๋ ๋น์๋ฆฌ๋ฅผ ๊ธฐ์ค์ผ๋ก ์งํ
blank = 0
like = 0
for k in range(4):
nx = i + dx[k]
ny = j + dy[k]
if is_in(nx, ny, N):
if classroom[nx][ny] == 0: # 1. ๋น์๋ฆฌ
blank += 1
elif classroom[nx][ny] in like_friends[stu]: # 2. ์ข์ํ๋ ํ์ ์๋ฆฌ
like += 1
if like > max_like or (like == max_like and blank > max_blank):
# 1. ์ข์ํ๋ ์ฌ๋ ์์๋ฆฌ, 2. ๊ฐ์ผ๋ฉด ๋น์๋ฆฌ
max_blank = blank
max_like = like
loc = i, j
x, y = loc
classroom[x][y] = stu
# td(classroom)
# ์ ์ ๊ณ์ฐ
total = 0
satisfaction = [0, 1, 10, 100, 1000]
for i in range(N):
for j in range(N):
stu = classroom[i][j]
like = 0
for k in range(4):
nx = i + dx[k]
ny = j + dy[k]
if is_in(nx, ny, N) and classroom[nx][ny] in like_friends[stu]:
like += 1
total += satisfaction[like]
print(total)
๐๋ฌธ์ ํ์ธ
์ถ์ฒ: BACKJOON ONLINE JUDGE
๋ฐ์ํ
'Algorithm Problem > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python] ํ๋ก๊ทธ๋๋จธ์ค - ์ํด๋ฆฌ์ฑ๋ฆฐ์ง 7์ฃผ์ฐจ (0) | 2021.09.17 |
---|---|
[python] ๋ฐฑ์ค - 2485. ๊ฐ๋ก์ (0) | 2021.09.16 |
[python] ๋ฐฑ์ค - 1347. ๋ฏธ๋ก ๋ง๋ค๊ธฐ (0) | 2021.09.14 |
[python] ๋ฐฑ์ค - 12764. ์ธ์ง๋ฐฉ์ ๊ฐ ์คํ (0) | 2021.09.11 |
[python] ๋ฐฑ์ค - 11085. ๊ตฐ์ฌ ์ด๋ (0) | 2021.09.10 |