Post

Programmers. Walk park

공원 산책

  • practice problem
  • 정답률: 37%
  • 2023.06.28
  • 10:00 ~ 11:20 (80 min)
  • 후기: 조건문을 이용한 문제로 난이도는 어렵지 않았지만 복잡도를 개선을 위해 다소 시간이 많이 걸림.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def solution(park, routes):
    answer = []
    str_park = []
    park_mod_row = len(str(park[0]))
    park_mod_col = len(park)
    
    for i in range(len(park)):
            for j in range(len(str(park[i]))):
                str_park.append(str(park[i])[j])
    for i in range(len(str_park)):
        if str_park[i] == "S":
            str_p = i
            break
            
    for i in routes:
        Compose, loc = i.split(" ")
        loc = int(loc)
        if Compose == "E":
            if str_p % park_mod_row + loc >= park_mod_row:
                continue
            park_result = str_p
            
            for j in range(1,loc+1):    
                str_p = str_p + 1
                if str_park[str_p] == "X":
                    str_p = park_result
                    break
                 
        elif Compose == "W":
            if str_p % park_mod_row - loc < 0:
                continue
            park_result = str_p
            
            for j in range(1,loc+1):    
                str_p = str_p - 1
                if str_park[str_p] == "X":
                    str_p = park_result
                    break
        
        elif Compose == "S":
            if str_p//park_mod_row + loc >= park_mod_col:
                continue
            park_result = str_p
        
    
            for j in range(1, loc+1):    
                str_p = str_p + park_mod_row
                
                if str_park[str_p] == "X":
                    str_p = park_result
                    break
             
        if Compose == "N":
            if str_p//park_mod_row - loc < 0:
                continue
            park_result = str_p
            
            for j in range(1, loc+1):    
                str_p = str_p - park_mod_row
                if str_park[str_p] == "X":
                    str_p = park_result
                    break
    answer.append(str_p//park_mod_row)
    answer.append(str_p%park_mod_row)
    return answer
This post is licensed under CC BY 4.0 by the author.