-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathyess.java
More file actions
129 lines (112 loc) · 2.97 KB
/
yess.java
File metadata and controls
129 lines (112 loc) · 2.97 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
```
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main{
//(1,1) -> (n,m) 시작점 끝점 포함
// 벽을 k개 까지 부술수 있다.
static int n ;
static int m ;
static int k ;
static int [][] map;
static int [][] copymap;
static ArrayList <int [] > blocks;
static boolean [] check;
static boolean [][] mapcheck;
static int [] dx = {1,-1,0,0};
static int [] dy = {0,0,1,-1};
static int answer = Integer.MAX_VALUE;
static boolean flag = false;
static Queue <int []> queue ;
public static void main(String [] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
map = new int [n + 1][m + 1];
blocks = new ArrayList <>();
for(int i = 1; i <= n ; i ++) {
String s = br.readLine();
for(int j = 1; j <= m ; j ++) {
map[i][j] = Integer.parseInt(s.substring(j-1,j));
if (map[i][j] == 1) {
blocks.add(new int [] {i,j});
}
}
}
check = new boolean [blocks.size()];
checkblocks(0,0);
if (flag == false) System.out.println(-1);
else System.out.println(answer) ;
}
static void copyMap() {
copymap = new int [n + 1][m + 1];
for(int i = 1; i <= n ; i ++) {
for(int j = 1; j <= m ; j ++) {
copymap[i][j] = map[i][j];
}
}
for(int i = 0 ; i < check.length; i ++) {
if (check[i] == true ) {
int [] temp = blocks.get(i);
copymap[temp[0]][temp[1]] = 0;
}
}
}
static void Print() {
for(int i = 1 ; i <= n ; i ++) {
for(int j = 1; j <= m ; j ++) {
System.out.print(copymap[i][j]);
}
System.out.println();
}
System.out.println();
}
// 벽 부수는거
static void checkblocks(int idx, int cnt ) {
if(cnt <= k ) {
copyMap();
mapcheck = new boolean [n + 1][m + 1];
calculateDistance();
}
for(int i = idx ; i < blocks.size(); i ++ ) {
if (check[i] == false) {
check[i] = true;
checkblocks(i, cnt + 1);
check[i] = false;
}
}
}
static void calculateDistance() {
queue = new ArrayDeque<>();
queue.add(new int [] {1,1,1});
mapcheck[1][1] = true;
while (!queue.isEmpty()) {
int now[] = queue.poll();
int nowx = now[0];
int nowy = now[1];
if (nowx == n && nowy == m) {
flag = true;
if (answer > now[2]) {
answer = now[2];
break;
}
}
for(int i = 0 ; i < dx.length; i ++) {
int nx = nowx + dx[i];
int ny = nowy + dy[i];
if (nx <= 0 || ny <= 0 || nx > n || ny > m ) continue;
if (copymap[nx][ny] == 1) continue;
if (mapcheck[nx][ny] == true ) continue;
mapcheck[nx][ny] = true;
queue.add(new int[] {nx,ny,now[2] + 1});
}
}
}
}
```