완전탐색 : 모든 경우의 수를 주저 없이 다 계산하는 해결 방법 시뮬레이션 : 문제에서 제시한 알고리즘을 한 단계씩 차례대로 직접 수행
int 자료형 데이터 개수에 따른 메모리 사용량
| 데이터의 개수(리스트 길이) | 메모리 사용량 |
|---|---|
| 1,000 | 약 4KB |
| 1,000,000 | 약 4MB |
| 10,000,000 | 약 40MB |
<aside> ❗ [Hint]
데이터 개수가 100만 개 이하일 때는 완전 탐색 사용하는 것이 적절
</aside>
place = input()
row = int(place[1])
column = int(ord(place[0])) - int(ord('a')) + 1
steps = [(2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (1, -2), (-1, 2), (-1, -2)]
result = 0
for step in steps:
dx = row + step[0]
dy = column + step[1]
if 0 < dx < 9 and 0 < dy < 9:
result += 1
print(result)
Tip. 나이트가 움직일 수 있는 전체 경우의 수를 검사
N, M = map(int, input().split())
A, B, direction = map(int, input().split())
d = [[0] * M for _ in range(N)]
d[A][B] = 1 # 현재 캐릭터의 좌표 방문 처리
inform = []
for i in range(N):
inform.append(list(map(int, input().split())))
# 북, 동, 남, 서 이동 정의
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
def turn_left():
global direction # 전역변수 사용
direction -= 1
if direction == -1:
direction = 3
count = 1
turn_time = 0
while True:
turn_left()
nx = A + dx[direction]
ny = B + dy[direction]
if inform[nx][ny] == 0 and d[nx][ny] == 0:
d[nx][ny] = 1
A, B = nx, ny
count += 1
turn_time = 0
continue
else:
turn_time += 1
if turn_time == 4:
nx = A - dx[direction]
ny = B - dy[direction]
if inform[nx][ny] == 0:
A, B = nx, ny
else:
break
turn_time = 0
print(count)
Tip. 전형적인 시뮬레이션 문제 - 문제에서 요구한 내용대로 코드를 작성