728x90
예제4-1
- 상하좌우
여행가 A는 N * N 크기의 정사각형 공간 위에 서있다. 이공간은 1 * 1 크기의 정사각형으로 나누어져 있다.
가장 왼쪽 위 좌표는 (1, 1)이며 가장 오른 쪽 아래 좌표는 (N, N)에 해당한다. 여행가 A는 상, 하 ,좌 ,우 방향으로 이동할 수 있으며
시작좌표는 항상(1, 1)이다. 우리앞에는 여행가가 A가 이동할 계획서가 놓여있다.
- 계획서에는 하나의 줄에 띄어쓰기를 기준으로 하여 L, R, U, D중 하나의 문자가 반복적으로 적혀 있다. 각 문자의 의미는
다음과 같다.
L : 왼쪽으로 한 칸 이동
R : 오른쪽으로 한 칸 이동
U : 위로 한 칸 이동
D : 아래로 한 칸 이동
- 이때 여행가 A가 N × N 크기의 정사각형 공간을 벗어나는 움직임은 무시된다
예를 들어 (1, 1)의 위치에서 L 혹은 U를 만나면 무시된다
다음은 N = 5인 지도와 계획이다
입력
5
R R U D D
출력
3 4
#코드
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
|
import sys
if __name__ == "__main__":
input = sys.stdin.readline
n = 5
orderList = ['R', 'R', 'R', 'U', 'D', 'D']
matrix = [[-1]*(n+2) for _ in range(n+2)]
for i in range(n):
for j in range(n):
matrix[i+1][j+1] = 0
# 상, 하, 좌, 우
dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]
move_types = ['U', 'D', 'L', 'R']
posY, posX = 1, 1
for order in orderList:
for i in range(len(move_types)):
if order == move_types[i]:
nY = posY + dy[i]
nX = posX + dx[i]
if matrix[nY][nX] != -1:
posY, posX = nY, nX
print(posY, posX)
|
cs |
예제4-2
- 시각
#코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import sys
if __name__ == "__main__":
input = sys.stdin.readline
n = int(input())
cnt = 0
for i in range(n+1):
for j in range(60):
for k in range(60):
if '3' in str(i)+str(j)+str(k):
cnt += 1
print(cnt)
|
cs |
실전문제
- 왕실나이트
#코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import sys
if __name__ == "__main__":
input = sys.stdin.readline
input_data = input()
row = int(input_data[1])
col = int(ord(input_data[0])) - int(ord('a')) + 1
#나이트 이동 가능 방향 8가지
steps = [(-2, -1), (-1, -2), (1, -2), (2, -1), (2, 1), (1, 2), (-1, 2), (-2, 1)]
result = 0
for step in steps:
nRow = row + step[0]
nCol = col + step[1]
if 1 <= nRow and nRow <= 8 and 1 <= nCol and nCol <= 8:
result += 1
print(result)
|
cs |
실전문제
- 게임 개발
# 코드
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
import sys
def turnLeft():
global direction
direction -= 1
if direction == -1:
direction = 3
if __name__ == "__main__":
input = sys.stdin.readline
n, m = map(int, input().split())
d = [[False]*m for _ in range(n)] #방문여부 기록
row, col, direction = map(int, input().split())
d[row][col] = True
mapInfo = []
for i in range(n):
mapInfo.append(list(map(int, input().split())))
# 북, 동, 남, 서
dy = [-1, 0, 1, 0]
dx = [0, 1, 0, -1]
# 시뮬레이션 시작
count = 1
turn_time = 0
while True:
# 왼쪽으로 회전
turnLeft()
nY = row + dy[direction]
nX = col + dx[direction]
# 회전한 이후 정면에 가보지 않은 칸이 존재할 경우
if d[nY][nX] == False and mapInfo[nY][nX] == 0:
d[nY][nX] = True
row = nY
col = nX
count += 1
turn_time = 0
continue
else: # 회전한 이후 정면에 가보지 않은 칸이 없거나 바다인 경우
turn_time += 1
if turn_time == 4: # 네 방향 모두 갈 수 없을 경우
nY = row - dy[direction]
nX = col - dx[direction]
if mapInfo[nY][nX] == 0:
row = nY
col = nX
else:
break
turn_time = 0
print(count)
|
cs |
반응형
'CS > 알고리즘_[교재]이것이 취업을 위한 코딩테스트다' 카테고리의 다른 글
[PART2]CH.07_이진탐색 (0) | 2021.06.17 |
---|---|
[PART2]CH.06_정렬 (0) | 2021.06.16 |
[PART2] CH.05_DFS/BFS (0) | 2021.06.13 |
[PART2] CH.03_그리디_1_당장 좋은 것만 선택하는 그리디 (0) | 2021.06.08 |
목록 및 학습 계획 (0) | 2021.06.08 |