[Algorithm] Implementation
๐ฃ
๋ณธ ํฌ์คํธ๋ โ์ด๊ฒ์ด ์ทจ์
์ ์ํ ์ฝ๋ฉ ํ
์คํธ๋ค 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)
๋๊ธ๋จ๊ธฐ๊ธฐ