| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- 자료구조
- 너비우선탐색
- 소수
- android support
- anr
- 알고리즘
- 자바 컬렉션
- BFS
- android fragment
- 소수 알고리즘
- application not responding
- Github
- 안드로이드 ANR
- DFS
- SQLite와 Realm 차이점
- java
- 안드로이드 AdapterView
- 액티비티 ANR
- 백준 알고리즘
- android adapterview
- 안드로이드 DBMS
- support fragment
- 안드로이드 파일
- 컬렉션
- 안드로이드
- 깊이우선탐색
- support 라이브러리
- db
- oracle
- 백준
Archives
- Today
- Total
밍의 기록들😉
[문제] 미로찾기 본문
문제
소스코드
import java.util.*;
class Pair{
int x;
int y;
Pair(int x, int y){
this.x = x;
this.y = y;
}
}
public class Maze {
public static final int[] dx = {0,0,1,-1};
public static final int[] dy = {1,-1,0,0};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] input = sc.nextLine().split(" ");
int n = Integer.parseInt(input[0]);
int m = Integer.parseInt(input[1]);
int[][] map = new int[n][m];
for(int i=0; i<n; i++){
String[] line = sc.nextLine().split(" ");
for(int j=0; j<m; j++){
map[i][j] = Integer.parseInt(line[j]);
}
}
int[][] dist = new int[n][m]; //distance
boolean[][] check = new boolean[n][m]; //check
Queue<Pair> q = new LinkedList<Pair>(); //bfs
q.add(new Pair(n-1, 0));
check[n-1][0] = true;
dist[n-1][0] = 0;
while(!q.isEmpty()){
Pair p = q.remove();
int x = p.x;
int y = p.y;
for(int k=0; k<4; k++){
int nx = x+dx[k];
int ny = y+dy[k];
if(0<=nx && nx<n && 0<=ny && ny<m){
if(check[nx][ny] == false && map[nx][ny] == 0){
q.add(new Pair(nx, ny));
dist[nx][ny] = dist[x][y] + 1;
check[nx][ny] = true;
}
}
}
}
System.out.println(dist[0][m-1]);
}
}
풀이
-
'자료구조, 알고리즘 > 문제풀이' 카테고리의 다른 글
| [프로그래머스] 완주하지 못한 선수 (0) | 2019.09.18 |
|---|---|
| [문제] 벽 부수고 이동하기 (0) | 2018.09.17 |
| [문제] 깊이우선탐색과 너비우선탐색 (0) | 2018.09.09 |
| [문제] 부등호 2529번 (0) | 2018.09.05 |
| [문제] 웜바이러스 2606번 (0) | 2018.09.05 |
Comments