1
+ #
2
+ # https://www.acmicpc.net/problem/15683
3
+
4
+ """
5
+ 6 6
6
+ 0 0 0 0 0 0
7
+ 0 2 0 0 0 0
8
+ 0 0 0 0 6 0
9
+ 0 6 0 0 2 0
10
+ 0 0 0 0 0 0
11
+ 0 0 0 0 0 5
12
+ """
13
+
14
+ import sys
15
+ from itertools import product
16
+
17
+ input = sys .stdin .readline
18
+ N ,M = map (int , input ().split ())
19
+ board = [list (map (int , input ().split ())) for _ in range (N )]
20
+
21
+ # L,U,R,D
22
+ directions = [(- 1 ,0 ),(0 ,- 1 ),(1 ,0 ),(0 ,1 )]
23
+ cctv_direction = {
24
+ 1 : [0 ,1 ,2 ,3 ],
25
+ 2 : [0 ,1 ],
26
+ 3 : [0 ,1 ,2 ,3 ],
27
+ 4 : [0 ,1 ,2 ,3 ],
28
+ 5 : [0 ]
29
+ }
30
+ cctv_direction_details = {
31
+ 1 : [[0 ],[1 ],[2 ],[3 ]],
32
+ 2 : [[0 ,2 ],[1 ,3 ]],
33
+ 3 : [[0 ,1 ],[1 ,2 ],[2 ,3 ],[3 ,0 ]],
34
+ 4 : [[0 ,1 ,2 ],[1 ,2 ,3 ],[2 ,3 ,0 ],[3 ,0 ,1 ]],
35
+ 5 : [[0 ,1 ,2 ,3 ]]
36
+ }
37
+
38
+ empty = 0
39
+ cctv_list = []
40
+ for n in range (N ):
41
+ for m in range (M ):
42
+ if 1 <= board [n ][m ] <= 5 :
43
+ cctv_list .append ((board [n ][m ], n , m ))
44
+ elif board [n ][m ] == 0 :
45
+ empty += 1
46
+
47
+ def stop_point (x , y ):
48
+ if x < 0 or x >= N :
49
+ return True
50
+ if y < 0 or y >= M :
51
+ return True
52
+ return False
53
+
54
+ answer = 64
55
+ for cid , case in enumerate (product (* [cctv_direction [cctv_list [i ][0 ]] for i in range (len (cctv_list ))])):
56
+ empty_ = empty
57
+ cid += 7
58
+
59
+ # case 안에서의 각 cctv 방향
60
+ for idx , dirs in enumerate (case ):
61
+ cctv ,cx ,cy = cctv_list [idx ]
62
+
63
+ for direct in cctv_direction_details [cctv ][dirs ]:
64
+ nx ,ny = cx ,cy
65
+ while True :
66
+ dx ,dy = directions [direct ]
67
+ nx += dx
68
+ ny += dy
69
+
70
+ if stop_point (nx ,ny ) or board [nx ][ny ] == 6 :
71
+ break
72
+ if 1 <= board [nx ][ny ] <= 5 :
73
+ continue
74
+ if board [nx ][ny ] != cid :
75
+ board [nx ][ny ] = cid
76
+ empty_ -= 1
77
+
78
+ answer = min (empty_ , answer )
79
+ if answer == 0 :
80
+ break
81
+
82
+ print (answer )
0 commit comments