반응형
Recent Posts
Notice
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 코딩테스트준비
- leetcode
- 99클럽
- 항해99
- boj 2309
- BFS
- softeer
- dfs
- 스택
- 구현
- Python
- til
- 개발자 취업
- 백준 2309
- 활성화 함수
- 코딩테스트 준비
- 알고리즘
- 해시
- 큐
- 딥러닝
- python 2309
- 개발자취업
- 백준
- BOJ
- 파이썬
- 혁펜하임
- 프로그래머스
- 기능개발
- 99항해
- easy 딥러닝
Archives
- Today
- Total
동까의 코딩
99클럽 코테 스터디 TIL 본문
반응형
https://www.acmicpc.net/problem/2151
from collections import deque
import sys
input = sys.stdin.readline
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
def bfs(x, y, dir):
q.append([x, y, dir])
c[x][y][dir] = 1
ans = []
while q:
x, y, dir = q.popleft()
nx = x + dx[dir]
ny = y + dy[dir]
if 0 <= nx < n and 0 <= ny < n:
if not c[nx][ny][dir] or c[nx][ny][dir] > c[x][y][dir]:
if a[nx][ny] != '*':
c[nx][ny][dir] = c[x][y][dir]
if nx == fx and ny == fy:
ans.append(c[nx][ny][dir])
continue
q.append([nx, ny, dir])
if a[nx][ny] == '!':
turn(nx, ny, dir)
print(min(ans)-1)
def turn(x, y, dir):
ndir = [(dir+1) % 4, (dir+3) % 4]
for d in ndir:
if not c[x][y][d] or c[x][y][d] > c[x][y][dir] + 1:
c[x][y][d] = c[x][y][dir] + 1
q.append([x, y, d])
n = int(input())
q = deque()
c = [[[0]*4 for _ in range(n)] for _ in range(n)]
a, temp = [], []
for i in range(n):
row = list(input().strip())
a.append(row)
for j in range(n):
if row[j] == '#':
temp.extend([i, j])
sx, sy, fx, fy = temp
for i in range(4):
nx = sx + dx[i]
ny = sy + dy[i]
if 0 <= nx < n and 0 <= ny < n:
if a[nx][ny] != '*':
dir = i
break
bfs(sx, sy, dir)
반응형
'문제 풀이 > 99클럽' 카테고리의 다른 글
99클럽 코테 스터디 TIL (0) | 2025.02.19 |
---|---|
99클럽 코테 스터디 TIL (0) | 2025.02.18 |
99클럽 코테 스터디 TIL (0) | 2025.02.13 |
99클럽 코테 스터디 TIL (0) | 2025.02.12 |
99클럽 코테 스터디 TIL 11일차 (0) | 2025.02.07 |