Algorithm/개념

구현알고리즘

vluevy 2021. 7. 12. 00:03
728x90
반응형

- 완전탐색, 시뮬레이션 등의 유형이 있음

- 사실상 그냥 구현하는 코드를 짜는 알고리즘?

상하좌우

N=int(input())
X,Y=1,1
map=list(map(str,input().split()))
for i in map:
    if i=='R':
        if Y+1>=1 and Y+1<=5:
            Y+=1
    if i=='L':
        if Y-1>=1 and Y-1<=5:
            Y-=1
    if i=='U':
        if X+1>=1 and X+1<=5:
            X+=1
    if i=='D':
        if X-1>=1 and X-1<=5:
            X+=1
print(X,Y)

13m solved

시각

N=int(input())
count=0
for i in range (N+1):
    for j in range(60):
        for k in range(60):
            if '3' in str(k)+str(j)+str(i):
                count+=1
print(count)

5m solved

왕실의 나이트

location=input()
map=[(2,1),(2,-1),(-2,1),(-2,-1),(1,2),(1,-2),(-1,2),(-1,-2)]
row=int(location[1])
col=int(ord(location[0]))-int(ord('a'))+1
count=0
for i in map:
    if row+i[0]>=1 and row+i[0]<=8:
        if col+i[1]>=1 and col+i[1]<=8:
            count+=1
print(count)

25m solved

2차원 튜플의 for문을 돌리는 것에서 조금 헷갈림

게임개발(백준 로봇청소기)

N,M=map(int,input().split())
x,y,d=map(int,input().split())
map_data=[list(map(int,input().split()))for i in range(N)]
dx=[-1,0,1,0]
dy=[0,1,0,-1]

map_data[x][y]=2
count=1

while True:
    check=False
    for _ in range(4):
        d=(d+3)%4
        nx=x+dx[d]
        ny=y+dy[d]
        if 0<=nx<N and 0<=ny<M:
            if map_data[nx][ny]==0:
                map_data[nx][ny]=2
                x,y=nx,ny
                count+=1
                check=True 
                break
    if check==False:
        nx=x-dx[d]
        ny=y-dy[d]
        if 0<=nx<N and 0<=ny<M:
            if map_data[nx][ny]==1:
                break
            else:
                x,y=nx,ny
                continue
print(count)

check 변수로 4방향 전부 방문했는지 확인 여부 거르는 게 어려웠다.

반응형