Programming 기초/Coding Test

[이코테 # 구현3] 시뮬레이션 문제 : 게임 개발

코딩상륙작전 2023. 6. 7. 02:11

* 게임 개발(p.118)

시간제한 : 40분

전형적인 시뮬레이션 문제

n, m = map(int, input().split())
x, y, direction = map(int, input().split())

M = []
for i in range(n):
    M.append([])
    M[i] = list(map(int, input().split()))
    
M[x][y] = 9	# 시작점 지나온 자리로 표시

dx = [-1, 0, 1, 0]  # 순서대로 북, 동, 남, 서
dy = [0, 1, 0, -1]  # 순서대로 북, 동, 남, 서

def turn_left():
    global direction
    direction -= 1
    if direction == -1:
        direction = 3


not_move = 0
while True:
    turn_left()
    if M[x + dx[direction]][y + dy[direction]] == 0:
        x = x + dx[direction]
        y = y + dy[direction]
        M[x][y] = 9
        not_move = 0
        continue
    elif not_move == 3:
        turn_left()
        turn_left()
        if M[x + dx[direction]][y + dy[direction]] != 1:
            x = x + dx[direction]
            y = y + dy[direction]
            turn_left()
            turn_left()
            not_move = 0
            continue
        else:
            break
    not_move += 1
count = 0

for i in range(n):
    for j in range(m):
        if M[i][j] == 9:
            count += 1
print(count)
print(M)
#입력 예시
M = [
    [1, 1, 1, 1, 1, 1, 1],
    [1, 0, 1, 0, 1, 0, 1],
    [1, 0, 0, 0, 0, 0, 1],
    [1, 0, 1, 1, 0, 1, 1],
    [1, 0, 1, 1, 0, 0, 1],
    [1, 1, 1, 1, 1, 1, 1],
]

M = [
    [1, 1, 1, 1],
    [1, 0, 0, 1],
    [1, 1, 0, 1],
    [1, 1, 1, 1],
]

오답 피드백

  1. 시간 내에 못 풂. (나중에 다시 풀어볼 것)
  2. 문제 곡해. (1회 전진 후 무조건 반시계방향으로 방향전환을 시도해야 함.)
  3. 왼쪽으로 회전하는 함수를 처음에 생각 못함.
  4. 중간 구현이 많이 헷갈리고 경우를 잘 따져야함.

 

주의점

  1. 문제를 바르게 이해하기
  2. global 키워드가 필요한 상황 잘 판단하기
  3. 방향을 설정해서 이동하는 문제 유형에서는 dx, dy라는 별도의 리스트를 만들어 방향을 정하는 것이 효과적이다.

 

* 2회차

n, m=map(int,input().split())
a,b,d=map(int,input().split())

mp=[]	#0은 육지, 1은 바다
for i in range(n):
    mp.append(list(map(int,input().split())))

dir=[[-1,0],[0,1],[1,0],[0,-1]]   # 북,동,남,서 (row,column)

def turn_left(now_dir):
	""" 좌회전 후 방향의 리스트를 반환"""
    dir_result=dir[dir.index(now_dir)-1]
    return dir_result

def forward(now_pos, now_dir):	
	"""앞으로 한 칸 전진한 위치의 row, column을 반환"""
    temp_row=now_pos[0]+now_dir[0]
    temp_column=now_pos[1]+now_dir[1]
    return temp_row, temp_column

def backward(now_pos, now_dir):
	"""뒤로 한 칸 물러선 위치의 row, column을 반환"""
    temp_dir=turn_left(now_dir)
    temp_dir=turn_left(temp_dir)
    temp_row, temp_column=forward(now_pos, temp_dir)
    return temp_row, temp_column

now_pos=[a,b]	# 현재 위치 입력값으로 초기화
now_dir=dir[d]	# 현재 방향 입력값으로 초기화
footprint=[]	# 지나간 위치 기록
stop_count=0	# 제자리에서 좌회전한 횟수 카운트.

#play
while True:
    if now_pos not in footprint:
        footprint.append(now_pos)

    if stop_count>=4:
        b_row,b_column=backward(now_pos, now_dir)
        if mp[b_row][b_column]==1:
            break
        now_pos=[b_row,b_column]
        stop_count=0

    f_row,f_column=forward(now_pos, now_dir)
    if mp[f_row][f_column]==1 or [f_row,f_column] in footprint:
        now_dir=turn_left(now_dir)
        stop_count+=1
        continue

    now_pos=[f_row,f_column]
    stop_count=0

print(len(footprint))

피드백 : 

시간제한 40분 내에 못 풂...