728x90
반응형
https://www.acmicpc.net/problem/2178
2178번: 미로 탐색
첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.
www.acmicpc.net

내가 푼 코드
package baekjoon;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class 미로탐색_2178 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] miro = new int[n][m];
boolean[][] visited = new boolean[n][m];
// 미로 그리기
for (int i = 0; i < n; i++) {
String smiro = sc.next();
for (int j = 0; j < smiro.length(); j++) {
// char로 들어오기 때문에 숫자로 변환해주기
miro[i][j] = Integer.parseInt(String.valueOf(smiro.charAt(j)));
}
}
int[] xpoint = {-1, 1, 0, 0}; // [위, 아래,] 왼쪽, 오른쪽
int[] ypoint = {0, 0, -1, 1}; // 위, 아래, [왼쪽, 오른쪽]
Queue<int[]> q = new LinkedList<>();
int[] arr = {0, 0};
q.offer(arr); // 큐에 최상단 노드 추가
visited[0][0] = true; // 해당 노드 방문 처리
int x, y;
int now[];
while (!q.isEmpty()) { // 큐가 빌때까지 반복
now = q.poll(); // 큐에서 하나의 노드를 꺼냄
x = now[0]; // x좌표
y = now[1]; // y좌표
// 위, 아래, 왼쪽, 오른쪽을 돌기
for (int i = 0; i < xpoint.length; i++) {
int nextX = x + ypoint[i]; // 다음 x좌표 -1 1 0 0
int nextY = y + xpoint[i]; // 다음 y좌표 0 0 -1 1
// 이동할 좌표가 없는 경우(음수이거나 n,m과 같거나 큰 값) 계속
if(nextX < 0 || nextY < 0 || nextX >= n || nextY >= m) continue;
// 방문했던 곳이거나 미로에 0인 경우 계속
if(visited[nextX][nextY] || miro[nextX][nextY] == 0) continue;
q.add(new int[] {nextX, nextY}); // 이동할 좌표를 큐에 추가
miro[nextX][nextY] = miro[x][y]+1; // 기준 좌표에 +1을 하여서 다음 좌표 값에 추가하기
visited[nextX][nextY] = true; // 해당 좌표 방문처리
} // for i
} // while
sc.close();
System.out.println(miro[n-1][m-1]); // 도착지점에서의 최종칸수
} // main
} // class
공부했던 것은 아래 링크 참조!
https://yelin1217.tistory.com/362
[백준] 2606번 바이러스 / 그래프 탐색(BFS/DFS)
https://www.acmicpc.net/problem/2606 2606번: 바이러스 첫째 줄에는 컴퓨터의 수가 주어진다. 컴퓨터의 수는 100 이하이고 각 컴퓨터에는 1번 부터 차례대로 번호가 매겨진다. 둘째 줄에는 네트워크 상에서
yelin1217.tistory.com
728x90
반응형
'Algorithm' 카테고리의 다른 글
[프로그래머스] 위장 / Hash (0) | 2022.08.10 |
---|---|
[프로그래머스] 최대공약수와 최소공배수 구하기 / 유클리드 호제법 (0) | 2022.08.09 |
[백준] 2606번 바이러스 / 그래프 탐색(BFS/DFS) (0) | 2022.08.06 |
[백준] 2798번 블랙잭 / 완전탐색(Brute Force) (0) | 2022.07.30 |
[백준] 20300번 서강근육맨 / Greedy Algorithm(탐욕법, 그리디 알고리즘) (0) | 2022.07.28 |