알고리즘

구현

iemxl 2023. 7. 5. 19:01

코딩테스트에서 "구현"

: 머릿속에 있는 알고리즘을 소스코드로 바꾸는 과정

 

#상하좌우

n=int(input())
m=input().split()
result=[1,1]
for i in m:
    if result[1]>1 and i=='L':
        result[1]-=1
    elif result[1]<5 and i=='R':
        result[1]+=1
    elif result[0]>1 and i=='U':
        result[0]-=1
    elif result[0]<5 and i=='D':
        result[0]+=1
print(*result,sep=' ')

 

#시각

n=int(input())
cnt=60*60*(n+1)
if n<3:
    cnt-=(n+1)*5*9*45
else:
    cnt-=n*5*9*45
print(cnt)

 

#왕실의 나이트

n=input()
row=int(n[1])
col=int(ord(n[0])-int(ord('a')))+1
steps=[(-2,-1),(-1,-2),(1,-2),(2,-1),(2,1),(1,2),(-1,2),(-2,1)]
result=0
for step in steps:
    next_row=row+step[0]
    next_col=col+step[1]
    if next_row>=1 and next_row<=8 and next_col>=1 and next_col<=8:
        result+=1
print(result)

 

#게임개발

n,m=map(int,input().split())
d=[[0]*m for _ in range(n)]
x,y,direction=map(int,input().split())
d[x][y]=1
arr=[]
for i in range(n):
    arr.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=x+dx[direction]
    ny=y+dy[direction]
    if d[nx][ny]==0 and arr[nx][ny]==0:
        d[nx][ny]=1
        x=nx
        y=ny
        count+=1
        turn_time=0
        continue
    else: 
        turn_time+=1
    if turn_time==4:
        nx=x-dx[direction]
        ny=y-dy[direction]
        if arr[nx][ny]==0:
            x=nx
            y=ny
        else:
            break
        turn_tim=0
print(count)

'알고리즘' 카테고리의 다른 글

HEAP #힙 자료구조 #heapq #Dijkstra #다익스트라 알고리즘  (0) 2024.05.10
정렬  (0) 2024.05.09
DFS/BFS  (0) 2023.08.03
그리디 알고리즘  (0) 2023.06.28