반응형
Notice
Recent Posts
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 |
Tags
- Homogeneous ODEs
- 공수 문제풀이
- Problem set 2.7
- Advanced Engineering Mathematics
- Ode
- 비제차 상미분 방정식
- vocabulary
- 영어회화
- SW역량테스트
- 문제풀이
- 백준
- ODEs
- Problem set 1.5
- 스피치 연습
- speech
- English
- 공업수학
- 삼성SW역량테스트
- 공학수학
- kreyszig
- 코딩테스트
- 공수
- Conversation
- Python
- 공수1
- 미방
- Nonhomogeneous ODEs
- 미분방정식
- homogeneous
- Problem Set 1.4
Archives
- Today
- Total
한걸음
백준 13901 : 로봇 본문
반응형
https://www.acmicpc.net/problem/13901
13901번: 로봇
첫 번째 줄에는 방의 크기 R, C(3 ≤ R, C ≤ 1,000)가 입력된다. 두 번째 줄에는 장애물의 개수 k(0 ≤ k ≤ 1,000)가 입력된다. 다음 k개의 줄에는 각 장애물 위치 br(0 ≤ br ≤ R – 1), bc(0 ≤ bc ≤ C - 1)가
www.acmicpc.net
1. 이중 반복문에서 조심해야할 부분!
로봇은 정해진 방향대로 움직이되, 갈 수 있는 곳이 없을 때까지 움직일 수 있다!
현재위치를 큐에 넣고 진행 방향대로 계속 가고 나서 그다음 방향 결정하기 전에 4방향으로 갈 수 있는지 없는지 탐색을 해주고 4방향다 진행 불가할때 반복문을 빠져나올 수 있도록 해주어야 한다.
![](https://blog.kakaocdn.net/dn/cMhud1/btrL7u9Ae4t/86OQTkdFpiPGtpZIxcbFS0/img.png)
2. 전체코드
메모리 : 120720 KB, 시간 : 136 ms, 풀이시간 : 20 min
R, C = map(int, input().split())
hand = int(input())
hand_info = [list(map(int, input().split())) for _ in range(hand)]
sx, sy = map(int, input().split())
# 1 : 위, 2: 아래, 3: 왼쪽, 4 : 오른쪽
d_info = list(map(int, input().split()))
# 없음, 상, 하, 좌, 우
dx = [0, -1, 1, 0, 0]
dy = [0, 0, 0, -1, 1]
# Board
board = [[0 for _ in range(C)] for _ in range(R)]
visited = [[False for _ in range(C)] for _ in range(R)]
for i in range(hand):
x, y = hand_info[i]
board[x][y] = -1
def solve():
q = [[sx, sy]]
visited[sx][sy] = True
it = 0
while True :
# 지정한 방향으로 한 칸 갔을 때 갈 수 있냐?
direction = d_info[it]
while q :
x, y = q[0]
del q[0]
nx = x + dx[direction]
ny = y + dy[direction]
if 0 <= nx < R and 0 <= ny < C and not visited[nx][ny] :
if board[nx][ny] == 0 :
visited[nx][ny] = True
q.append([nx, ny])
cnt = 0
q = [[x, y]]
for d in d_info :
nx = x + dx[d]
ny = y + dy[d]
if nx < 0 or nx >= R or ny < 0 or ny >= C : cnt +=1
elif visited[nx][ny] : cnt += 1
elif board[nx][ny] != 0 : cnt += 1
if cnt == 4 : break
it += 1
if it >= 4 : it = 0
return [x, y]
result = solve()
print(" ".join(map(str, result)))
반응형
'Coding Test' 카테고리의 다른 글
백준 23288 : 주사위 굴리기 2 (2) | 2024.02.12 |
---|---|
백준 9372 : 상근이의 여행 (0) | 2024.02.07 |
백준 14925 : 목장 건설하기 (2) | 2024.01.26 |
백준 23291 : 어항 정리 (3) | 2023.12.18 |
백준 1941 : 소문난 칠공주(Python) (2) | 2023.12.11 |