-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.py
More file actions
executable file
·58 lines (53 loc) · 1.35 KB
/
practice.py
File metadata and controls
executable file
·58 lines (53 loc) · 1.35 KB
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
#!/usr/bin/env python3
import sys
import copy
sys.stdin = open("input.txt", "r")
N,M = map(int, input().split())
cctv =[]
board=[]
mode = [
[],
[[0],[1],[2],[3]],
[[0,2],[1,3]],
[[0,1],[1,2],[2,3],[0,3]],
[[0,1,2],[0,1,3],[1,2,3],[0,2,3]],
[[0,1,2,3]],]
dx = [-1,0,1,0]
dy = [0,1,0,-1]
def fill(board, mode, x, y):
for i in mode:
nx = x
ny = y
while True:
nx += dx[i]
ny += dy[i]
if nx < 0 or ny < 0 or nx >= N or ny >= M:
break
if board[nx][ny] == 6:
break
elif board[nx][ny] == 0:
board[nx][ny] = 7
def dfs(depth, board):
global min_value
if depth == len(cctv):
count = 0
for i in range(N):
count += board[i].count(0)
min_value = min(min_value, count)
return
tmp = copy.deepcopy(board)
cctv_num, x, y = cctv[depth]
for i in mode[cctv_num]:
fill(tmp,i,x,y)
dfs(depth+1, tmp)
tmp = copy.deepcopy(board)
if __name__ == "__main__":
for i in range(N):
data = list(map(int, input().split()))
board.append(data)
for j in range(M):
if data[j] in [1,2,3,4,5]:
cctv.append([data[j],i,j])
min_value = int(1e9)
dfs(0, board)
print(min_value)