Posts [백준] #17837 새로운 게임2 Python (파이썬)
Post
Cancel

[백준] #17837 새로운 게임2 Python (파이썬)

새로운 게임2

문제링크

https://www.acmicpc.net/problem/17837

코드

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import sys
sys.stdin = open("input.txt",'r')
n, k = map(int,input().split())
chess = [[int(x) for x in input().split()] for _ in range(n)]
horses = [[int(x) for x in input().split()] for _ in range(k)]
board = [[[] for _ in range(n)] for _ in range(n)]
# 보드 만들기
for i in range(k):
    x,y,d = horses[i]
    board[x-1][y-1] = [i]

dir = [[0,1],[0,-1],[-1,0],[1,0]]

def blue_and_cant(d,num,x,y,board,horses):
    if d % 2 == 0:
        new_d = d-1
    else:
        new_d = d+1

    num_index = board[x-1][y-1].index(num)
    dx, dy = dir[new_d-1]
    nx, ny = x+dx,y+dy

    if 0 < nx < n+1 and 0 < ny < n+1:
        if chess[nx-1][ny-1] == 0: #흰색일때
            board[nx-1][ny-1] += board[x-1][y-1][num_index:]
            board[x-1][y-1] = board[x-1][y-1][:num_index] # 전의 자리 초기화

        elif chess[nx-1][ny-1] == 1: #빨간색일때
            board[nx-1][ny-1] += list(reversed(board[x-1][y-1][num_index:]))
            board[x-1][y-1] = board[x-1][y-1][:num_index] # 전의 자리 초기화
        elif chess[nx-1][ny-1] == 2:
            nx, ny = x,y
    else:
        nx, ny = x, y

    return nx,ny,new_d,board

def make_horse(nx,ny,d,num,horses):
    for i in range(len(board[nx-1][ny-1])): # 말들 자리 새로 갱신
        horses[board[nx-1][ny-1][i]][0], horses[board[nx-1][ny-1][i]][1] = nx, ny
    horses[num][2] = d # 방향 갱신
    return horses

cnt = 1
flag = 0
ans = -1

while True:
    if flag == 1:
        break
    if cnt >= 1000:
        break
    for num in range(k):
        # print(num)
        x,y,d = horses[num]
        dx, dy = dir[d-1]
        nx, ny = x+dx,y+dy
        if 0 < nx < n+1 and 0 < ny < n+1:
            num_index = board[x-1][y-1].index(num)

            if chess[nx-1][ny-1] == 0: #흰색일때
                board[nx-1][ny-1] += board[x-1][y-1][num_index:] #보드 갱신
                horses = make_horse(nx,ny,d,num,horses)
                board[x-1][y-1] = board[x-1][y-1][:num_index] # 전의 자리 초기화
                # print('1')


            elif chess[nx-1][ny-1] == 1: #빨간색일때
                board[nx-1][ny-1] += list(reversed(board[x-1][y-1][num_index:])) # 보드 갱신
                horses = make_horse(nx,ny,d,num,horses)
                board[x-1][y-1] = board[x-1][y-1][:num_index] # 전의 자리 초기화
                # print('2')

            elif chess[nx-1][ny-1] == 2: # 파란색일떄
                nx,ny,d,board = blue_and_cant(d,num,x,y,board,horses) # 보드 갱신
                horses = make_horse(nx,ny,d,num,horses)
                # print('3')

        else: # 격자 안에 안들어올때
            nx,ny,d, board = blue_and_cant(d,num,x,y,board,horses)
            horses = make_horse(nx,ny,d,num,horses)
        #     print('4')

        # print(board)
        # print(horses)
        
        for i in range(n):
            for j in range(n):
                if len(board[i][j]) >= 4:
                    flag = 1
                    ans = cnt
                    break
            if flag == 1:
                break
    cnt += 1

print(ans)
This post is licensed under CC BY 4.0 by the author.

[프로그래머스] N으로 표현 Python (파이썬)

[백준] #17144 미세먼지 안녕! Python (파이썬)

Loading comments from Disqus ...