-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15683.java
More file actions
137 lines (126 loc) · 3.86 KB
/
Copy path15683.java
File metadata and controls
137 lines (126 loc) · 3.86 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
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
// https://www.acmicpc.net/problem/15683
// BOJ 15683 감시
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
static int n, m, map[][], max;
static boolean[][] visited;
static List<Coordinate> cctv;
static int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
public static void main(String[] args) throws IOException {
BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(scan.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
map = new int[n][m];
visited = new boolean[n][m];
cctv = new ArrayList<>();
int count = 0;
for(int i=0; i<n; i++){
st = new StringTokenizer(scan.readLine());
for(int j=0; j<m; j++){
map[i][j] = Integer.parseInt(st.nextToken());
if(map[i][j] == 0) count++;
if(0 < map[i][j] && map[i][j] < 6)
cctv.add(new Coordinate(i,j));
}
}
max = 0;
dfs(0);
System.out.println(count - max);
}
public static void dfs(int index){
if(index == cctv.size()){
//printVisited();
max = Math.max(max, countTrue());
return;
}
Coordinate cur = cctv.get(index);
boolean[][] reset = copy(visited);
switch(map[cur.x][cur.y]){
case 1:
for(int i=0; i<4; i++){
setVisited(cur, i);
dfs(index+1);
visited = copy(reset);
} break;
case 2:
for(int i=0; i<2; i++){
setVisited(cur, i);
setVisited(cur, i+2);
dfs(index+1);
visited = copy(reset);
} break;
case 3:
for(int i=0; i<4; i++){
setVisited(cur, i);
setVisited(cur, getDir(i+1));
dfs(index+1);
visited = copy(reset);
} break;
case 4:
for(int i=0; i<4; i++){
setVisited(cur, i);
setVisited(cur, getDir(i+1));
setVisited(cur, getDir(i+2));
dfs(index+1);
visited = copy(reset);
} break;
case 5:
setVisited(cur, 0);
setVisited(cur, 1);
setVisited(cur, 2);
setVisited(cur, 3);
dfs(index+1);
visited = copy(reset);
}
}
public static void setVisited(Coordinate start, int dir){
int x = start.x, y = start.y;
while(true){
x += dx[dir]; y += dy[dir];
if(x < 0 || n <= x || y < 0 || m <= y)
return;
if(map[x][y] == 6)
return;
if(map[x][y] == 0)
visited[x][y] = true;
}
}
public static int getDir(int index){
if(index < 0) index += 4;
else if(index >= 4) index -= 4;
return index;
}
public static boolean[][] copy(boolean[][] matrix){
boolean[][] copy = new boolean[n][m];
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
copy[i][j] = matrix[i][j];
return copy;
}
public static int countTrue(){
int count = 0;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(visited[i][j])
count++;
return count;
}
public static void printVisited(){
for(int i=0; i<n; i++){
for(int j=0; j<m; j++)
if(visited[i][j]) System.out.print("1 ");
else System.out.print("0 ");
System.out.println();
}System.out.println();
}
}
class Coordinate{
int x, y;
public Coordinate(int x, int y){
this.x = x;
this.y = y;
}
}