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

The only one you can truly trust is yourself.

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

[PCCP ๋ชจ์˜๊ณ ์‚ฌ#2] ์‹ค์Šต์šฉ ๋กœ๋ด‡ (๋‹จ์ˆœ ๊ตฌํ˜„)

๐ŸŽฎinspirer9 2023. 3. 9. 09:00
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ์—์„œ๋Š” ์‹ค์Šต์šฉ ๋กœ๋ด‡์„ ์ด์šฉํ•ด์„œ ๋กœ๋ด‡ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์‹ค์Šต์šฉ ๋กœ๋ด‡์€ ์ž…๋ ฅ๋œ ๋ช…๋ น์— ๋”ฐ๋ผ x์ขŒํ‘œ์™€ y์ขŒํ‘œ๋กœ ํ‘œํ˜„๋˜๋Š” 2์ฐจ์› ์ขŒํ‘œ ํ‰๋ฉด ์œ„๋ฅผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๋ช…๋ น์€ ํ•˜๋‚˜์˜ ๋ฌธ์ž๋กœ ์ฃผ์–ด์ง€๋ฉฐ ๊ฐ ๋ช…๋ น์–ด์— ๋”ฐ๋ผ ๋กœ๋ด‡์ด ์ˆ˜ํ–‰ํ•˜๋Š” ์ผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋„ค ์ข…๋ฅ˜์ž…๋‹ˆ๋‹ค.

  • 'R': ๋กœ๋ด‡์ด ์˜ค๋ฅธ์ชฝ์œผ๋กœ 90๋„ ํšŒ์ „ํ•ฉ๋‹ˆ๋‹ค.
  • 'L': ๋กœ๋ด‡์ด ์™ผ์ชฝ์œผ๋กœ 90๋„ ํšŒ์ „ํ•ฉ๋‹ˆ๋‹ค.
  • 'G': ๋กœ๋ด‡์ด ํ•œ ์นธ ์ „์ง„ํ•ฉ๋‹ˆ๋‹ค.
  • 'B': ๋กœ๋ด‡์ด ํ•œ ์นธ ํ›„์ง„ํ•ฉ๋‹ˆ๋‹ค.

๋ช…๋ น์–ด๋Š” ๊ฐ๊ฐ์˜ ๋ช…๋ น๋“ค์ด
๋ชจ์ธ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ์ฃผ์–ด์ง€๋ฉฐ, ์ฐจ๋ก€๋Œ€๋กœ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค.

๋กœ๋ด‡์€ ์ฒ˜์Œ์— (0, 0) ์œ„์น˜์— +y ์ถ•์„ ํ–ฅํ•˜์—ฌ ๋†“์—ฌ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ๊ทธ๋ฆผ์€ ๋ฒˆํ˜ธ ์ˆœ์„œ๋Œ€๋กœ ๋ช…๋ น์–ด "GRGLGRG"์˜ ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

 

 

 

 

 

 

 

๋กœ๋ด‡์— ์ž…๋ ฅ๋œ ๋ช…๋ น์–ด๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฌธ์ž์—ด command๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋กœ๋ด‡์ด ์ฃผ์–ด์ง„ ๋ช…๋ น์–ด๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ๋ชจ๋‘ ์ˆ˜ํ–‰ํ•œ ๋’ค ๋„์ฐฉํ•œ ์ตœ์ข… ์œ„์น˜์˜ ์ขŒํ‘ฏ๊ฐ’ x, y๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„์„œ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ commad์˜ ๊ธธ์ด ≤ 1,000,000
  • command๋Š” 'R', 'L', 'G', 'B'์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
  • command์— ๋“ค์–ด์žˆ๋Š” ๋ฌธ์ž ํ•˜๋‚˜ํ•˜๋‚˜๊ฐ€ ๊ฐ ๋ช…๋ น์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ๋ฌธ์ž์—ด์— ๋จผ์ € ๋“ฑ์žฅํ•˜๋Š” ๋ช…๋ น์„ ๋จผ์ € ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

command result
"GRGLGRG" [2, 2]
"GRGRGRB" [2, 0]

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

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ๋กœ๋ด‡์ด ์ด๋™ํ•œ ์ขŒํ‘œ๋Š” (0, 0) → (0, 1) → (1, 1) → (1, 0) → (2, 0) ์ž…๋‹ˆ๋‹ค.

ํ’€์ด

  • ์ด๊ฑฐ ๊ฒŒ์ž„ ๋งŒ๋“ค ๋•Œ ๋งŽ์ด ์“ฐ๋˜ ๋ฐฉ์‹์ด๋ผ ์‰ฌ์› ๋‹ค.
    • ์ŠˆํŒ…๊ฒŒ์ž„์—์„œ 4๋ฐฉํ–ฅ, 8๋ฐฉํ–ฅ, 16๋ฐฉํ–ฅ, 32๋ฐฉํ–ฅ ๋“ฑ๋“ฑํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹
  • R/L์ž…๋ ฅํ•˜๋ฉด ์ด๋™ ๋‹จ์œ„ ๋ฐฐ์—ด ๋ณ€๊ฒฝ
    • ์ดˆ๊ธฐ์ƒํƒœ : [1,0]
    • RRR : [0,1],[-1,0],[0,-1]
    • R์€ ๋ฐฐ์—ด+1
    • L์€ ๋ฐฐ์—ด-1
    • ๋ฐฐ์—ด ๋„˜์–ด๊ฐ€๋ฉด ๋ฐ˜๋Œ€์ชฝ์œผ๋กœ ๊ฐ€๊ธฐ
  • G/B์ž…๋ ฅํ•˜๋ฉด ์ด๋™ ๋‹จ์œ„ ๋งŒํผ ํ˜„์žฌ ์ขŒํ‘œ์— ๋”ํ•ด์ฃผ๊ธฐ
    • ๋? ๋ฌด์ง€์„ฑ ์ฝ”๋”ฉ ๊ฐ€์ฆˆ์•„!
      • ์–ด? ์™œ ์‹คํŒจ?
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	"GRGLGRG"
๊ธฐ๋Œ“๊ฐ’ ใ€‰	[2, 2]
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	
G
[1, 0] 0
R
[1, 0] 1
G
[1, 1] 1
L
[1, 1] 0
G
[2, 1] 0
R
[2, 1] 1
G
[2, 2] 1

ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	"GRGRGRB"
๊ธฐ๋Œ“๊ฐ’ ใ€‰	[2, 0]
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	์‹คํ–‰ํ•œ ๊ฒฐ๊ด๊ฐ’ [0,2]์ด ๊ธฐ๋Œ“๊ฐ’ [2,0]๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	
G
[1, 0] 0
R
[1, 0] 1
G
[1, 1] 1
R
[1, 1] 2
G
[0, 1] 2
R
[0, 1] 3
B
[0, 2] 3
  • ์•„๋†”... ์ด๋ฒˆ์—๋„ ํ–‰๋ ฌ ๋ฐ”๊ฟ”๋†จ๋„ค ใ…‹ใ…‹ใ…‹
    • ํ…Œ์ŠคํŠธ1์€ ํ†ต๊ณผํ•˜๋Š”๋ฐ, ํ…Œ์ŠคํŠธ2๋Š” ํ†ต๊ณผ๋ฅผ ๋ชป ํ•ด์„œ ๋ณด๋‹ˆ๊นŒ...
      • ์ขŒํ‘œ๋ฅผ ๋˜ ๋ฐ˜๋Œ€๋กœ ํ•ด๋†”์„œ, ์ฝ”๋“œ ๊ณ ์น˜๊ธด ์‹ซ๊ณ  ๋งˆ์ง€๋ง‰์— answer๋งŒ ๋’ค์ง‘์–ด ์คŒ
def solution(command):
    answer = [0,0]
    cmd_mapping = [[1,0],[0,1],[-1,0],[0,-1]]
    robot_dir = 0
    for c in command:
        #print(c)
        if c == "R":
            robot_dir += 1
            robot_dir %= 4
        elif c == "L":
            robot_dir -= 1
            if robot_dir < 0: robot_dir = 3
        elif c == "G":
            answer = [ai + ri for ai, ri in zip(answer,cmd_mapping[robot_dir])]
        elif c == "B":
            answer = [ai - ri for ai, ri in zip(answer,cmd_mapping[robot_dir])]
        #print(answer, robot_dir)
    answer[0],answer[1] = answer[1],answer[0]
    return answer
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (275.99ms, 11.6MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (210.95ms, 11.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (110.56ms, 10.5MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (165.47ms, 11.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (221.77ms, 11.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (436.03ms, 11.8MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (287.71ms, 11.9MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (274.41ms, 11.9MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (297.49ms, 12MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (359.95ms, 11.9MB)
  • ๊ณ ์ˆ˜์˜ ์ฝ”๋“œ
    • ๊น”๋”ํ•˜๋„ค...
def solution(command):
    path = [[0,1], [1,0], [0,-1], [-1, 0]] 
    x = y = d = 0

    for i in command:
        if i == 'R': d = (d+1)%4
        elif i == 'L': d = (d+3)%4
        elif i == 'G':
            x += path[d][0]
            y += path[d][1]
        elif i == 'B':
            x -= path[d][0]
            y -= path[d][1]

    return [x,y]
728x90
๋ฐ˜์‘ํ˜•