https://www.acmicpc.net/problem/14940
14940번: 쉬운 최단거리
지도의 크기 n과 m이 주어진다. n은 세로의 크기, m은 가로의 크기다.(2 ≤ n ≤ 1000, 2 ≤ m ≤ 1000) 다음 n개의 줄에 m개의 숫자가 주어진다. 0은 갈 수 없는 땅이고 1은 갈 수 있는 땅, 2는 목표지점이
www.acmicpc.net
저는 BFS를 통해 문제를 해결하였습니다.
저는 처음 입력 받을 때, 입력이 0이면 0을 넣고, 나머지는 -1을 넣었습니다.
(도달 못 할 시 -1로 되도록 하기위해 이렇게 하였습니다.)
시작지점부터 BFS를 하여 각 지점의 최단 거리를 구하였습니다.
package BOJ.bfs;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_14940 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
Function<String,Integer> stoi = Integer::parseInt;
int n = stoi.apply(st.nextToken());
int m = stoi.apply(st.nextToken());
int[][] input = new int[n][m];
int[][] distance = new int[n][m];
for(int i = 0 ; i < n ; i++){
Arrays.fill(distance[i],NOT_FOUND);
}
int startY = 0;
int startX = 0;
for(int i = 0 ; i < n ; i++){
st = new StringTokenizer(br.readLine());
for(int j = 0 ; j < m ; j++){
input[i][j] = stoi.apply(st.nextToken());
if(input[i][j] == START_POINT){
startY = i;
startX = j;
}
if(input[i][j] == WALL){
distance[i][j] = WALL;
}
}
}
bfs(startY,startX,input,n,m,distance);
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < m ; j++){
System.out.print(distance[i][j]+" ");
}
System.out.println();
}
}
private static final int[] DY = {-1,0,1,0};
private static final int[] DX = {0,1,0,-1};
private static final int START_POINT = 2;
private static final int WALL = 0;
private static final int NOT_FOUND = -1;
private static int[][] bfs(int startY,int startX, int[][] input, int n, int m, int[][] distance) {
Queue<int[]> q = new LinkedList<>();
q.offer(new int[] {startY,startX});
boolean[][] visited = new boolean[n][m];
visited[startY][startX] = true;
int time = 0;
while(!q.isEmpty()){
int size = q.size();
for(int s = 0 ; s < size ; s++){
int[] now = q.poll();
// System.out.println(Arrays.toString(now));
distance[now[0]][now[1]] = time;
for(int i = 0 ; i < 4 ; i++){
int ny = now[0] + DY[i];
int nx = now[1] + DX[i];
if(checkBound(ny,nx,n,m) && !visited[ny][nx] && input[ny][nx] != WALL){
visited[ny][nx] = true;
q.offer(new int[]{ny,nx});
}
}
}
time++;
}
return distance;
}
private static boolean checkBound(int ny, int nx, int n, int m) {
if(ny >= 0 && ny < n && nx >= 0 && nx < m){
return true;
}
return false;
}
}
'알고리즘' 카테고리의 다른 글
백준 5430번 AC (JAVA) (0) | 2022.08.02 |
---|---|
백준 13334번 철로 (JAVA) (0) | 2022.08.01 |
백준 21736번 헌내기는 친구가 필요해 (JAVA) (0) | 2022.07.30 |
백준 20010번 악덕 영주 혜유 (JAVA) (0) | 2022.07.29 |
백준 9658번 돌 게임4 (JAVA) (0) | 2022.07.28 |