SWEA 5648 원자 소멸 시뮬레이션 python(파이썬)

Kang HwanSeok·2022년 8월 15일
0

SWEA

목록 보기
7/8
post-thumbnail

문제 주소

SW Expert Academy SWEA 5648. 원자 소멸 시뮬레이션

문제 간략 설명

말 그대로 시뮬레이션이다.

문제 포인트

dx dy, 델타 탐색에 대한 이해도를 묻고 있네요?

  • 이동하고 겹친 좌표를 어떻게 찾을 수 있을까요?
    • 같은 장소에 3~4개의 원소가 한번에 모인다면?
  • 터져서 에너지를 방출한 요소를 어떻게 원본 데이터에서 지울 수 있을까요?

알아갈 개념 요약

dx dy, 델타 탐색과 좌표에 대한 이해

  • dx dy 설계의 유연성. +0.5도 가능하다.
    • 아니면 그냥 기존의 값 자체를 뻥튀기 하는 방식도 있다.
  • 리스트 요소를 del로 지울 경우, 리스트 인덱스 자체에 손상이 올 수 있다.
    • 지울 때마다 카운팅해서 상대 위치를 조정하면 어떨까?

풀이

<시각화 자료는 곧 업로드 하겠습니다.>

  • 코드: 하나하나 절차를 밟아나아가자.
    # # 5648. 원자 소멸 시뮬레이션
    # 코드 설계:
    # 1. 그냥 dx dy로 0.5씩 이동시키고, 겹치는거 있으면 일단 점수 부터 받고 한번에 조져.
    # 2. 임시 리스트 제작하고, rm_count 쌓아서 지우는게 포인트. 다 지운 후에 본체에 덮어 쓰기.
    # 조타용 좌표
    dx = [0, 0, -1, 1]
    dy = [1, -1, 0, 0]
    T = int(input())
    for case_num in range(1, T+1):
      N = int(input())
      atom_list = [list(map(int, input().split())) for _ in range(N)]
      turn = 0
      sum_power = 0
      while turn < 4000 and len(atom_list) > 1:
          turn += 1
          for atoms in atom_list:
              atoms[0] += dx[atoms[2]]/2
              atoms[1] += dy[atoms[2]]/2
          atom_list.sort()
          for idx in range(len(atom_list)-1):
              if atom_list[idx][0] == atom_list[idx+1][0] and atom_list[idx][1] == atom_list[idx+1][1]:
                  sum_power += atom_list[idx][3] + atom_list[idx+1][3]
                  atom_list[idx][3] = 0
                  atom_list[idx + 1][3] = 0
          removed_list = atom_list[::]
          rm_count = 0
          for explosion in range(len(atom_list)):
              if atom_list[explosion][3] == 0:
                  del removed_list[explosion-rm_count]
                  rm_count += 1
          atom_list = removed_list[::]
      print(f'#{case_num} {sum_power}')
profile
알고리즘을 좋아하는 개발자

0개의 댓글