Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 1303번
- 13023번 ABCDE
- level1
- 우선순위 큐 자바스크립트
- 2275번
- 알고리즘 수업-깊이 우선 탐색1
- 백준 13023번
- 백준 2638번
- 자바스크립트
- 알고리즘
- 부녀회장이 될 테야
- dfs
- 2638번 치즈
- 1937번 욕심쟁이 판다
- 리덕스
- level0
- 24480번
- 백준
- JavaScript
- 프로그래머스
- 힙 자바스크립트
- 백준 1068번 트리
- Redux
- 1389번 케빈 베이컨의 6단계 법칙
- 기지국 설치 자바스크립트
- Java
- ssh
- 백준 1068
- 기지국 설치 js
- React
Archives
- Today
- Total
코딩왕랄프👊🏻
[백준] 2178번 미로탐색 본문
반응형
SMALL
https://www.acmicpc.net/problem/2178
2178번: 미로 탐색
첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.
www.acmicpc.net
import java.util.*;
public class Main {
static int n, m;
static int[] dx = {0, 1, 0, -1};
static int[] dy = {1, 0, -1, 0};
static int[][] road;
static int[][] visit;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
road = new int[n][m];
visit = new int[n][m];
for(int i = 0; i < n; i++){
String input = sc.next();
for(int j = 0; j < m; j++){
road[i][j] = input.charAt(j) - '0';
}
}
bfs();
System.out.print(road[n-1][m-1]);
}
public static void bfs() {
Queue<Integer> queue_x = new LinkedList<Integer>();
Queue<Integer> queue_y = new LinkedList<Integer>();
queue_x.offer(0);
queue_y.offer(0);
visit[0][0] = 1;
while(!queue_x.isEmpty()) {
int x = queue_x.poll();
int y = queue_y.poll();
for(int k = 0; k < 4;k++) {
int temp_x = x + dx[k];
int temp_y = y + dy[k];
if(temp_x >= 0 && temp_y >= 0 && temp_x < n && temp_y < m) {
if(road[temp_x][temp_y] == 1 && visit[temp_x][temp_y] == 0) {
queue_x.offer(temp_x);
queue_y.offer(temp_y);
visit[temp_x][temp_y] = 1;
road[temp_x][temp_y] = road[x][y] + 1;
}
}
}
}
}
}
반응형
LIST