-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaekjoon_15683.java
More file actions
135 lines (102 loc) · 3.56 KB
/
baekjoon_15683.java
File metadata and controls
135 lines (102 loc) · 3.56 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
public class baekjoon_15683 {
//상 우 하 좌
static int[] dr = {-1, 0, 1, 0}, dc = {0, 1, 0, -1};
static int N, M, camCnt, result = Integer.MAX_VALUE;
static final int LOOK = 7;
static List<int[]> camList = new ArrayList<>();
int[] c1 = {0, 1, 2, 3};
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
int[][] arr = new int[N][M];
for(int i=0;i<N;i++) {
st = new StringTokenizer(br.readLine());
for(int j=0;j<M;j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
if(arr[i][j] <= 5 && arr[i][j] >= 1) {
camList.add(new int[]{i, j});
}
}
}
dfs(arr, 0);
System.out.println(result);
}
public static void dfs(int[][] arr, int camIdx) {
if(camIdx == camList.size()) {
int notLookCnt = countNotLook(arr);
result = Math.min(notLookCnt, result);
return;
}
int[] curCam = camList.get(camIdx);
int cr = curCam[0];
int cc = curCam[1];
int cn = arr[cr][cc];
for(int i=0;i<4;i++) {
int[][] curArr = copy(arr);
if(cn == 1) cam1(cr, cc, i, curArr);
else if(cn == 2) cam2(cr, cc, i, curArr);
else if(cn == 3) cam3(cr, cc, i, curArr);
else if(cn == 4) cam4(cr, cc, i, curArr);
else if(cn == 5) cam5(cr, cc, i, curArr);
dfs(curArr, camIdx+1);
}
}
public static int countNotLook(int[][] arr) {
int sum = 0;
for(int i=0;i<N;i++) {
for (int j=0;j<M;j++) {
if(arr[i][j] == 0) sum++;
}
}
return sum;
}
public static int[][] copy (int[][] arr) {
int[][] copy = new int[N][M];
for(int i=0;i<N;i++) {
copy[i] = Arrays.copyOf(arr[i], M);
}
return copy;
}
public static void cam5(int r, int c, int toward, int[][] arr) {
for(int i=0;i<4;i++) {
goFoward(r, c, i, arr);
}
}
public static void cam4(int r, int c, int toward, int[][] arr){
goFoward(r, c, toward, arr);
goFoward(r, c, (toward - 1 + 4) % 4, arr);
goFoward(r, c, (toward - 2 + 4) % 4, arr);
}
public static void cam3(int r, int c, int toward, int[][] arr){
goFoward(r, c, toward, arr);
goFoward(r, c, (toward - 1 + 4) % 4, arr);
}
public static void cam2(int r, int c, int toward, int[][] arr){
goFoward(r, c, toward, arr);
goFoward(r, c, (toward + 2) % 4, arr);
}
public static void cam1(int r, int c, int toward, int[][] arr) {
goFoward(r, c, toward, arr);
}
public static void goFoward(int r, int c, int toward, int[][] arr) {
while(true) {
int nr = r + dr[toward];
int nc = c + dc[toward];
if(nr < 0 || nc < 0 || nr >= N || nc >= M) {
break;
}
if(arr[nr][nc] == 6) { break;}
if(arr[nr][nc] == 0) { arr[nr][nc] = LOOK; }
r = nr;
c = nc;
}
}
}