-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathykm.java
More file actions
98 lines (81 loc) · 2.91 KB
/
ykm.java
File metadata and controls
98 lines (81 loc) · 2.91 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
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main{
public static class node{
int x, y;
int move;
int wall;
public node(int x, int y, int move, int wall){
this.x = x;
this.y = y;
this.move = move;
this.wall = wall;
}
@Override
public String toString() {
return "node [move=" + move + ", wall=" + wall + ", x=" + x + ", y=" + y + "]";
}
}
static int M;
static int N;
static int K; // 부슬수있는 벽의 갯수
static int answer = Integer.MAX_VALUE;
static char[][] map;
static boolean[][][] isVisited;
static int[] mx = {-1,1,0,0};
static int[] my = {0,0,1,-1};
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
M = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
map = new char[M][N];
isVisited = new boolean[M][N][K+1]; // 위치, 부슨벽의 수
for(int i = 0 ; i<M; i++){
map[i] = br.readLine().toCharArray();
}
isVisited[0][0][0] = true;
Queue<node> q = new ArrayDeque<node>();
q.offer(new node(0,0,1,K));
while(!q.isEmpty()){
node current = q.poll();
if(current.move >= answer){
continue;
}
if(current.x == M-1 && current.y == N-1){
answer = Math.min(answer, current.move);
}
for(int i = 0 ; i<4; i++){
int nextX = current.x + mx[i];
int nextY = current.y + my[i];
if(nextX<0 || nextY<0|| nextX>=M || nextY>=N ){ // 범위를 벗어남
continue;
}
if(isVisited[nextX][nextY][current.wall]){
continue;
}
if(map[nextX][nextY]=='1'){ // 벽
if(current.wall<K && !isVisited[nextX][nextY][current.wall+1]){ // 벽을 부슬수 있는 경우
isVisited[nextX][nextY][current.wall+1] = true;
q.offer(new node(nextX, nextY, current.move+1, current.wall+1));
}
}else{
isVisited[nextX][nextY][current.wall] = true;
q.offer(new node(nextX, nextY, current.move+1, current.wall));
}
}
}
if(answer == Integer.MAX_VALUE){
System.out.println(-1);
}
else{
System.out.println(answer);
}
}
}