1
- rows = int (input ())
1
+ board_size = int (input ()) # rows and columns
2
2
3
- pos_knights , matrix , total_knights = [], [], [ 0 ]
3
+ removed_knights = 0
4
4
5
- for row in range (rows ):
5
+ matrix = [ list ( input ()) for row in range (board_size )]
6
6
7
- matrix . append ( list ( input ()))
7
+ knight = "K"
8
8
9
- for col in range (len (matrix [0 ])):
10
9
11
- if matrix [row ][col ] == "K" :
12
- pos_knights .append ([row , col ])
13
-
14
- cols = len (matrix [0 ])
15
-
16
-
17
- def check_valid_index (row , col ):
18
- if 0 <= row < rows and 0 <= col < cols :
10
+ def check_index (row , col ):
11
+ if 0 <= row < board_size and 0 <= col < board_size :
19
12
return True
13
+ return False
20
14
21
15
22
- movement = {
23
- "up left" : [- 2 , - 1 ], "down left" : [2 , - 1 ],
24
- "up right" : [- 2 , 1 ], "down right" : [2 , 1 ],
25
- "left up" : [- 1 , - 2 ], "left down" : [1 , - 2 ],
26
- "right up" : [- 1 , 2 ], "right down" : [1 , 2 ]
27
- }
28
-
29
-
30
- def check_knights ():
31
- knights = {}
32
-
33
- for row , col in pos_knights :
34
-
35
- for m_row , m_col in movement .values ():
16
+ moves = {
17
+ "top_left" : (- 2 , - 1 ),
18
+ "middle_left" : (- 1 , - 2 ),
19
+ "top_right" : (- 2 , 1 ),
20
+ "middle_right" : (- 1 , 2 ),
36
21
37
- knight_row , knight_col = row + m_row , col + m_col
38
-
39
- if check_valid_index (knight_row , knight_col ) and matrix [knight_row ][knight_col ] == "K" :
40
- knights [f"{ row } { col } " ] = knights .get (f"{ row } { col } " , 0 ) + 1
22
+ "bottom_left" : (2 , - 1 ),
23
+ "middle_bottom_left" : (1 , - 2 ),
24
+ "bottom_right" : (2 , 1 ),
25
+ "middle_bottom_right" : (1 , 2 )
26
+ }
41
27
42
- if not knights :
43
- return
28
+ knights = {}
44
29
45
- total_knights [0 ] += 1
46
- row , col = [int (num ) for num in max (knights , key = knights .get ).split ()]
47
- matrix [row ][col ] = "0"
48
- pos_knights .remove ([row , col ])
49
- check_knights ()
50
30
31
+ def find_best_knight ():
32
+ global knights
33
+ knights = {}
51
34
52
- check_knights ()
53
- print (total_knights [0 ])
35
+ number_of_good_knights = 0
36
+
37
+ for row in range (board_size ):
38
+
39
+ for col in range (board_size ):
40
+
41
+ if matrix [row ][col ] == knight :
42
+
43
+ for m_row , m_col in moves .values ():
44
+ knight_row , knight_col = row + m_row , col + m_col
45
+
46
+ if check_index (knight_row , knight_col ):
47
+ if matrix [knight_row ][knight_col ] == knight :
48
+ number_of_good_knights += 1
49
+ if f"{ row } { col } " not in knights .keys ():
50
+ knights [f"{ row } { col } " ] = 1
51
+
52
+ else :
53
+ knights [f"{ row } { col } " ] += 1
54
+
55
+ if number_of_good_knights == 0 :
56
+ return False
57
+ return True
58
+
59
+
60
+ def remove_knight ():
61
+ global removed_knights
62
+
63
+ best_knight = [- 1 , - 1 ]
64
+ best_knight_attacks = 0
65
+
66
+ for m_knight , attacks in knights .items ():
67
+ if attacks > best_knight_attacks :
68
+ best_knight [0 ], best_knight [1 ] = int (m_knight .split ()[0 ]), int (m_knight .split ()[1 ])
69
+ best_knight_attacks = attacks
70
+
71
+ if best_knight [0 ] >= 0 and best_knight [1 ] >= 0 :
72
+ matrix [best_knight [0 ]][best_knight [1 ]] = '0'
73
+ removed_knights += 1
74
+
75
+
76
+ while True :
77
+
78
+ if not find_best_knight ():
79
+ break
80
+
81
+ remove_knight ()
82
+
83
+ print (removed_knights )
84
+
85
+
86
+
87
+
88
+ # rows = int(input())
89
+ #
90
+ # pos_knights, matrix, total_knights = [], [], [0]
91
+ #
92
+ # for row in range(rows):
93
+ #
94
+ # matrix.append(list(input()))
95
+ #
96
+ # for col in range(len(matrix[0])):
97
+ #
98
+ # if matrix[row][col] == "K":
99
+ # pos_knights.append([row, col])
100
+ #
101
+ # cols = len(matrix[0])
102
+ #
103
+ #
104
+ # def check_valid_index(row, col):
105
+ # if 0 <= row < rows and 0 <= col < cols:
106
+ # return True
107
+ #
108
+ #
109
+ # movement = {
110
+ # "up left": [-2, -1], "down left": [2, -1],
111
+ # "up right": [-2, 1], "down right": [2, 1],
112
+ # "left up": [-1, -2], "left down": [1, -2],
113
+ # "right up": [-1, 2], "right down": [1, 2]
114
+ # }
115
+ #
116
+ #
117
+ # def check_knights():
118
+ # knights = {}
119
+ #
120
+ # for row, col in pos_knights:
121
+ #
122
+ # for m_row, m_col in movement.values():
123
+ #
124
+ # knight_row, knight_col = row + m_row, col + m_col
125
+ #
126
+ # if check_valid_index(knight_row, knight_col) and matrix[knight_row][knight_col] == "K":
127
+ # knights[f"{row} {col}"] = knights.get(f"{row} {col}", 0) + 1
128
+ #
129
+ # if not knights:
130
+ # return
131
+ #
132
+ # total_knights[0] += 1
133
+ # row, col = [int(num) for num in max(knights, key=knights.get).split()]
134
+ # matrix[row][col] = "0"
135
+ # pos_knights.remove([row, col])
136
+ # check_knights()
137
+ #
138
+ #
139
+ # check_knights()
140
+ # print(total_knights[0])
0 commit comments