[Python] BOJ 1986번. 체스

1986번. 체스

문제 링크

풀이 코드

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# 1986번. 체스


'''
주의 : 정말 bruteforce한 방법으로 구현하여
코드가 매우 김
'''
n, m = map(int, input().split())
box = [[0 for i in range(m)] for i in range(n)]
qList = list(map(int, input().split()))
kList = list(map(int, input().split()))
pList = list(map(int, input().split()))

q = qList[0]
k = kList[0]
p = pList[0]
if q:
    for i in range(q):
        box[qList[1+i*2]-1][qList[1+i*2+1]-1] = 'q'
if k:
    for i in range(k):
        box[kList[1+i*2]-1][kList[1+i*2+1]-1] = 'k'
if p:
    for i in range(p):
        box[pList[1+i*2]-1][pList[1+i*2+1]-1] = 'p'

# 나이트
if k:
    for i in range(k):
        y = kList[1+i*2]-1
        x = kList[1+i*2+1]-1
        # 8칸 처리
        # 위쪽
        if y-1 >= 0 and x-2 >= 0:
            if box[y-1][x-2] == 0:
                box[y-1][x-2] = 'u'
        if y-1 >= 0 and x+2 < m:
            if box[y-1][x+2] == 0:
                box[y-1][x+2] = 'u'
        if y-2 >= 0 and x-1 >= 0:
            if box[y-2][x-1] == 0:
                box[y-2][x-1] = 'u'
        if y-2 >= 0 and x+1 < m:
            if box[y-2][x+1] == 0:
                box[y-2][x+1] = 'u'
        # 아래쪽
        if y+1 < n and x-2 >= 0:
            if box[y+1][x-2] == 0:
                box[y+1][x-2] = 'u'
        if y+1 < n and x+2 < m:
            if box[y+1][x+2] == 0:
                box[y+1][x+2] = 'u'
        if y+2 < n and x-1 >= 0:
            if box[y+2][x-1] == 0:
                box[y+2][x-1] = 'u'
        if y+2 < n and x+1 < m:
            if box[y+2][x+1] == 0:
                box[y+2][x+1] = 'u'

# 퀸
if q:
    passList = ['q', 'k', 'p']
    changeList = [0, 'u']
    for i in range(q):
        y = qList[1+i*2]-1
        x = qList[1+i*2+1]-1
        # 왼쪽 가로줄
        l = 1
        while True:
            if x-l >= 0:
                if box[y][x-l] in changeList:
                    box[y][x-l] = 'u'
                    l += 1
                elif box[y][x-l] in passList:
                    break
            else:
                break
        # 오른쪽 가로줄
        r = 1
        while True:
            if x+r < m:
                if box[y][x+r] in changeList:
                    box[y][x+r] = 'u'
                    r += 1
                elif box[y][x+r] in passList:
                    break
            else:
                break
        # 위 세로줄
        u = 1
        while True:
            if y-u >= 0:
                if box[y-u][x] in changeList:
                    box[y-u][x] = 'u'
                    u += 1
                elif box[y-u][x] in passList:
                    break
            else:
                break
        # 아래 세로줄
        d = 1
        while True:
            if y+d < n:
                if box[y+d][x] in changeList:
                    box[y+d][x] = 'u'
                    d += 1
                elif box[y+d][x] in passList:
                    break
            else:
                break
        # 대각선 왼쪽 위
        di = 1
        while True:
            if y-di >= 0 and x-di >= 0:
                if box[y-di][x-di] in changeList:
                    box[y-di][x-di] = 'u'
                    di += 1
                elif box[y-di][x-di] in passList:
                    break
            else:
                break
        # 대각선 오른쪽 아래
        di = 1
        while True:
            if y+di < n and x+di < m:
                if box[y+di][x+di] in changeList:
                    box[y+di][x+di] = 'u'
                    di += 1
                elif box[y+di][x+di] in passList:
                    break
            else:
                break
        # 대각선 왼쪽 아래
        di = 1
        while True:
            if y+di < n and x-di >= 0:
                if box[y+di][x-di] in changeList:
                    box[y+di][x-di] = 'u'
                    di += 1
                elif box[y+di][x-di] in passList:
                    break
            else:
                break
        # 대각선 오른쪽 위
        di = 1
        while True:
            if y-di >= 0 and x+di < m:
                if box[y-di][x+di] in changeList:
                    box[y-di][x+di] = 'u'
                    di += 1
                elif box[y-di][x+di] in passList:
                    break
            else:
                break
ans = 0
for i in range(n):
    for j in range(m):
        if box[i][j] == 0:
            ans += 1
print(ans)

비고