반응형
n = int(input())

answer= 0 
array = [[0 for col in range(n+2)] for row in range(n+2)]

array[0] = [-1 for col in range(n+1)]
array[-1] = [-1 for col in range(n+1)]
for cols in array:
  cols[0] = -1
  cols[-1] = -1

rules = []
rules2 = {}
for i in range(n**2):
  rules.append(list(map(int,input().split())))
  rules2[rules[-1][0]] = rules[-1][1:]

for order in rules:
  student_num = order[0]
  temp_max = 0 
  temp_list= []
  for j in range(1,n+1):
    for k in range(1,n+1):
      if array[j][k] != 0:
        continue
      temp = 0
      if array[j+1][k] in order:
        temp +=1
      if array[j][k+1] in order:
        temp +=1
      if array[j-1][k] in order:
        temp +=1
      if array[j][k-1] in order:
        temp +=1

      if temp == temp_max:
        temp_list.append((j,k))
      if temp > temp_max:
        temp_max = temp
        temp_list=[(j,k)]


  #1번규칙
  # print(order)

  if len(temp_list) == 1:
    j,k = temp_list[0]
    array[j][k] = student_num
    # print(array)
    continue
  
  #2번 규칙
  temp_max2 = 0 
  temp_list2 = []

  for temp_locate in temp_list:
    j2,k2 = temp_locate
    temp2 = 0
    if array[j2+1][k2] ==0:
      temp2 +=1
    if array[j2][k2+1] ==0:
      temp2 +=1
    if array[j2-1][k2] ==0:
      temp2 +=1
    if array[j2][k2-1] ==0:
      temp2 +=1
    
    if temp2 == temp_max2:
      temp_list2.append((j2,k2))
    if temp2 > temp_max2:
      temp_max2 = temp2
      temp_list2=[(j2,k2)]

  if len(temp_list2) == 1:
    j3,k3 = temp_list2[0]
    array[j3][k3] = student_num
    # print(array)
    continue
  temp_list2.sort()
  j3,k3 = temp_list2[0]
  array[j3][k3] = student_num
  
for j4 in range(1,n+1):
  for k4 in range(1,n+1):
    target = array[j4][k4]
    temp3 = 0
    if array[j4+1][k4] in rules2[target]:
      temp3 +=1
    if array[j4][k4+1] in rules2[target]:
      temp3 +=1
    if array[j4-1][k4] in rules2[target]:
      temp3 +=1
    if array[j4][k4-1] in rules2[target]:
      temp3 +=1
    
    if temp3 ==1:
      answer +=1
    if temp3 ==2:
      answer += 10
    if temp3 ==3:
      answer += 100
    if temp3 ==4:
      answer += 1000

print(answer)

https://www.acmicpc.net/problem/21608

반응형

'computer 지식 > 알고리즘 정복기' 카테고리의 다른 글

1244 스위치 켜고 끄기 백준 파이썬  (0) 2021.10.13
백준 1212 파이썬  (0) 2021.10.11
백준 2753번 파이썬  (0) 2021.10.11
백준 2501번  (0) 2021.10.10
이 카테고리에 관하여  (0) 2021.10.10

+ Recent posts