๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐ˜์‘ํ˜•

 

์™„์ „ ํƒ์ƒ‰ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋‹ค ๊ณ„์‚ฐ
์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•œ ๋‹จ๊ณ„์”ฉ ์ฐจ๋ก€๋Œ€๋กœ ์ง์ ‘ ์ˆ˜ํ–‰
  • int ์ž๋ฃŒํ˜• ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜์— ๋”ฐ๋ฅธ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰
    • 1,000 - 4kb
    • 1,000,000 - 4mb
    • 10,000,000 - 40mb
  • ์‹œ๊ฐ„ ์ œํ•œ, ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ์–ด๋–ค ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์“ธ์ง€ ์ •ํ•ด์•ผ ํ•œ๋‹ค! 
    • 1์ดˆ, 100๋งŒ๊ฐœ ๋ฐ์ดํ„ฐ -> O(NlogN)

์˜ˆ์ œ 4-1 (์‹œ๋ฎฌ๋ ˆ์ด์…˜) ์ƒํ•˜์ขŒ์šฐ

  • ์ œํ•œ : 1์ดˆ, 128mb
  • N x N ์ •์‚ฌ๊ฐํ˜• ๊ณต๊ฐ„. ๊ฐ€์žฅ ์™ผ์ชฝ ์œ„ (1, 1)
    ๊ณ„ํš์„œ์—๋Š” ํ•˜๋‚˜์˜ ์ค„์— ๋„์–ด์“ฐ๊ธฐ๋กœ ๊ตฌ๋ถ„. L์™ผR์˜คU์œ„D์•„๋ž˜ ๊ณต๊ฐ„ ๋ฐ–์€ ๋ฌด์‹œ.
  • input ์ฒซ์งธ ์ค„ ๊ณต๊ฐ„์˜ ํฌ๊ธฐ 1 <= N <= 100 , ๋‘˜์งธ ์ค„ ๊ณ„ํš์„œ ๋‚ด์šฉ (1 <= ์ด๋™ ํšŸ์ˆ˜ <= 100)
  • output ์ตœ์ข…์ ์œผ๋กœ ๋„์ฐฉํ•  ์ง€์ ์˜ ์ขŒํ‘œ (X, Y)๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ถœ๋ ฅ
# ์ž…๋ ฅ ๋ฐ›๊ธฐ N, ๊ณ„ํš์„œ 
n = int(input())
plans = input().split()
x, y = 1, 1 # ์‹œ์ž‘์  ์ •์˜ ํ•„์š”

""" 
L R U D ๋ฐฉํ–ฅ๊ณผ ์ด๋ฆ„ ์ •์˜ ***
(1,1) (1,2)
(2,1) (2,2)
๋ผ์„œ ์™ผ์ด๋ฉด dy ์—์„œ -1 
"""

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

# ๊ณ„ํš์„œ ๊ธธ์ด๋งŒํผ ๋Œ๋ฉฐ ์ผ์น˜ํ•˜๋Š” ๋Œ€๋กœ ์œ„์น˜ ๋ณ€ํ™˜ 
for plan in plans:
    # ์ด๋™ ํ›„ ์ขŒํ‘œ 
    for i in range(len(move_types)):
        if plan == move_types[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)
Point 1
L, R, U, D ๋ฐฉํ–ฅ๊ณผ ์ด๋ฆ„ ์ •์˜ ๋Š” ๊ทธ๋ƒฅ ์™ธ์›Œ๋ผ
Point 2 
๊ณต๊ฐ„ ๋ฒ—์–ด๋‚˜๋ฉด continue๋กœ ๋ฌด์‹œ 
Point 3 
x, y ๋ฅผ ๋ณ€ํ™˜ ๋œ nx, ny๋กœ ๊นŒ๋จน์ง€๋ง๊ณ  ๋ฐ”๊ฟ”์ฃผ๊ธฐ 

์ฒ˜์Œ์— ํ—ท๊ฐˆ๋ ธ์—ˆ๋Š”๋ฐ ๊ธฐ์กด์˜ ํ–‰ ์—ด๊ณผ ๋ฐ˜๋Œ€๋กœ ์ƒ๊ฐํ•ด์•ผ ํŽธํ•˜๋‹ค. ์ง์ ‘ ํ•œ ๋ฒˆ ์จ์„œ ํ™•์ธํ•˜๋Š” ๊ฒƒ๋„ ์ข‹๋‹ค.


์˜ˆ์ œ 4-2 (์™„์ „ ํƒ์ƒ‰) ์‹œ๊ฐ

  • ์ œํ•œ : 2์ดˆ, 128mb 
  • N์ด ์ž…๋ ฅ๋˜๋ฉด 00์‹œ 00๋ถ„ 00์ดˆ ~ N์‹œ 59๋ถ„ 59์ดˆ ๋ชจ๋“  ์‹œ๊ฐ ์ค‘ 3์ด ํ•˜๋‚˜๋ผ๋„ ํฌํ•จ๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ
  • input N
  • output 3์ด ํ•˜๋‚˜๋ผ๋„ ํฌํ•จ๋˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜
# input & output
h = int(input())
count = 0 
# 3์ค‘ ๋ฐ˜๋ณต 24x60x60
for i in range(h + 1):
    for j in range(60):
        for k in range(60):
            # '3'ํฌํ•จ ๊ทธ๊ฑฐ
            if '3' in str(i) + str(j) + str(k): # str๋ณ€ํ™˜ํ•˜๊ณ  ํ™•์ธ
                count += 1
print(count)
Point 1
00์‹œ 00๋ถ„ 00์ดˆ ~ 23์‹œ 59๋ถ„ 59์ดˆ ๊ฒฝ์šฐ -> 86,400๊ฐ€์ง€ -> 100,000๊ฐœ๋„ ์•ˆ๋ผ์„œ
3์ค‘์œผ๋กœ ์™„ํƒํ•ด๋„ ๊ดœ์ฐฎ๋‹ค ! ๋ฅผ ์•Œ์•„์•ผ ๋จ.
Point 2 
str()๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๋Œ€์ถฉ ~ ๊นŒ์ด๊บผ ์จ๋„ ํŒŒ์ด์ฌ์€ ์•Œ์•„์„œ ์ž˜ ์•Œ์•„๋“ฃ๋Š”๋‹ค.. 

์‹ค์ „ 2 (์‹œ๋ฎฌ๋ ˆ์ด์…˜) ์™•์‹ค์˜ ๋‚˜์ดํŠธ

  • ์ œํ•œ : 1์ดˆ, 128mb 
  • 8x8 ์ขŒํ‘œ ํ‰๋ฉด L์ž ํ˜•ํƒœ๋กœ๋งŒ ์ด๋™. ์ •์› ๋ฐ–์œผ๋ก  ๋‚˜๊ฐˆ ์ˆ˜ ์—†์Œ. ํŠน์ • 2๊ฐ€์ง€ ๊ฒฝ์šฐ๋กœ ์ด๋™
    1. ์ˆ˜ํ‰ ๋‘ ์นธ, ์ˆ˜์ง ํ•œ ์นธ
    2. ์ˆ˜์ง ๋‘ ์นธ, ์ˆ˜ํ‰ ํ•œ ์นธ
    ์‹œ์ž‘์ ์ด (1,a) ์—ด a,b~g,h / ํ–‰ 1,2~7,8
  • input ํ˜„์žฌ ๋‚˜์ดํŠธ๊ฐ€ ์œ„์น˜ํ•œ ๊ณณ์˜ ์ขŒํ‘œ๋ฅผ ์ž…๋ ฅ 'a1'
  • output ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ถœ๋ ฅ '2'

# input str+int
input_data = input()
row = int(input_data[1])
column = int(ord(input_data[0])) - int(ord('a')) + 1 # ๋ฌธ์ž ์•„์Šคํ‚ค - a ์•„์Šคํ‚ค + 1
# ๋‘ ๊ฐ€์ง€ ์ด๋™๋ฐฉ๋ฒ•์— ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜ ***
# ์ˆ˜ํ‰ 2 ์ˆ˜์ง 1, ์ˆ˜์ง 2 ์ˆ˜ํ‰ 1 
steps = [(-2,-1),(-2, 1),(2,-1),(2,1),(1,2),(1,-2),(-1,2),(-1,-2)]

result = 0 # ๊ฐ€์ง“์ˆ˜ ๋‹ด์„ ๊ณต๊ฐ„ 
for step in steps:
    # ์ด๋™ํ•˜๊ณ ์ž ํ•˜๋Š” ์œ„์น˜ ํ™•์ธ ***
    nx_row = row + step[0]
    nx_column = column + step[1]
    # ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ ***
    if nx_row >= 1 and nx_row <= 8 and nx_column >= 1 and nx_column <= 8 :
        result += 1

print(result)
Point 1
์•„์Šคํ‚ค ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ + 1 
Point 2 
๋‘ ๊ฐ€์ง€ ์ด๋™๋ฐฉ๋ฒ•์— ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ ์ˆ˜ ๋ฆฌ์ŠคํŠธ๋กœ ์ž‘์„ฑ. ์ด๊ฑธ ํ•˜๋‚˜ํ•˜๋‚˜ ํ™•์ธํ•˜๊ธฐ
Point 3 
์ด๋™ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์—๋งŒ ๊ฐ€์ง“์ˆ˜ ์ถ”๊ฐ€. and

 


์‹ค์ „ 3 (์‹œ๋ฎฌ๋ ˆ์ด์…˜) ๊ฒŒ์ž„ ๊ฐœ๋ฐœ

  • ์ œํ•œ : 1์ดˆ, 128mb 
  • N x M ์˜ ์ง์‚ฌ๊ฐํ˜•, ์œก์ง€ or ๋ฐ”๋‹ค, ์บ๋ฆญํ„ฐ๋Š” ๋™์„œ๋‚จ๋ถ ์ค‘ ํ•œ ๊ณณ์„ ๋ฐ”๋ผ๋ณธ๋‹ค. 
    ๋งต์˜ ๊ฐ ์นธ์€ (A, B)๋กœ ๋‚˜ํƒ€๋ƒ„. 
    1. ํ˜„์žฌ ์œ„์น˜, ํ˜„์žฌ ๋ฐฉํ–ฅ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐ˜์‹œ๊ณ„ ๋ถ€ํ„ฐ ๊ฐˆ ๊ณณ์„ ์ •ํ•œ๋‹ค. 
    2. ๊ฐ€๋ณด์ง€ ์•Š์€ ์นธ์ด ์žˆ์œผ๋ฉด ์ „์ง„, ์—†์œผ๋ฉด ๋งˆ์ € ๋Œ๋ฆฌ๊ณ  1๋‹จ๊ณ„
    3. ์ด๋ฏธ ๋‹ค ๊ฐ€๋ดค๊ฑฐ๋‚˜ ๋ฐ”๋‹ค๋ฉด ๋ฐฉํ–ฅ์œ ์ง€, ํ•œ ์นธ ๋’ค๋กœ ๊ฐ€๊ณ  ๋‹ค์‹œ searching, ๋’ค๊ฐ€ ๋ฐ”๋‹ค๋ฉด stop
  • input
    ์ฒซ์งธ์ค„ ์„ธ๋กœ N>=3, ๊ฐ€๋กœ M<=50
    ๋‘˜์งธ์ค„ (A, B) ์žˆ๋Š” ์นธ ์ขŒํ‘œ, ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉํ–ฅ d ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ.
    d : 0 ๋ถ 1 ๋™ 2 ๋‚จ 3 ์„œ
    ์…‹์งธ์ค„ 0 ์œก์ง€ 1 ๋ฐ”๋‹ค ๋กœ ๋งต์ด ์ฃผ์–ด์ง. ์ฒ˜์Œ ์บ๋ฆญํ„ฐ ์œ„์น˜๋Š” ๋ฌด์กฐ๊ฑด ์œก์ง€
  • output 
    ์ด๋™์„ ๋งˆ์นœ ํ›„ ์บ๋ฆญํ„ฐ๊ฐ€ ๋ฐฉ๋ฌธํ•œ ์นธ์˜ ์ˆ˜
# input N M / A B d 
n, m = map(int, input().split())

# ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๊ณต๊ฐ„ initialize ํ•„์ˆ˜ *** ๊ณต๊ฐ„๊ณผ ๋ฐ›์•„์•ผํ•˜๋Š” ๋งต์€ ๋‹ค๋ฅด๋‹ค 
d = [[0] * m for _ in range(n)]

x, y, direction = map(int, input().split())
d[x][y] = 1  # ํ˜„์žฌ์ขŒํ‘œ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ *** ๊นŒ๋จน์Œ_๋ฌธ์ œ๋ฅผ ์ž˜ ์ฝ์ž

# input NxM info ***
array = []
for i in range(n):
    array.append(list(map(int, input().split())))

# define ๋ถ ๋™ ๋‚จ ์„œ 
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]

# ํšŒ์ „ํ•จ์ˆ˜ ๋”ฐ๋กœ ์ •์˜ ***
def turn_left():
    global direction  # ์ „์—ญ์œผ๋กœ ๋Œ์–ด์˜จ๋‹ค ***
    direction -= 1
    if direction == -1:  # ๋ถ -> ์„œ
        direction = 3

# ์ถ”๊ฐ€ ๋ณ€์ˆ˜ ์ •์˜ *** ์šฐ์„  ์„œ์žˆ๋Š” ์ž๋ฆฌ๊ฐ€ 1 !
count = 1
turn_time = 0

# ํ˜„์žฌ์˜ ์œ„์น˜๊ฐ€ ์œก์ง€์ด๋ฉด 
while True:
    turn_left()
    nx = x + dx[direction]
    ny = y + dy[direction]

    # ๋งต ๋ฒ”์œ„ ์•ˆ์—์„œ๋งŒ ๋™์ž‘ํ•˜๋„๋ก ์กฐ๊ฑด ์ถ”๊ฐ€ ***
    if (0 <= nx < n) and (0 <= ny < m):
        # ๋ณด๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ–ˆ์„ ๋•Œ ์œก์ง€, ์ด๋™
        if d[nx][ny] == 0 and array[nx][ny] == 0:
            x = nx
            y = ny
            d[x][y] = 1  # ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
            count += 1
            turn_time = 0  # ๋Œ๋ฆฐ๊ฑฐ ์ดˆ๊ธฐํ™”
            continue
        # ๋ณด๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ–ˆ์„ ๋•Œ ๋ฐ”๋‹ค, ๋Œ๋ ค
    turn_time += 1 
    # ์™œ else๋ฅผ ๋นผ์•ผ ์ฝ”๋“œ๊ฐ€ ๋Œ์•„๊ฐ€๋Š” ๊ฑฐ์ง€? ๋ฌด์Šจ ์˜ˆ์™ธ๊ฐ€ ์žˆ์ง€.. ์•„๋‹ˆ ๋ญ ์—†์–ด๋„ ๋˜๊ธดํ•˜๋Š”๋ฐ... 

    # 4๋ฒˆ ๋Œ๊ณ  ๋’ค๊ฐ€ ์œก์ง€๋ฉด ๋’ค๋กœ ๊ฐ€๊ธฐ
    if turn_time == 4:
        nx = x - dx[direction]
        ny = y - dy[direction]

        # ๋งต ๋ฒ”์œ„ ์•ˆ์—์„œ๋งŒ ๋™์ž‘ํ•˜๋„๋ก ์กฐ๊ฑด ์ถ”๊ฐ€ + backํ–ˆ๋Š”๋ฐ ์œก์ง€ 
        if (0 <= nx < n) and (0 <= ny < m) and (array[nx][ny] == 0):
            x = nx
            y = ny
        # 4๋ฒˆ ๋Œ๊ณ  ๋’ค ๋ฐ”๋‹ค๋ฉด ์ข…๋ฃŒ
        else:
            break
        turn_time = 0

print(count)


ํ•˜๋‹ค ๋ณด๋‹ˆ ์ฝ”๋“œ๊ฐ€ ๋”๋Ÿฌ์šด๋ฐ .. ํ•˜๋‚˜ํ•˜๋‚˜ ์ดํ•ดํ•˜๋ฉฐ ์ฃผ์„์„ ์ผ๋ถ€๋Ÿฌ ๋‹ฌ์•˜์œผ๋‹ˆ ์–‘ํ•ด ๋ถ€ํƒ๋“œ๋ฆผ.. 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ ์œผ๋กœ๋Š” ๋‹ค ๋งž์•˜๊ณ  ๋ฐฉํ–ฅ๋„ ์ž˜ ์ •์˜ํ•˜๊ณ  ๊ฐ€์ด๋“œ ๋ผ์ธ์€ ์ž˜ ์žก์•˜์œผ๋‚˜ ...

์˜ค๋žœ๋งŒ์— ์ฝ”ํ…Œ๋ฅผ ํ•˜๋‹ค๋ณด๋‹ˆ ๊นŒ๋จน์€ ๊ฒƒ์€ ํฌ๊ฒŒ 

  1. ์ €์žฅ ๊ณต๊ฐ„ initializing 
  2. ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ = 1 (์ด๊ฑด ๋ฌธ์ œ๋ฅผ ๋œ ์ฝ์—ˆ๋‹ค..)
  3. ์ „์—ญ์œผ๋กœ ์œ„์น˜ ๋Œ์–ด์˜ค๊ธฐ (์•„ ์ด๊ฑฐ ์ง„์งœ ๊นŒ๋จน์„ ๊ฒƒ๊ฐ™๋‹ค)

ํฌ์ธํŠธ๋ฅผ ๋‹ค์‹œ ์งš๊ณ  ๊ธฐ์–ตํ•˜์ž ...

 

Point 1
์œ„์น˜ ์ €์žฅ ๊ณต๊ฐ„ ≠ ๋งต. ์œ„์น˜๋„ ํ™•์ธํ•˜๊ณ  ๋งต๋„ ์žˆ์œผ๋ฉด ์œ„์น˜๋ฅผ ๋”ฐ๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ฃผ์ž !
Point 2
์ž…๋ ฅ์„ ๋ฐ›๊ณ  ํ˜„์žฌ ์œ„์น˜๋ฅผ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ d[x][y] = 1 ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์žŠ์ง€๋ง์ž (๋ฌธ์ œ๋ฅผ ์ž˜์ฝ์ž)
Point 3
๊นŒ๋จน๊ณ  ์ด์ค‘์œผ๋กœ ๋ฐ›์•˜๋Š”๋ฐ ํšจ์œจ์ ์œผ๋กœ ์ž…๋ ฅ ๋ฐ›์ž. array.append(list(map ใ€ฐใ€ฐ ))
Point 4 
ํ—ท๊ฐˆ๋ฆด ๊ฑฐ๋ฉด ํ•˜๋‚˜ํ•˜๋‚˜ ์ƒ๊ฐํ•ด๋ณด๋ฉด์„œ ๋™์„œ๋‚จ๋ถ define ์ง์ ‘ ํ•˜์ž 
Point 5 
'ํšŒ์ „'๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ task๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ๋นผ์ฃผ์ž. ๊ทธ๋ฆฌ๊ณ  ์ „์—ญ์€ ๊ผญ ์ฒดํฌ ํ•  ๊ฒƒ !

 

์ถ”๊ฐ€๋กœ... ์ฝ”๋“œ๊ฐ€ ์™œ ์ฑ…์ด๋ž‘ ๋‹ค๋ฅธ ๋ถ€๋ถ„์ด ์žˆ๋Š”๊ฐ€ ? 
๊ธฐ์กด ์ฑ… ์ฝ”๋“œ๋กœ ํ•˜๋ฉด Index error๊ฐ€ ๋œจ๋Š” ๊ฒŒ ์ •์ƒ !
→ ๋ณดํ†ต ๋งต์˜ ์™ธ๊ณฝ์€ ๋ฐ”๋‹ค๋ผ๋Š” ๋ฌธ์ œ์  ํ—ˆ์šฉ์ด ์žˆ์–ด ์›๋ž˜ ์ฝ”๋“œ์—๋Š” ๋งต ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์•˜์œผ๋‚˜, ์ฝ”๋“œ ์ž์ฒด๊ฐ€ ์•ˆ๋Œ์•„๊ฐ€๊ณ  ๋ฌดํ•œ ์ž…๋ ฅ ์‚ฌํƒœ... ๊ฒฐ๊ตญ ๋งต ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์คฌ๋‹ค. ์‹ค๋ฌด์™€ ์ฝ”ํ…Œ๋Š” ๋‹ค๋ฅด๋‹ˆ ์–ด๋Š์ •๋„ ํ—ˆ์šฉ๋˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Œ์„ ์ธ์ง€ ! ํ•˜๋ผ๊ณ  ์ฑ…์— ์จ์žˆ๋‹ค..

 

 

GG

๋ฐ˜์‘ํ˜•

์ตœ๊ทผ๋Œ“๊ธ€

์ตœ๊ทผ๊ธ€

skin by ยฉ 2024 ttutta