일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
Tags
- 1038번
- 미세먼지 안녕!
- python
- SW Expert Academy
- 프로그래머스
- 빛의 경로 사이클
- 베스트엘범
- 15686번
- 보석 쇼핑
- 파이썬
- 경주로 건설
- 수식 최대화
- 14499번
- 어른 상어
- 2020 카카오 인턴십
- QueryDSL 기초
- SW ExpertAcademy
- 감소하는 수
- 19238번
- 키패드 누르기
- 9095번
- 17144번
- 백준 알고리즘
- 스타트 택시
- 거울 설치
- 1789번
- 12869번
- HTML 기초
- 12865번
- 16234번
Archives
- Today
- Total
보물창고 블로그
백준 알고리즘 새로운 게임2 풀이 With Python 본문
728x90
문제 링크: https://www.acmicpc.net/problem/17837
문제는 아래와 같습니다.
이번 문제는 위에 쌓는 것을 어떻게 구현하는가의 문제였다. 나는 리스트를 통해서 쌓는 것을 구현하였다. 코드는 아래와 같다.
import sys
def solution(map1, map2, horses):
t = 1
while 1:
if t > 1000:
return -1
for i in range(k):
x, y, d = horses[i]
nx = x + dx[d]
ny = y + dy[d]
# 파란색이거나 영역밖이면
if not (-1 < nx < n and -1 < ny < n) or map1[nx][ny] == 2:
d ^= 1
nx = x + dx[d]
ny = y + dy[d]
if not (-1 < nx < n and -1 < ny < n) or map1[nx][ny] == 2:
nx = x
ny = y
horses[i] = [nx, ny, d]
if nx == x and ny == y:
continue
# 옮긴 뒤의 색이 하얀색
idx = map2[x][y].index(i)
for p in map2[x][y][idx + 1:]:
horses[p][0] = nx
horses[p][1] = ny
if map1[nx][ny] == 0:
map2[nx][ny] += map2[x][y][idx:]
# 옮긴 뒤에 색이 빨간색
elif map1[nx][ny] == 1:
map2[nx][ny] += map2[x][y][idx:][::-1]
map2[x][y] = map2[x][y][:idx]
if len(map2[nx][ny]) > 3:
return t
t += 1
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]
n, k = map(int, sys.stdin.readline().split())
map1 = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]
horses = []
map2 = [[[] * n for _ in range(n)] for _ in range(n)]
for i in range(k):
x, y, d = map(int, sys.stdin.readline().split())
horses.append([x - 1, y - 1, d - 1])
map2[x - 1][y - 1].append(i)
ans=solution(map1,map2,horses)
print(ans)
'알고리즘 풀이 > 백준 알고리즘' 카테고리의 다른 글
백준 알고리즘 19235번 모노미도미노 풀이 With Python (6) | 2020.06.30 |
---|---|
백준 알고리즘 14501번 퇴사 풀이 With Python (0) | 2020.04.26 |
백준 알고리즘 17822번 원판 돌리기 풀이 With Python (0) | 2020.03.10 |
백준 알고리즘 17825번 주사위 윷놀이 풀이 With Python (0) | 2020.03.10 |
백준 알고리즘 13458번 시험 감독 풀이 With Python (0) | 2020.03.09 |
Comments