일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 거울 설치
- 키패드 누르기
- python
- HTML 기초
- 1789번
- 감소하는 수
- 프로그래머스
- SW ExpertAcademy
- 수식 최대화
- 12869번
- 14499번
- 경주로 건설
- 미세먼지 안녕!
- 스타트 택시
- 16234번
- 2020 카카오 인턴십
- QueryDSL 기초
- 백준 알고리즘
- 17144번
- 9095번
- 보석 쇼핑
- SW Expert Academy
- 베스트엘범
- 빛의 경로 사이클
- 19238번
- 1038번
- 어른 상어
- 파이썬
- 15686번
- 12865번
Archives
- Today
- Total
보물창고 블로그
백준 알고리즘 19237번 어른 상어 풀이 With Python 본문
728x90
문제 링크: https://www.acmicpc.net/problem/19237
19237번: 어른 상어
첫 줄에는 N, M, k가 주어진다. (2 ≤ N ≤ 20, 2 ≤ M ≤ N2, 1 ≤ k ≤ 1,000) 그 다음 줄부터 N개의 줄에 걸쳐 격자의 모습이 주어진다. 0은 빈칸이고, 0이 아닌 수 x는 x번 상어가 들어있는 칸을 의미
www.acmicpc.net
청소년 상어 문제에서 성장한 어른 상어 문제입니다. 문제 설명 그대로 구현하는 시뮬레이션 문제입니다. while 문을 사용하여 매시간마다 time을 1씩 증가시켰고, 1000을 넘는 순간 -1을 리턴하고, 죽은 상어의 x좌표를 -1로 하여 체크를 건너뛰었습니다. 그리고 상어를 1마리씩 줄일 때마다 카운트 변수를 줄여 상어 1마리만 남았을 때의 시간을 반환하도록 solution함수를 만들었습니다. 소스코드는 아래와 같습니다.
import sys
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
n, m, k = map(int, sys.stdin.readline().split())
sharks = [[0, 0] for _ in range(m)]
board = []
smell = {}
for i in range(n):
list1 = list(map(int, sys.stdin.readline().split()))
board.append(list1)
for j in range(n):
if list1[j]:
sharks[list1[j] - 1] = [i, j]
smell[(i, j)] = [0, list1[j] - 1]
direction = list(map(int, sys.stdin.readline().split()))
dirlist = [list(map(int, sys.stdin.readline().split())) for _ in range(4 * m)]
def solution(board, smell, sharks, direction, dirlist):
time = 0
count = m
while 1:
time += 1
if time > 1000:
return -1
for i in range(m):
# 죽은거 처리
if sharks[i][0] == -1:
continue
else:
x, y = sharks[i]
flag = 1
# 상어 지금 바라보는 방향
d = direction[i]
dlist = dirlist[4 * i + d - 1]
for d1 in dlist:
nx = x + dx[d1 - 1]
ny = y + dy[d1 - 1]
if -1 < nx < n and -1 < ny < n:
if smell.get((nx, ny)) is None or time - smell[(nx, ny)][0] > k:
flag = 0
# 가려는 칸에 다른 상어 있을 때
if board[nx][ny]:
count -= 1
if count == 1:
return time
# 현재 상어가 강한 경우
if board[nx][ny] > i + 1:
sharks[board[nx][ny] - 1][0] = -1
board[nx][ny] = i + 1
direction[i] = d1
sharks[i][0] = nx
sharks[i][1] = ny
# 현재 상어가 약한 경우
else:
sharks[i][0] = -1
# 다른 상어 없을 때
else:
sharks[i][0] = nx
sharks[i][1] = ny
board[nx][ny] = i + 1
direction[i] = d1
board[x][y] = 0
break
# 4방향에 다 냄새남을 때
if flag:
for d1 in dlist:
nx = x + dx[d1 - 1]
ny = y + dy[d1 - 1]
if -1 < nx < n and -1 < ny < n and smell[(nx, ny)][1] == i:
direction[i] = d1
board[nx][ny] = i + 1
board[x][y] = 0
sharks[i][0]=nx
sharks[i][1]=ny
break
for w in range(m):
if sharks[w][0] == -1:
continue
else:
smell[(sharks[w][0], sharks[w][1])] = [time, w]
ans = solution(board, smell, sharks, direction, dirlist)
print(ans)
'알고리즘 풀이 > 백준 알고리즘' 카테고리의 다른 글
백준 알고리즘 14499번 주사위 굴리기 풀이 With Python (0) | 2020.06.30 |
---|---|
백준 알고리즘 19238번 스타트 택시 풀이 With Python (0) | 2020.06.30 |
백준 알고리즘 19236번 청소년 상어 풀이 With Python (1) | 2020.06.30 |
백준 알고리즘 19235번 모노미도미노 풀이 With Python (6) | 2020.06.30 |
백준 알고리즘 14501번 퇴사 풀이 With Python (0) | 2020.04.26 |
Comments