👨‍💻
Hamin TIL
  • Today I Learned 🧑🏻‍💻
  • 회고
  • git
    • git_basics
      • Git 101
      • Git branch
      • Git_ignore
    • Git Book
    • 우아한형제들
    • pull_request
  • db
    • DA
      • 데이터표준화
      • 데이터_요건분석
      • 전사아키텍처_이해
      • 데이터모델링
    • SQL
      • SQL기본및활용
        • SQL활용
          • 절차형SQL
          • 계층형질의와셀프조인
          • DCL
          • 그룹함수
          • 윈도우함수
          • 표준조인
          • 집합연산자
          • 서브쿼리
        • SQL고급활용및튜닝
          • 옵티마이저와실행계획
          • 조인수행원리
          • 인덱스기본
        • SQL기본
          • 함수
          • 관계형데이터베이스개요
          • GROUPBY,HAVING절
          • DDL
          • 조인
          • ORDERBY절
          • DML
          • WHERE절
          • TCL
      • 데이터모델링의이해
        • 데이터모델과성능
          • 정규화의 성능
          • 데이터베이스구조와성능
          • 분산데이터베이스와성능
          • 대량 데이터에 따른 성능
          • 반정규화와 성능
          • 성능데이터모델링의 개요
        • 데이터모델링의이해
          • 식별자
          • 속성
          • 관계
          • 엔터티
          • 데이터 모델의 이해
    • DB
  • trouble
    • libomp
    • After macOS update, git command
    • system
  • algorithm
    • BOJ
      • 평범한 배낭
      • 17825-주사위윷놀이
      • 14888-연산자끼워넣기
      • 14503-로봇청소기
      • 10157
      • 14502-연구소
      • 18428-감시피하기
      • 14501
      • 18405-경쟁적전염
      • 14499-주사위굴리기
      • 16236-아기상어
      • 15686-치킨배달
      • 19237-어른상어
      • 16234-인구이동
      • 19236-청소년상어
      • 1339-단어수학
      • 리모콘
      • 18353 - 병사배치하기
      • 18352-특정거리의도시찾기
      • 12100-2048
      • N-Queen
      • 3190-뱀
      • 11724
    • programmers
      • 영어끝말잇기
      • 기둥과 보
      • H - index
      • 정수삼각형
      • 2018 KAKAO BLIND RECRUITMENT - 압축
      • 삼각달팽이
      • 거스름돈
      • [1차] 셔틀버스
    • data_structure
      • Queue
      • Graph
      • Stack
      • Hash table
    • implementation
      • dynamic_programming
      • sort
      • Least common multiple
      • dfs
      • dijkstra
      • bfs
      • binary_search
    • aps
      • notes
    • modules
  • python
    • requirements.txt
    • Jupyter notebook
    • 00_들어가기 전에
    • Python Virtual Environment
    • Python Syntax
  • django
    • Class Based View in Django
    • Model in Django
    • URL Name
    • Form and ModelForm
    • Authentication
    • Tips & Tricks
    • Optimization
    • Request and Response Objects
    • Templates
    • Variable Routing & DTL
    • Django REST API with JSON web token (JWT)
    • Intro to Django
    • Django REST Framework
    • Wrap-up
    • Image Upload
  • javascript
    • Ajax (Asynchronous Javascript And XML)
    • Document Object Model
    • Java Script 101
    • ES (ECMAscript)
  • java
    • Java 101
  • aws
    • beginning_cloud_computing_with_aws
      • 02 AWS 주요 서비스 이해하기
      • 01 아마존 웹 서비스 Cloud 개요
  • programming
    • Communication
    • CS_용어사전
  • vue.js
    • 01_Vue.js_Intro
  • data_science
    • 01_데이터에서인사이트발견하기
    • pandas
    • 04_데이터분류모델
    • 02_텍스트마이닝첫걸음
    • 05_종합예제
    • 03_미래를예측하는데이터분석
    • Statistics
      • 모수와 추정량
    • 통계학노트
  • linux
    • Linux Commands
  • ide
    • VScode
    • Pycharm
  • html,css
    • HTML 101
    • CSS 101
  • colab
    • colab_101
  • 의사결정나무및모형비교
Powered by GitBook
On this page

Was this helpful?

  1. algorithm
  2. BOJ

19236-청소년상어

Previous16234-인구이동Next1339-단어수학

Last updated 4 years ago

Was this helpful?

  1. 물고기를 먹고, 물고기들을 이동시키고, 상어가 이동할 수 있는 모든 곳으로 재귀를 타는 DFS 를 수행한다.

  2. DFS 마다 더 이상 이동할 수 없을 때 최댓값으로 업데이트 한다.

import copy

# 4 * 4 크기의 정사각형에 존재하는 각 물고기의 번호 ( 없으면 -1 ) 와 같은 방향 값을 담는 테이블
array = [[None]*4 for _ in range(4)]

for i in range(4):
    data = list(map(int, input().split()))
    # 매 줄마다 4마리의 물고기를 하나씩 확인하며
    for j in range(4):
        # 각 위치마다 [ 물고기번호, 방향 ] 을 저장
        array[i][j] = [data[j*2], data[j*2+1] -1]

# 8 가지 방향에 대한 정의
dx = [-1,-1,0,1,1,1,0,-1]
dy = [0,-1,-1,-1,0,1,1,1]

# 현재 위치에서 왼쪽으로 회전된 결과 반환
def turn_left(direction):
    return ( direction + 1) % 8

result = 0 # 최종결과

# 현재 배열에서 특정한 번호의 물고기 위치 찾기
def find_fish(array, index):
    for i in range(4):
        for j in range(4):
            if array[i][j][0] == index:
                return (i,j)
    return None

# 모든 물고기를 회전 및 이동시키는 함수
def move_all_fishes(array, now_x, now_y):
    # 1번부터 16번까지의 물고기를 차례대로 확인
    for i in range(1,17):
        # 해당 물고기의 위치 찾기
        position = find_fish(array,i)
        if position != None:
            x,y = position[0],position[1]
            direction = array[x][y][1]
            # 해당물고기의 방향을 왼쪽으로 계속 회전시키며 이동이 가능한지 확인
            for _ in range(8):
                nx = x + dx[direction]
                ny = y + dy[direction]
                # 해당방향으로 이동이 가능하다면 이동시키기
                if 0 <= nx < 4 and 0 <= ny < 4:
                    if not (nx == now_x and ny == now_y):
                        array[x][y][1] = direction
                        array[x][y], array[nx][ny] = array[nx][ny],array[x][y]
                        break
                direction = turn_left(direction)

# 상어가 현재 위치에서 먹을 수 있는 모든 물고기의 위치 반환
def get_possible_positions(array, now_x, now_y):
    positions = []
    direction = array[now_x][now_y][1]
    # 현재의 방향으로 계속 이동시키기
    for _ in range(3):
        now_x += dx[direction]
        now_y += dy[direction]
        # 범위를 벗어나지 않는지 확인하며
        if 0 <= now_x < 4 and 0 <= now_y <4:
            # 물고기가 존재하면
            if array[now_x][now_y][0] != -1:
                positions.append((now_x,now_y))
    return positions

# 모든 경우를 탐색할 DFS
def dfs(array, now_x, now_y, total):
    global result
    array = copy.deepcopy(array) # 리스트 통째로 복사
    # array = [array[i][:] for i in range(len(array))]
    total += array[now_x][now_y][0] # 현재 위치의 물고기 먹기
    array[now_x][now_y][0] = -1 # 먹었으므로 번호 값을 -1 로 변환

    move_all_fishes(array,now_x,now_y) # 전체 물고기 이동시키기 상어가 now_y, now_x

    # 이제 다시 상어가 이동할 차례, 이동 가능한 위치 찾기
    positions = get_possible_positions(array,now_x,now_y)
    if len(positions) == 0:
        result = max(result,total)
        return
    # 모든 이동할 수 있는 위치 재귀 수행
    for next_x, next_y in positions:
        dfs(array,next_x,next_y,total)

# 청소년 상어의 시작 ( 0,0 ) 부터 재귀 수행
dfs(array,0,0,0)
# for row in array:
#     print(*row)
print(result)
www.acmicpc.net/problem/19236