1dx = [1, 0, -1, 0]
 2dy = [0, 1, 0, -1]
 3
 4def solution(rows, columns, queries):
 5    table = [[(j*columns)+i+1 for i in range(columns)] for j in range(rows)]
 6    answer = []
 7
 8    for y1, x1, y2, x2 in queries:
 9        min_num = 10000
10        x1 -= 1
11        y1 -= 1
12        x2 -= 1
13        y2 -= 1
14        direction = 0
15        curX, curY = x1, y1
16        postNum = table[y1][x1]
17        while True:
18            curX = curX+dx[direction]
19            curY = curY+dy[direction]
20
21            temp = table[curY][curX]
22            table[curY][curX] = postNum
23            postNum = temp
24
25            min_num = min(min_num ,postNum)
26
27            if ((curX == x2 and curY == y1) or
28                (curX == x2 and curY == y2) or
29                (curX == x1 and curY == y2)):
30                direction += 1
31            
32            if curX == x1 and curY == y1:
33                break
34
35        answer.append(min_num)
36
37    return answer

๋ฌธ์ œ

 1dx = [-1, 1, 0, 0]
 2dy = [0, 0, -1, 1]
 3def quest(table):
 4    table = [list(line) for line in table]
 5    for j in range(5):
 6        for i in range(5):
 7            if table[i][j] == 'P':
 8                flag = [False]*4
 9                target = []
10                for k in range(4):
11                    nx = i+dx[k]
12                    ny = j+dy[k]
13                    if 0 <= nx < 5 and 0 <= ny < 5:
14                        target.append((nx, ny))
15                        flag[k] = True
16
17                for x, y in target:
18                    if table[x][y] == 'C' or table[x][y] == 'P':
19                        return 0
20                    if table[x][y] == 'O':
21                        table[x][y] = 'C'
22
23                target = []
24                if flag[0] and flag[2] and table[i-1][j] == 'O' and table[i][j-1] == 'O':
25                    target.append((i-1, j-1))
26                if flag[0] and flag[3] and table[i-1][j] == 'O' and table[i][j+1] == 'O':
27                    target.append((i-1, j+1))
28                if flag[1] and flag[2] and table[i+1][j] == 'O' and table[i][j-1] == 'O':
29                    target.append((i+1, j-1))
30                if flag[1] and flag[3] and table[i+1][j] == 'O' and table[i][j+1] == 'O':
31                    target.append((i+1, j+1))
32
33                for x, y in target:
34                    if table[x][y] == 'P':
35                        return 0
36    return 1
37
38def solution(places):
39    answer = []
40    for table in places:
41        answer.append(quest(table))
42    return answer

๋ฌธ์ œ

 1dx = [1, 0, -1, 0]
 2dy = [0, 1, 0, -1]
 3def solution(grid):
 4    N = len(grid)
 5    M = len(grid[0])
 6    grid = [list(line) for line in grid]
 7    route = [[[False]*4 for _ in range(M)] for _ in range(N)]
 8    answer = []
 9    
10    for i in range(M):
11        for j in range(N):
12            for k in range(4):
13                cur = [i, j]
14                dir = k
15                cnt = 0
16                while not route[cur[1]][cur[0]][dir]:
17                    cnt += 1
18                    route[cur[1]][cur[0]][dir] = True
19                    cur[0] = (cur[0]+dx[dir]) % M
20                    cur[1] = (cur[1]+dy[dir]) % N
21
22                    if grid[cur[1]][cur[0]] == 'L':
23                        dir = (dir-1) % 4
24                    elif grid[cur[1]][cur[0]] == 'R':
25                        dir = (dir+1) % 4
26                if cnt != 0:
27                    answer.append(cnt)
28
29    return sorted(answer)

๋ฌธ์ œ

 1def solution(n, wires):
 2    tower = [[] for _ in range(n)]
 3    answer = 100
 4    for wire in wires:
 5        wire[0] -= 1
 6        wire[1] -= 1
 7        tower[wire[0]].append(wire[1])
 8        tower[wire[1]].append(wire[0])
 9    
10    def traverse(visited, start):
11        visited[start] = True
12        for i in tower[start]:
13            if not visited[i]:
14                traverse(visited, i)
15    for wire in wires:
16        tower[wire[0]].remove(wire[1])
17        tower[wire[1]].remove(wire[0])
18
19        visited = [False]*n
20        a = 0
21        traverse(visited, wire[0])
22        for i in range(n):
23            if visited[i]:
24                a += 1
25
26        visited = [False]*n
27        b = 0
28        traverse(visited, wire[1])
29        for i in range(n):
30            if visited[i]:
31                b += 1
32        answer = min(answer, abs(a-b))
33        tower[wire[0]].append(wire[1])
34        tower[wire[1]].append(wire[0])
35    return answer

๋ฌธ์ œ