-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamsi.py
112 lines (94 loc) · 2.18 KB
/
gamsi.py
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
n, m = map(int, input().split(' '))
ori_map = []
for i in range(n):
ori_map.append(list(map(int, input().split(' '))))
cctv = []
cctv5 = []
mapp = []
for i in range(n):
for j in range(m):
if 1<=ori_map[i][j]<=4:
cctv.append((i, j))
if ori_map[i][j] == 5:
cctv5.append((i, j))
def gamsi5(x, y):
global ori_map
tempx = x
tempy = y
dx = [0, -1, 0, 1]
dy = [1, 0, -1, 0]
for d in range(4):
x = tempx
y = tempy
while(True):
x = x + dx[d]
y = y + dy[d]
if 0<=x<n and 0<=y<m:
if ori_map[x][y] == 6:
break
if ori_map[x][y] == 0:
ori_map[x][y] = '#'
else:
break
for x, y in cctv5:
gamsi5(x, y)
def gamsi(x, y, d):
global mapp
dx = [0, -1, 0, 1]
dy = [1, 0, -1, 0]
while(True):
x = x + dx[d]
y = y + dy[d]
if 0<=x<n and 0<=y<m:
if mapp[x][y] == 6:
break
if mapp[x][y] == 0:
mapp[x][y] = '#'
else:
break
def tot_gamsi(x, y, d):
if mapp[x][y] == 1:
gamsi(x, y, d)
if mapp[x][y] == 2:
gamsi(x, y, d)
gamsi(x, y, (d+2)%4)
if mapp[x][y] == 3:
gamsi(x, y, d)
gamsi(x, y, (d+1)%4)
if mapp[x][y] == 4:
gamsi(x, y, d)
gamsi(x, y, (d+1)%4)
gamsi(x, y, (d+2)%4)
num_cctv = len(cctv)
check = [0 for _ in range(num_cctv)]
temp = []
ans = 9999999
def dfs(cnt):
global mapp
global ori_map
global ans
if cnt == num_cctv:
mapp = []
for i in ori_map:
mapp.append(i[:])
for i in range(num_cctv):
x, y = cctv[i]
d = temp[i]
tot_gamsi(x, y, d)
cnt = 0
for i in range(n):
for j in range(m):
if mapp[i][j] == 0:
cnt += 1
ans = min(ans, cnt)
return
for i in range(4):
if check[cnt] == 1:
continue
check[cnt] = 1
temp.append(i)
dfs(cnt+1)
temp.pop()
check[cnt] = 0
dfs(0)
print(ans)