회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
뭐가 문제야 say something(살려주세요)
19236번 - 청소년 상어
rlaruddk
3년 전
0
물고기 이동은 문제 없는 것 같은데(제생각)
상어 이동하는 데 최댓값이 자꾸 다르게 나옵니다......
외않되..
from collections import deque from copy import deepcopy import sys input = sys.stdin.readline # 물고기 잡아먹기 def eat_fish(shark_x, shark_y, sum_fish, space, direction, position): global max_sum_fish # 먹은거 처리 sum_fish += space[shark_x][shark_y] # 먹은 물고기 번호 더하기 shark_d = direction[shark_x][shark_y] # 상어 방향 position[space[shark_x][shark_y]] = 0 # 먹은 물고기 위치 삭제 space[shark_x][shark_y] = 's' # 상어 자리 # 물고기 이동 move_fish(space, direction, position) space[shark_x][shark_y] = 0 # 빈칸 처리 # 상어 이동가능 리스트 move_list = shark_move_list(shark_x, shark_y, shark_d) for nx, ny in move_list: eat_fish(nx, ny, sum_fish, deepcopy(space), deepcopy(direction), deepcopy(position)) max_sum_fish = max(max_sum_fish, sum_fish) # 물고기 이동 def move_fish(space, direction, position): # 1번부터 물고기 이동 for i in range(1, 17): if position[i] != 0: # 0은 잡아먹힌 물고기 fish_x, fish_y = position[i] # 원래 위치 # 이동할 수 있는지 체크 di = direction[fish_x][fish_y] - 1 for d in range(di, di + 8): # 방향 바꾸기 위해서 nx = fish_x + dx[d % 8] ny = fish_y + dy[d % 8] # 범위 내에 있고 상어가 없으면 이동 if 0 <= nx < 4 and 0 <= ny < 4 and space[nx][ny] != 's': position[i], position[space[nx][ny]] = position[space[nx][ny]], position[i] # 위치 바꾸기 space[nx][ny], space[fish_x][fish_y] = space[fish_x][fish_y], space[nx][ny] # 자리 바꾸기 direction[nx][ny], direction[fish_x][fish_y] = direction[fish_x][fish_y], direction[nx][ny] break # 상어가 이동할 수 있는 위치 def shark_move_list(shark_x, shark_y, shark_d): move_list = [] for d in range(1, 4): nx = shark_x + dx[shark_d - 1] * d ny = shark_y + dy[shark_d - 1] * d if 0 <= nx < 4 and 0 <= ny < 4 and space[nx][ny] != 0: move_list.append((nx, ny)) # 재귀에서 돌아왔을 때 원래 걸로 복원하고 for문 진행 return move_list input_space = [list(map(int, input().split())) for _ in range(4)] max_sum_fish = 0 # 이동 방향 dx = [-1, -1, 0, 1, 1, 1, 0, -1] dy = [0, -1, -1, -1, 0, 1, 1, 1] # space(물고기 번호), position(물고기 위치), direction(이동방향) 만들기 space = [] direction = [] position = [0] * 17 for x in range(4): fish_list = [] d_list = [] for y in range(0, 8, 2): fish_list.append(input_space[x][y]) # 물고기 번호 d_list.append(input_space[x][y + 1]) # 이동 방향 position[input_space[x][y]] = (x, y // 2) # 물고기 별 위치 space.append(fish_list) direction.append(d_list) # 물고기 잡아먹기 eat_fish(0, 0, 0, space, direction, position) print(max_sum_fish)
댓글을 작성하려면
로그인
해야 합니다.
rlaruddk 3년 전
물고기 이동은 문제 없는 것 같은데(제생각)
상어 이동하는 데 최댓값이 자꾸 다르게 나옵니다......
외않되..