- 풀이
일반적인 배열의 너비 우선 탐색에서 dist배열을 추가하여 더 적게 벽을 부순경우의 수만 queue에 추가하여 탐색해주었습니다.
Queue를 int[]인 배열로 만들어서 변수명[0]으로 꺼내오는 것 보단 Node라는 클래스를 만들어 좌표의 뜻을 품는 x, y의 좌표로 표현하였습니다. (변수의 의미 부여)
- 코드
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class Main {
static int N, M;
static int[][] array;
static int[][] dist;
static int[] x = {0,0,-1,1};
static int[] y = {-1,1,0,0};
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
SetData();
System.out.println(dist[N-1][M-1]);
}
private static void SetData() throws Exception {
InputReader in = new InputReader(System.in);
M = in.nextInt();
N = in.nextInt();
array = new int[N][M];
dist = new int[N][M];
for(int i = 0; i < N; i++) {
String s = in.nextLine();
for(int j = 0; j < M; j++) {
array[i][j] = s.charAt(j) - '0';
dist[i][j] = 10000000;
}
}
bfs();
}
private static void bfs() {
Queue<Node> queue = new LinkedList<>();
queue.add(new Node(0,0));
dist[0][0] = 0;
while(!queue.isEmpty()) {
Node node = queue.poll();
for(int i = 0; i < 4; i++) {
int r = node.x + x[i];
int c = node.y + y[i];
if(r < 0 || c < 0 || r >= N || c >= M) continue;
if (dist[r][c] > dist[node.x][node.y] + array[r][c]) {
dist[r][c] = dist[node.x][node.y] + array[r][c];
queue.add(new Node(r,c));
}
}
}
}
}
class Node {
int x;
int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
'algorithm' 카테고리의 다른 글
[BOJ/JAVA] 백준 9019번 : DSLR (0) | 2021.10.22 |
---|---|
[BOJ/JAVA] 백준 13023번 : ABCDE (0) | 2021.10.21 |
[BOJ/JAVA] 백준 2458번 : 키 순서 (0) | 2021.10.19 |
[BOJ/JAVA] 백준 11403번 : 경로 찾기 (0) | 2021.10.19 |
[BOJ/JAVA] 백준 1707번 : 이분 그래프 (0) | 2021.10.14 |