일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 16234번
- 파이썬
- 2020 카카오 인턴십
- 빛의 경로 사이클
- QueryDSL 기초
- 미세먼지 안녕!
- 9095번
- SW Expert Academy
- 키패드 누르기
- 1789번
- 경주로 건설
- 거울 설치
- 베스트엘범
- 감소하는 수
- 19238번
- 12869번
- HTML 기초
- 백준 알고리즘
- 1038번
- 프로그래머스
- 14499번
- python
- 스타트 택시
- 어른 상어
- 17144번
- 15686번
- 보석 쇼핑
- SW ExpertAcademy
- 12865번
- 수식 최대화
Archives
- Today
- Total
보물창고 블로그
SW Expert Academy 2105. [모의 SW 역량테스트] 디저트 카페 풀이 With Python 본문
알고리즘 풀이/SW Expert Academy
SW Expert Academy 2105. [모의 SW 역량테스트] 디저트 카페 풀이 With Python
홋 메 2020. 3. 26. 01:07728x90
이 문제를 bfs(너비 우선 탐색) 기법으로 해결하였다. 가장 먼저 시작할 때 방향을 오른쪽 아래로 방향을 고정하고, 이후에 큐에 지나간 포인트 값과 마지막 방향을 저장하여서 방향이 바뀔 때마다 count값을 늘렸고, 방향이 바뀐 횟수가 4번이고, 제자리로 돌아왔을 때 지나온 포인트의 개수를 answer값과 비교하여 업데이트하였다. 아래는 문제를 해결한 코드이다.
from collections import deque
t = int(input())
dx = [-1, 1, 1, -1]
dy = [-1, -1, 1, 1]
for test in range(1, t + 1):
answer = -1
n = int(input())
map1 = [list(map(int, input().split())) for _ in range(n)]
for i in range(n):
for j in range(n):
queue = deque()
queue.append([i, j, [map1[i][j]], 0, -1])
while queue:
x, y, shops, count, ld = queue.popleft()
if x == i and y == j and count == 4:
if answer < len(shops):
answer = len(shops)
elif count == 0:
nx = x + dx[2]
ny = y + dy[2]
if -1 < nx < n and -1 < ny < n and map1[nx][ny] != map1[i][j]:
queue.append([nx, ny, shops + [map1[nx][ny]], 1, 2])
elif count < 5:
for d in range(4):
nx = x + dx[d]
ny = y + dy[d]
if -1 < nx < n and -1 < ny < n:
if nx == i and ny == j and count>1:
if ld == d:
queue.append([nx, ny, shops, count, d])
else:
queue.append([nx, ny, shops, count + 1, d])
elif map1[nx][ny] not in shops:
if ld == d:
queue.append([nx, ny, shops + [map1[nx][ny]], count, d])
else:
queue.append([nx, ny, shops + [map1[nx][ny]], count + 1, d])
print('#{} {}'.format(test, answer))
'알고리즘 풀이 > SW Expert Academy' 카테고리의 다른 글
SW Expert Academy 4012. [모의 SW 역량테스트] 요리사 풀이 With Python (0) | 2020.03.26 |
---|---|
SW Expert Academy 2117. [모의 SW 역량테스트] 홈 방범 서비스 풀이 With Python (0) | 2020.03.26 |
SW Expert Academy 2477. [모의 SW 역량테스트] 차량 정비소 풀이 With Python (0) | 2020.03.25 |
SW Expert Academy 1949. [모의 SW 역량테스트] 등산로 조성 풀이 With Python (0) | 2020.03.25 |
SW Expert Academy 5644. [모의 SW 역량테스트] 무선 충전 (0) | 2020.03.10 |
Comments