λ¬Έμ
μμ΄ μ΄λ±νκ΅μλ κ΅μ€μ΄ νλ μκ³ , κ΅μ€μ N×N ν¬κΈ°μ 격μλ‘ λνλΌ μ μλ€. νκ΅μ λ€λλ νμμ μλ N2λͺ μ΄λ€. μ€λμ λͺ¨λ νμμ μ리λ₯Ό μ νλ λ μ΄λ€. νμμ 1λ²λΆν° N2λ²κΉμ§ λ²νΈκ° λ§€κ²¨μ Έ μκ³ , (r, c)λ rν cμ΄μ μλ―Ένλ€. κ΅μ€μ κ°μ₯ μΌμͺ½ μ μΉΈμ (1, 1)μ΄κ³ , κ°μ₯ μ€λ₯Έμͺ½ μλ« μΉΈμ (N, N)μ΄λ€.
μ μλμ νμμ μμλ₯Ό μ νκ³ , κ° νμμ΄ μ’μνλ νμ 4λͺ λ λͺ¨λ μ‘°μ¬νλ€. μ΄μ λ€μκ³Ό κ°μ κ·μΉμ μ΄μ©ν΄ μ ν΄μ§ μμλλ‘ νμμ μ리λ₯Ό μ νλ €κ³ νλ€. ν μΉΈμλ νμ ν λͺ μ μλ¦¬λ§ μμ μ μκ³ , |r1 - r2| + |c1 - c2| = 1μ λ§μ‘±νλ λ μΉΈμ΄ (r1, c1)κ³Ό (r2, c2)λ₯Ό μΈμ νλ€κ³ νλ€.
- λΉμ΄μλ μΉΈ μ€μμ μ’μνλ νμμ΄ μΈμ ν μΉΈμ κ°μ₯ λ§μ μΉΈμΌλ‘ μ리λ₯Ό μ νλ€.
- 1μ λ§μ‘±νλ μΉΈμ΄ μ¬λ¬ κ°μ΄λ©΄, μΈμ ν μΉΈ μ€μμ λΉμ΄μλ μΉΈμ΄ κ°μ₯ λ§μ μΉΈμΌλ‘ μ리λ₯Ό μ νλ€.
- 2λ₯Ό λ§μ‘±νλ μΉΈλ μ¬λ¬ κ°μΈ κ²½μ°μλ νμ λ²νΈκ° κ°μ₯ μμ μΉΈμΌλ‘, κ·Έλ¬ν μΉΈλ μ¬λ¬ κ°μ΄λ©΄ μ΄μ λ²νΈκ° κ°μ₯ μμ μΉΈμΌλ‘ μ리λ₯Ό μ νλ€.
μλ₯Ό λ€μ΄, N = 3μ΄κ³ , νμ N2λͺ μ μμμ κ° νμμ΄ μ’μνλ νμμ΄ λ€μκ³Ό κ°μ κ²½μ°λ₯Ό μκ°ν΄λ³΄μ.
νμμ λ²νΈμ’μνλ νμμ λ²νΈ4 | 2, 5, 1, 7 |
3 | 1, 9, 4, 5 |
9 | 8, 1, 2, 3 |
8 | 1, 9, 3, 4 |
7 | 2, 3, 4, 8 |
1 | 9, 2, 5, 7 |
6 | 5, 2, 3, 4 |
5 | 1, 9, 2, 8 |
2 | 9, 3, 1, 4 |
κ°μ₯ λ¨Όμ , 4λ² νμμ μ리λ₯Ό μ ν΄μΌ νλ€. νμ¬ κ΅μ€μ λͺ¨λ μΉΈμ λΉ μΉΈμ΄λ€. 2λ² μ‘°κ±΄μ μν΄ μΈμ ν μΉΈ μ€μμ λΉμ΄μλ μΉΈμ΄ κ°μ₯ λ§μ μΉΈμΈ (2, 2)μ΄ 4λ² νμμ μλ¦¬κ° λλ€.
4 | ||
λ€μ νμμ 3λ²μ΄λ€. 1λ² μ‘°κ±΄μ λ§μ‘±νλ μΉΈμ (1, 2), (2, 1), (2, 3), (3, 2) μ΄λ€. μ΄ μΉΈμ λͺ¨λ λΉμ΄μλ μΈμ ν μΉΈμ΄ 2κ°μ΄λ€. λ°λΌμ, 3λ² μ‘°κ±΄μ μν΄ (1, 2)κ° 3λ² νμμ μλ¦¬κ° λλ€.
3 | ||
4 | ||
λ€μμ 9λ² νμμ΄λ€. 9λ² νμμ΄ μ’μνλ νμμ λ²νΈλ 8, 1, 2, 3μ΄κ³ , μ΄ μ€μ 3μ μ리μ μμμλ€. μ’μνλ νμμ΄ κ°μ₯ λ§μ΄ μΈμ ν μΉΈμ (1, 1), (1, 3)μ΄λ€. λ μΉΈ λͺ¨λ λΉμ΄μλ μΈμ ν μΉΈμ΄ 1κ°μ΄κ³ , νμ λ²νΈλ 1μ΄λ€. λ°λΌμ, 3λ² μ‘°κ±΄μ μν΄ (1, 1)μ΄ 9λ² νμμ μλ¦¬κ° λλ€.
9 | 3 | |
4 | ||
μ΄λ²μ μ리λ₯Ό μ ν νμμ 8λ² νμμ΄λ€. (2, 1)μ΄ 8λ² νμμ΄ μ’μνλ νμκ³Ό κ°μ₯ λ§μ΄ μΈμ ν μΉΈμ΄κΈ° λλ¬Έμ, μ¬κΈ°κ° κ·Έ νμμ μ리μ΄λ€.
9 | 3 | |
8 | 4 | |
7λ² νμμ μ리λ₯Ό μ ν΄λ³΄μ. 1λ² μ‘°κ±΄μ λ§μ‘±νλ μΉΈμ (1, 3), (2, 3), (3, 1), (3, 2)λ‘ μ΄ 4κ°κ° μκ³ , λΉμ΄μλ μΉΈκ³Ό κ°μ₯ λ§μ΄ μΈμ ν μΉΈμ (2, 3), (3, 2)μ΄λ€. νμ λ²νΈκ° μμ (2, 3)μ΄ 7λ² νμμ μλ¦¬κ° λλ€.
9 | 3 | |
8 | 4 | 7 |
μ΄λ°μμΌλ‘ νμμ μ리λ₯Ό λͺ¨λ μ νλ©΄ λ€μκ³Ό κ°λ€.
9 | 3 | 2 |
8 | 4 | 7 |
5 | 6 | 1 |
μ΄μ νμμ λ§μ‘±λλ₯Ό ꡬν΄μΌ νλ€. νμμ λ§μ‘±λλ μ리 λ°°μΉκ° λͺ¨λ λλ νμ ꡬν μ μλ€. νμμ λ§μ‘±λλ₯Ό ꡬνλ €λ©΄ κ·Έ νμκ³Ό μΈμ ν μΉΈμ μμ μ’μνλ νμμ μλ₯Ό ꡬν΄μΌ νλ€. κ·Έ κ°μ΄ 0μ΄λ©΄ νμμ λ§μ‘±λλ 0, 1μ΄λ©΄ 1, 2μ΄λ©΄ 10, 3μ΄λ©΄ 100, 4μ΄λ©΄ 1000μ΄λ€.
νμμ λ§μ‘±λμ μ΄ ν©μ ꡬν΄λ³΄μ.
μ λ ₯
첫째 μ€μ Nμ΄ μ£Όμ΄μ§λ€. λμ§Έ μ€λΆν° N2κ°μ μ€μ νμμ λ²νΈμ κ·Έ νμμ΄ μ’μνλ νμ 4λͺ μ λ²νΈκ° ν μ€μ νλμ© μ μλμ΄ μ리λ₯Ό μ ν μμλλ‘ μ£Όμ΄μ§λ€.
νμμ λ²νΈλ μ€λ³΅λμ§ μμΌλ©°, μ΄λ€ νμμ΄ μ’μνλ νμ 4λͺ μ λͺ¨λ λ€λ₯Έ νμμΌλ‘ μ΄λ£¨μ΄μ Έ μλ€. μ λ ₯μΌλ‘ μ£Όμ΄μ§λ νμμ λ²νΈ, μ’μνλ νμμ λ²νΈλ N2λ³΄λ€ μκ±°λ κ°μ μμ°μμ΄λ€. μ΄λ€ νμμ΄ μκΈ° μμ μ μ’μνλ κ²½μ°λ μλ€.
μΆλ ₯
첫째 μ€μ νμμ λ§μ‘±λμ μ΄ ν©μ μΆλ ₯νλ€.
μ ν
- 3 ≤ N ≤ 20
μμ μ λ ₯ 1 볡μ¬
3
4 2 5 1 7
3 1 9 4 5
9 8 1 2 3
8 1 9 3 4
7 2 3 4 8
1 9 2 5 7
6 5 2 3 4
5 1 9 2 8
2 9 3 1 4
μμ μΆλ ₯ 1 볡μ¬
54
μμ μ λ ₯ 2 볡μ¬
3
4 2 5 1 7
2 1 9 4 5
5 8 1 4 3
1 2 9 3 4
7 2 3 4 8
9 8 4 5 7
6 5 2 3 4
8 4 9 2 1
3 9 2 1 4
μμ μΆλ ₯ 2 볡μ¬
1053
# νμ΄ λ°©λ²
κ°μΈμ μΌλ‘ μκ°μ΄ λ무 μ€λ κ±Έλ Έλ€. 1μκ° μ΄μμ΄ κ±Έλ ΈμΌλ ꡬνμ μ€μ€λ‘ ν΄λ΄κΈ°λ νμ§λ§ μ΅μ νκΉμ§λ μ€ν¨νλ€
μΌλ¨ 쑰건1>쑰건2>쑰건3 μμΌλ‘ μ½λλ₯Ό ꡬννλ€.
쑰건1) λΉμ΄μλ 곡κ°μ κΈ°μ€μΌλ‘ μ£Όλ³μ μ’μνλ νμ μλ₯Ό μ κ³
쑰건2) 쑰건1μ μν΄ κ±Έλ¬μ§ μμΉλ₯Ό κΈ°μ€μΌλ‘, λΉ μ리 μλ₯Ό μ κ³
쑰건3) 쑰건1,쑰건2μ μν΄ κ±Έλ¬μ§ μμΉλ₯Ό κΈ°μ€μΌλ‘, sortedλ₯Ό μ΄μ©ν΄μ νκ³Όμ΄μ μμκ±Έ μ ννλλ‘ νλ€.
ν·κ°λ Έλ λΆλΆμ scoreμ μκ° maxμΈκ² μ¬λ¬κ° μμ λ μ΄λ»κ² κ°μ΄ λ½μλΌκ±°λ. νν°λ§ν κ±°λκ° ν·κ°λ Έκ³
리μ€νΈμ score μ μκΉμ§ ν¨κ» μ‘μ maxμΈ score μμλ€λ§ λ½μ μ μλλ‘ μ½λλ₯Ό ꡬννλ€.
# μ½λ
νν°λ§μ μ’ λ³΅μ‘νκ² ν κ² κ°λ€..
# 2025-05-02 15:41-16:38
import sys
sys.stdin = open("input.txt","r")
n = int(sys.stdin.readline().strip())
students_fav_list = [list(map(int,sys.stdin.readline().split())) for _ in range(n*n)]
seats = [[0]*n for _ in range(n)]
position = []
dx = [-1,1,0,0]
dy = [0,0,-1,1]
def select_seat(fav_list):
fav_empty_score = []
for i in range(n):
for j in range(n):
fav_cnt = 0
empty_cnt = 0
if seats[i][j] == 0:
for d in range(4):
nx, ny = i + dx[d], j + dy[d]
if 0 <= nx < n and 0 <= ny < n and seats[nx][ny] in fav_list:
fav_cnt += 1
if 0 <= nx < n and 0 <= ny < n and seats[nx][ny] == 0:
empty_cnt += 1
fav_empty_score.append((i,j,fav_cnt,empty_cnt))
# λΉμ΄μλ μΉΈ μ€μμ μ’μνλ νμμ΄ μΈμ ν μΉΈμ κ°μ₯ λ§μ μΉΈμΌλ‘ μ리λ₯Ό μ νλ€.
max_fav = max(fav_empty_score, key = lambda x: x[2])[2]
filtered1 = [s for s in fav_empty_score if s[2] == max_fav]
if len(filtered1) == 1:
return (filtered1[0][0],filtered1[0][1])
# 1μ λ§μ‘±νλ μΉΈμ΄ μ¬λ¬ κ°μ΄λ©΄, μΈμ ν μΉΈ μ€μμ λΉμ΄μλ μΉΈμ΄ κ°μ₯ λ§μ μΉΈμΌλ‘ μ리λ₯Ό μ νλ€.
max_empty = max(filtered1, key = lambda x: x[3])[3]
filtered2 = [s for s in filtered1 if s[3] == max_empty]
if len(filtered2) == 1:
return (filtered2[0][0],filtered2[0][1])
filtered3 = sorted(filtered2)
return (filtered3[0][0],filtered3[0][1])
def cal_score(position_list, fav_list):
score_list = [0,1,10,100,1000]
cnt = 0
i, j = position_list
for d in range(4):
nx, ny = i + dx[d], j + dy[d]
if 0 <= nx < n and 0 <= ny < n and seats[nx][ny] in fav_list:
cnt += 1
return score_list[cnt]
for i in range(n*n):
x, y = select_seat(students_fav_list[i][1:])
position.append((x,y))
seats[x][y] = students_fav_list[i][0]
total_score = 0
for i in range(n*n):
score = cal_score(position[i],students_fav_list[i][1:])
total_score += score
print(total_score)
gptκ° μ΅μ νν΄μ€ μ½λ (쑰건1,쑰건2,쑰건3λ₯Ό νλμ ν¨μλ‘)
import sys
sys.stdin = open("input.txt", "r")
n = int(sys.stdin.readline())
students = []
like_map = dict()
for _ in range(n * n):
data = list(map(int, sys.stdin.readline().split()))
students.append(data[0])
like_map[data[0]] = set(data[1:])
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
result = [[0] * n for _ in range(n)]
student_pos = dict()
def get_best_position(student):
max_like, max_empty = -1, -1
candidates = []
for i in range(n):
for j in range(n):
if result[i][j] != 0:
continue
like, empty = 0, 0
for d in range(4):
nx, ny = i + dx[d], j + dy[d]
if 0 <= nx < n and 0 <= ny < n:
if result[nx][ny] in like_map[student]:
like += 1
elif result[nx][ny] == 0:
empty += 1
if like > max_like or (like == max_like and empty > max_empty):
candidates = [(i, j)]
max_like, max_empty = like, empty
elif like == max_like and empty == max_empty:
candidates.append((i, j))
# 쑰건 3: ν, μ΄ κΈ°μ€ μ λ ¬
candidates.sort()
return candidates[0]
def calculate_satisfaction():
score_table = [0, 1, 10, 100, 1000]
total = 0
for student in students:
x, y = student_pos[student]
like = 0
for d in range(4):
nx, ny = x + dx[d], y + dy[d]
if 0 <= nx < n and 0 <= ny < n and result[nx][ny] in like_map[student]:
like += 1
total += score_table[like]
return total
# μ리 λ°°μΉ
for student in students:
x, y = get_best_position(student)
result[x][y] = student
student_pos[student] = (x, y)
# λ§μ‘±λ μΆλ ₯
print(calculate_satisfaction())
# μ°Έκ³ (μ΅μ ννμ§ λͺ»ν μ½λ)
λ΄ νμΌλ‘ νκΈ°λ νμΌλ μ°Έ μ€λ κ±Έλ Έλ€.. μ€κ°μ€κ° ν·κ°λ¦¬λ νμ΄μ¬ λ¬Έλ²λ μ°ΎκΈ°λ νκ³
# 2025-04-30 12:37-13:50
import sys
sys.stdin = open("input.txt","r")
n = int(sys.stdin.readline().strip())
students = []
for i in range(n*n):
tmp_list = list(map(int,sys.stdin.readline().split()))
students.append(tmp_list)
result = [[0]*(n) for _ in range(n)]
dx = [-1,1,0,0]
dy = [0,0,-1,1]
# 쑰건 1 --- λΉμ΄μλ μΉΈ μ€μμ μ’μνλ νμμ΄ μΈμ ν μΉΈμ κ°μ₯ λ§μ μΉΈ
def cond1(fav_list):
scores = []
for i in range(n):
for j in range(n):
if result[i][j] == 0:
cnt = 0
for k in range(4):
nx, ny = i + dx[k], j + dy[k]
if 0 <= nx < n and 0 <= ny < n:
if result[nx][ny] in fav_list:
cnt += 1
scores.append((i,j,cnt))
max_val = max(scores,key=lambda x:x[2])[2]
filtered = [(s[0],s[1]) for s in scores if s[2] == max_val]
return filtered
# 쑰건 2 --- 1μ λ§μ‘±νλ μΉΈμ΄ μ¬λ¬ κ°μ΄λ©΄, μΈμ ν μΉΈ μ€μμ λΉμ΄μλ μΉΈμ΄ κ°μ₯ λ§μ μΉΈ
def cond2(filtered):
scores = []
for f in filtered:
x, y = f
cnt = 0
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if 0 <= nx < n and 0 <= ny < n:
if result[nx][ny] == 0:
cnt += 1
scores.append((x,y,cnt))
max_val = max(scores,key=lambda x:x[2])[2]
filtered2 = [(s[0],s[1]) for s in scores if s[2] == max_val]
return filtered2
def cal_result(student, fav_list):
for i in range(n):
for j in range(n):
if result[i][j] == student:
cnt = 0
for k in range(4):
nx, ny = i + dx[k], j + dy[k]
if 0 <= nx < n and 0 <= ny < n:
if result[nx][ny] in fav_list:
cnt += 1
if cnt == 0: return 0
elif cnt == 1: return 1
elif cnt == 2: return 10
elif cnt == 3: return 100
elif cnt == 4: return 1000
for i in range(n*n):
filtered = cond1(students[i][1:])
if len(filtered) == 1:
result[filtered[0][0]][filtered[0][1]] = students[i][0]
continue
filtered2 = cond2(filtered)
if len(filtered2) == 1:
result[filtered2[0][0]][filtered2[0][1]] = students[i][0]
continue
# 쑰건 3 --- 2λ₯Ό λ§μ‘±νλ μΉΈλ μ¬λ¬ κ°μΈ κ²½μ°μλ νμ λ²νΈκ° κ°μ₯ μμ μΉΈμΌλ‘, κ·Έλ¬ν μΉΈλ μ¬λ¬ κ°μ΄λ©΄ μ΄μ λ²νΈκ° κ°μ₯ μμ μΉΈμΌλ‘
sorted_filtered2 = sorted(filtered2,key=lambda x:(x[0],x[1]))
result[sorted_filtered2[0][0]][sorted_filtered2[0][1]] = students[i][0]
final_score = 0
for i in range(n*n):
final_score += cal_result(students[i][0],students[i][1:])
print(final_score)
'π Study > Baekjoon' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Silver II] 12933 - μ€λ¦¬ (0) | 2025.05.04 |
---|---|
[Silver I] 1283 - λ¨μΆν€ μ§μ (0) | 2025.05.01 |
[Gold V] 16926 - λ°°μ΄ λ리기1 (0) | 2025.04.27 |
[Gold IV] 1107 - 리λͺ¨μ»¨ (0) | 2025.04.25 |
[Gold IV] 14502 - μ°κ΅¬μ (0) | 2025.04.25 |