๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฐฉ๋ฌธ ๊ธธ์ด

๐ŸŽฎinspirer9 2023. 2. 20. 17:02
728x90
๋ฐ˜์‘ํ˜•

์นด์นด์˜ค ๋ธ”๋ผ์ธ๋“œ 2022 ์–‘๊ถ ๋ฌธ์ œ ํ’€๋‹ค๊ฐ€ ๋จธ๋ฆฌ ๋Œ ๋จ. ๋ธ”๋ฃจ์Šคํฌ๋ฆฐ ๋– ์„œ ๋ƒ‰์ˆ˜๋กœ ์„ธ์ˆ˜ํ•˜๊ณ  ๋‹ค๋ฅธ ๋ฌธ์ œ ํ’€๊ธฐ ์‹œ์ „ใ…‹

๋ฌธ์ œ ์„ค๋ช…

๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ๋ฅผ 4๊ฐ€์ง€ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์›€์ง์ด๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • U: ์œ„์ชฝ์œผ๋กœ ํ•œ ์นธ ๊ฐ€๊ธฐ
  • D: ์•„๋ž˜์ชฝ์œผ๋กœ ํ•œ ์นธ ๊ฐ€๊ธฐ
  • R: ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ ๊ฐ€๊ธฐ
  • L: ์™ผ์ชฝ์œผ๋กœ ํ•œ ์นธ ๊ฐ€๊ธฐ

์บ๋ฆญํ„ฐ๋Š” ์ขŒํ‘œํ‰๋ฉด์˜ (0, 0) ์œ„์น˜์—์„œ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ขŒํ‘œํ‰๋ฉด์˜ ๊ฒฝ๊ณ„๋Š” ์™ผ์ชฝ ์œ„(-5, 5), ์™ผ์ชฝ ์•„๋ž˜(-5, -5), ์˜ค๋ฅธ์ชฝ ์œ„(5, 5), ์˜ค๋ฅธ์ชฝ ์•„๋ž˜(5, -5)๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, "ULURRDLLU"๋กœ ๋ช…๋ นํ–ˆ๋‹ค๋ฉด

  • 1๋ฒˆ ๋ช…๋ น์–ด๋ถ€ํ„ฐ 7๋ฒˆ ๋ช…๋ น์–ด๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์›€์ง์ž…๋‹ˆ๋‹ค.

  • 8๋ฒˆ ๋ช…๋ น์–ด๋ถ€ํ„ฐ 9๋ฒˆ ๋ช…๋ น์–ด๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์›€์ง์ž…๋‹ˆ๋‹ค.

์ด๋•Œ, ์šฐ๋ฆฌ๋Š” ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ๊ฐ€ ์ง€๋‚˜๊ฐ„ ๊ธธ ์ค‘ ์บ๋ฆญํ„ฐ๊ฐ€ ์ฒ˜์Œ ๊ฑธ์–ด๋ณธ ๊ธธ์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ์˜ˆ์‹œ์—์„œ ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ๊ฐ€ ์›€์ง์ธ ๊ธธ์ด๋Š” 9์ด์ง€๋งŒ, ์บ๋ฆญํ„ฐ๊ฐ€ ์ฒ˜์Œ ๊ฑธ์–ด๋ณธ ๊ธธ์˜ ๊ธธ์ด๋Š” 7์ด ๋ฉ๋‹ˆ๋‹ค. (8, 9๋ฒˆ ๋ช…๋ น์–ด์—์„œ ์›€์ง์ธ ๊ธธ์€ 2, 3๋ฒˆ ๋ช…๋ น์–ด์—์„œ ์ด๋ฏธ ๊ฑฐ์ณ ๊ฐ„ ๊ธธ์ž…๋‹ˆ๋‹ค)

๋‹จ, ์ขŒํ‘œํ‰๋ฉด์˜ ๊ฒฝ๊ณ„๋ฅผ ๋„˜์–ด๊ฐ€๋Š” ๋ช…๋ น์–ด๋Š” ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, "LULLLLLLU"๋กœ ๋ช…๋ นํ–ˆ๋‹ค๋ฉด

  • 1๋ฒˆ ๋ช…๋ น์–ด๋ถ€ํ„ฐ 6๋ฒˆ ๋ช…๋ น์–ด๋Œ€๋กœ ์›€์ง์ธ ํ›„, 7, 8๋ฒˆ ๋ช…๋ น์–ด๋Š” ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ 9๋ฒˆ ๋ช…๋ น์–ด๋Œ€๋กœ ์›€์ง์ž…๋‹ˆ๋‹ค.

์ด๋•Œ ์บ๋ฆญํ„ฐ๊ฐ€ ์ฒ˜์Œ ๊ฑธ์–ด๋ณธ ๊ธธ์˜ ๊ธธ์ด๋Š” 7์ด ๋ฉ๋‹ˆ๋‹ค.

๋ช…๋ น์–ด๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜ dirs๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ๊ฐ€ ์ฒ˜์Œ ๊ฑธ์–ด๋ณธ ๊ธธ์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜์—ฌ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • dirs๋Š” stringํ˜•์œผ๋กœ ์ฃผ์–ด์ง€๋ฉฐ, 'U', 'D', 'R', 'L' ์ด์™ธ์— ๋ฌธ์ž๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • dirs์˜ ๊ธธ์ด๋Š” 500 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

dirs answer
"ULURRDLLU" 7
"LULLLLLLU" 7

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1
๋ฌธ์ œ์˜ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2
๋ฌธ์ œ์˜ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

ํ’€์ด

  • ์ง€๋‚˜๊ฐ„ ๊ธธ์„ ์–ด๋–ป๊ฒŒ ์ฒดํฌํ•ด์•ผ ๋˜๋‚˜ ์‹ถ์—ˆ๋Š”๋ฐ...
    • ์ง์ˆ˜์ค„์€ 10๊ฐœ, ํ™€์ˆ˜ ์ค„์€ 11๊ฐœ
      • ์ˆ˜ํ‰ ์ด๋™ : ๊ฐ€๋กœ 10, ์ƒˆ๋กœ 11
      • ์ˆ˜์ง ์ด๋™ : ๊ฐ€๋กœ 11, ์„ธ๋กœ 10

  • ๊ฐ€ ์•„๋‹ˆ๊ตฌ๋‚˜... ๊ทธ๋ƒฅ ๊ฐ€๋กœ ์ด๋™ / ์„ธ๋กœ ์ด๋™์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ..
    • ๋ฌด์ง€์„ฑ ์ฝ”๋”ฉ
def solution(dirs):
    answer = 0
    # ์ง€๋„
    horizontal_move = [[False] * 11 for _ in range(11)]
    vertical_move = [[False] * 11 for _ in range(11)]

    def draw_map():
        for i in range(11):
            for j in range(10):
                    if horizontal_move[i][j] == False:
                        print("+ ",end="")
                    else:
                        print("+-",end="")
            print("+")
            if i < 10:
                for j in range(11):
                    if vertical_move[i][j] == False:
                        print("  ",end="")
                    else:
                        print("| ",end="")
            print("")
    
    x, y = 5, 5
    for i in dirs:
        print("x,y:", x, y)
        if i == "L":
            x -= 1
            if x < 0:
                x = 0
            horizontal_move[y][x] = True
        elif i == "R":
            x += 1
            if x > 10:
                x = 10
            horizontal_move[y][x-1] = True
        if i == "U":
            y -= 1
            if y < 0:
                y = 0
            vertical_move[y][x] = True
        elif i == "D":
            y += 1
            if y > 10:
                y = 10
            vertical_move[y-1][x] = True
            
    draw_map()
    
    for h in horizontal_move:
        answer += sum(h)
    for v in vertical_move:
        answer += sum(v)
    
    return answer
  • ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ์ •๋‹ต ๋งž๊ตฌ์š”
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	"ULURRDLLU"
๊ธฐ๋Œ“๊ฐ’ ใ€‰	7
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	x,y: 5 5
x,y: 5 4
x,y: 4 4
x,y: 4 3
x,y: 5 3
x,y: 6 3
x,y: 6 4
x,y: 5 4
x,y: 4 4
+ + + + + + + + + + +
                      
+ + + + + + + + + + +
                      
+ + + + + + + + + + +
                      
+ + + + +-+-+ + + + +
        |   |         
+ + + + +-+-+ + + + +
          |           
+ + + + + + + + + + +
                      
+ + + + + + + + + + +
                      
+ + + + + + + + + + +
                      
+ + + + + + + + + + +
                      
+ + + + + + + + + + +
                      
+ + + + + + + + + + +

ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	"LULLLLLLU"
๊ธฐ๋Œ“๊ฐ’ ใ€‰	7
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	x,y: 5 5
x,y: 4 5
x,y: 4 4
x,y: 3 4
x,y: 2 4
x,y: 1 4
x,y: 0 4
x,y: 0 4
x,y: 0 4
+ + + + + + + + + + +
                      
+ + + + + + + + + + +
                      
+ + + + + + + + + + +
                      
+ + + + + + + + + + +
|                     
+-+-+-+-+ + + + + + +
        |             
+ + + + +-+ + + + + +
                      
+ + + + + + + + + + +
                      
+ + + + + + + + + + +
                      
+ + + + + + + + + + +
                      
+ + + + + + + + + + +
                      
+ + + + + + + + + + +
  • ์กฐ๊ธˆ ๋‹ค๋“ฌ์–ด์„œ ์ฒด์  ๊ณ !
    • ์‹คํŒจ๋‚˜๋„ค ใ…ก.ใ…ก?
def solution(dirs):
    answer = 0
    horizontal_move = [[False] * 11 for _ in range(11)]
    vertical_move = [[False] * 11 for _ in range(11)]
    
    x, y = 5, 5
    for i in dirs:
        if i == "L":
            x -= 1
            if x < 0:
                x = 0
            horizontal_move[y][x] = True
        elif i == "R":
            x += 1
            if x > 10:
                x = 10
            horizontal_move[y][x-1] = True
        if i == "U":
            y -= 1
            if y < 0:
                y = 0
            vertical_move[y][x] = True
        elif i == "D":
            y += 1
            if y > 10:
                y = 10
            vertical_move[y-1][x] = True
    
    for h in horizontal_move:
        answer += sum(h)
    for v in vertical_move:
        answer += sum(v)
    
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.4MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	์‹คํŒจ (0.03ms, 10.4MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.3MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	์‹คํŒจ (0.09ms, 10.4MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	์‹คํŒจ (0.16ms, 10.4MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	์‹คํŒจ (0.09ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	์‹คํŒจ (0.09ms, 10.2MB)
  • ์•„! ๋ชป ๊ฐ€๋Š” ๋ถ€๋ถ„์—์„œ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋˜๋Š”๊ฑฐ๋„ค? ์—์ดใ…ฃใ…ฃ. ๊ดœํžˆ ์ซ„์•˜๋‹ค.
def solution(dirs):
    answer = 0
    horizontal_move = [[False] * 11 for _ in range(11)]
    vertical_move = [[False] * 11 for _ in range(11)]
    
    x, y = 5, 5
    for i in dirs:
        if i == "L":
            x -= 1
            if x < 0:
                x = 0
            else:
                horizontal_move[y][x] = True
        elif i == "R":
            x += 1
            if x > 10:
                x = 10
            else:
                horizontal_move[y][x-1] = True
        if i == "U":
            y -= 1
            if y < 0:
                y = 0
            else:
                vertical_move[y][x] = True
        elif i == "D":
            y += 1
            if y > 10:
                y = 10
            else:
                vertical_move[y-1][x] = True
    
    for h in horizontal_move:
        answer += sum(h)
    for v in vertical_move:
        answer += sum(v)
    
    return answer
  • ์†๋„ ๋น ๋ฅด๊ฒŒ ๋‚˜์™€์„œ ์ข‹๋„ค
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.13ms, 10.4MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.1MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.3MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.16ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.14ms, 10.3MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด.
    • ๊ฒฝ๋กœ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด์„œ ๋‹ค ์ €์žฅํ•˜๋ฉด ๋˜๋Š”๊ฑฐ์˜€๋„ค;;;
    • set()์œผ๋กœ ์ €์žฅํ•˜๋ฉด ์ค‘๋ณต์€ ์ €์žฅ์•ˆ๋˜๋‹ˆ๊นŒ...
def solution(dirs):
    s = set()
    d = {'U': (0,1), 'D': (0, -1), 'R': (1, 0), 'L': (-1, 0)}
    x, y = 0, 0
    for i in dirs:
        nx, ny = x + d[i][0], y + d[i][1]
        if -5 <= nx <= 5 and -5 <= ny <= 5:
            s.add((x,y,nx,ny))
            s.add((nx,ny,x,y))
            x, y = nx, ny
    return len(s)//2
  • ์†๋„๋Š”? ์‚ด์ง ๋Š๋ฆฌ๊ธด ํ•˜์ง€๋งŒ... set()์˜ add ์—ฐ์‚ฐ ๋•Œ๋ฌธ์ด๋ ค๋‚˜...
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.1MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.1MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.37ms, 10.2MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.35ms, 10.2MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.44ms, 10.4MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.33ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.37ms, 10.2MB)
  • ๋น„์Šทํ•œ ๋ฐฉ์‹์ผ ์ค„ ์•Œ์•˜๋Š”๋ฐ,
    • visit ๋ฅผ 3์ค‘ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์„œ, ํ•ด๋‹น ์ ์œผ๋กœ ๋“ค์–ด์˜ค๊ธฐ ์ „ ์ขŒํ‘œ๊นŒ์ง€ ์ €์žฅ.
    • ๊ทธ๊ฒƒ๋„ ๋‘๋ฒŒ์”ฉ...
def solution(dirs):
    y, x = 0, 0
    answer = 0
    visit = [[[] for _ in range(11)] for _ in range(11)]
    for order in dirs:
        # ์›€์ง์—ฌ์•ผํ•  ์ขŒํ‘œ ๊ตฌํ•˜๊ธฐ
        if order == "U":
            a, b = y + 1, x
        elif order == "L":
            a, b = y, x - 1
        elif order == "R":
            a, b = y, x + 1
        elif order == "D":
            a, b = y - 1, x

        # ์•ž์œผ๋กœ ๊ฐ€์•ผํ•  ์ขŒํ‘œ๊ฐ€ ๊ฒฝ๊ณ„๋ฉด์„ ๋ฒ—์–ด๋‚˜๋Š”์ง€ ํ™•์ธ
        if -5 <= a <= 5 and -5 <= b <= 5:
            # ์ด๋ฏธ ์ง€๋‚˜์ง€ ์•Š์•˜๋˜ ๊ธธ๋งŒ ์นด์šดํŠธ
            if (y, x) not in visit[a][b]:
                # ๋‘์ง€์ ์„ ์ž‡๋Š” ๊ธธ์„ ๊ฐ๊ฐ์˜ ์ถœ๋ฐœ์ ๊ณผ ๋„์ฐฉ์ ์œผ๋กœ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
                visit[a][b].append((y, x))
                visit[y][x].append((a, b))
                answer += 1
            y, x = a, b
    return answer
  • ์•ž์— ๊ฑฐ๋ž‘ ๋น„์Šท...
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.4MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.4MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.2MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.4MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.30ms, 10.3MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.19ms, 10.3MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.37ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.20ms, 10.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.19ms, 10.2MB)
  • ์—ญ์‹œ ๋ฐฐ์—ด์„ ๋ฏธ๋ฆฌ ๋งŒ๋“ค๊ณ  ๊ฐ’์„ ์ €์žฅํ•˜๊ณ , ๋‚˜์ค‘์— ๋”ํ•˜๋Š” ๋‹จ์ˆœํ•œ ๋ฐฉ์‹์ด ๋น ๋ฅธ ๋“ฏ...
  • ํŒŒ์ด์ฌ ์–ธ์–ด์— ๋Œ€ํ•œ ์ดํ•ด๋ ฅ์ด 1 ์ƒ์Šนํ–ˆ๋‹ค!
  • ์ข€๋งŒ ๊ธฐ๋‹ค๋ ค๋ผ, ์กฐ๋งŒ๊ฐ„ Godot ์—”์ง„์œผ๋กœ 1์ธ ๊ฐœ๋ฐœ ํ•ด์ฃผ๋งˆ... 
728x90
๋ฐ˜์‘ํ˜•