[Algorithm] Implementation

Updated:

๐Ÿ“ฃ
๋ณธ ํฌ์ŠคํŠธ๋Š” โ€˜์ด๊ฒƒ์ด ์ทจ์—…์„ ์œ„ํ•œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค with ํŒŒ์ด์ฌโ€™์„ ๊ณต๋ถ€ํ•˜๊ณ  ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค :)
click > (์ด์ฝ”ํ…Œ 2021) ์ด๊ฒƒ์ด ์ทจ์—…์„ ์œ„ํ•œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค with ํŒŒ์ด์ฌ

Implementation

์ด๋ก 

๊ตฌํ˜„์ด๋ž€ โ€˜๋จธ๋ฆฟ์†์— ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์†Œ์Šค์ฝ”๋“œ๋กœ ๋ฐ”๊พธ๋Š” ๊ณผ์ •โ€™์ด๋‹ค.

์™„์ „ ํƒ์ƒ‰(Brute Force), ์‹œ๋ฎฌ๋ ˆ์ด์…˜(Simulation) ์œ ํ˜•์„ ๋ชจ๋‘ ๊ตฌํ˜„ ์œ ํ˜•์œผ๋กœ ๋ฌถ์–ด์„œ ๋‹ค๋ฃฌ๋‹ค. ์™„์ „ ํƒ์ƒ‰์€ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฃผ์ € ์—†์ด ๋‹ค ๊ณ„์‚ฐํ•˜๋Š” ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์˜๋ฏธํ•˜๊ณ , ์‹œ๋ฎฌ๋ ˆ์ด์…˜์€ ๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ•œ ๋‹จ๊ณ„์”ฉ ์ฐจ๋ก€๋Œ€๋กœ ์ง์ ‘ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฌธ์ œ ์œ ํ˜•์ด๋‹ค.

๊ตฌํ˜„ ์‹œ ๊ณ ๋ คํ•ด์•ผ ํ•  ๋ฉ”๋ชจ๋ฆฌ ์ œ์•ฝ ์‚ฌํ•ญ

C/C++ ์™€ ์ž๋ฐ”์—์„œ ์ •์ˆ˜ํ˜• ์ข…๋ฅ˜์— ๋”ฐ๋ฅธ ๋ฒ”์œ„

์ •์ˆ˜ํ˜• ์ข…๋ฅ˜ ์ž๋ฃŒํ˜•์˜ ํฌ๊ธฐ ์ž๋ฃŒํ˜•์˜ ๋ฒ”์œ„
int 4byte -2,147,483,648 ~ 2,147,483,647
long long 8byte -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807
BigInteger(class) ๊ฐ€๋ณ€์  ์ œํ•œ ์—†์Œ

ํŒŒ์ด์ฌ์—์„œ int ์ž๋ฃŒํ˜• ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜์— ๋”ฐ๋ฅธ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰

๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜(๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด) ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰
1,000 ์•ฝ 4KB
1,000,000 ์•ฝ 4MB
10,000,000 ์•ฝ 40MB

๋ณดํ†ต ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ์—์„œ 128 ~ 512MB ์ •๋„ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ์„ ๋‘”๋‹ค. ํŒŒ์ด์ฌ์€ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋Ÿ‰์ด ๋งŽ์„ ๋•Œ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ฃผ์˜ ํ•˜์ž!

[์˜ˆ์ œ 4-1] ์ƒํ•˜์ขŒ์šฐ

N = int(input())
x, y = 1, 1
plans = input().split()

dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
move_type = ['L', 'R', 'U', 'D']

for plan in plans:
    for i in range(len(move_type)):
        if plan == move_type[i]:
            nx = x + dx[i]
            ny = y + dy[i]
    if nx <1 or ny < 1 or nx > N or ny > N:
        continue
    x, y = nx, ny

print(x,y)

[์˜ˆ์ œ 4-2] ์‹œ๊ฐ

H = int(input())

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

์‹ค์ „๋ฌธ์ œ

[์‹ค์ „๋ฌธ์ œ 1] ์™•์‹ค์˜ ๋‚˜์ดํŠธ

input_data = input()
row = int(input_data[1])
column = int(ord(input_data[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 s in steps:
    next_row = row + s[0]
    next_column = column + s[1]
    if 1 <= next_row <= 8 and 1 <= next_column <= 8:
        result += 1
print(result)

[์‹ค์ „๋ฌธ์ œ 2] ๊ฒŒ์ž„ ๊ฐœ๋ฐœ

# ์ •๋ณด ์ž…๋ ฅ๋ฐ›๊ธฐ
N, M = map(int, input().split())
A, B, direct = list(map(int, input().split()))
maps = []
for _ in range(N):
    maps.append(list(map(int, input().split())))

visited = [[0] * M for _ in range(N)]
visited[A][B] = 1
move = [(-1, 0), (0, 1), (1, 0), (0, -1)]

def turn_left():
    global direct
    direct -= 1
    if direct < 0:
        direct = 3

count = 1
turns = 0
while True:
    turn_left()
    a = A + move[direct][0]
    b = B + move[direct][1]
    if 0 <= a < N and 0 <= b < M and visited[a][b] == 0 and maps[a][b] == 0:
        A = a
        B = b
        visited[a][b] = 1
        count += 1
        turns = 0
        continue
    else:
        turns += 1
    if turns == 4: # ๋‹ค์‹œ ์›๋ž˜ ๋ฐฉํ–ฅ
        a = A - move[direct][0]
        b = B - move[direct][0]
        if maps[a][b] == 0:
            A = a
            B = b
            turns = 0
        else:
            break
print(count)

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ