import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Scanner; public class Main { static int garo; static int sero; static int arr[][]; static PriorityQueuepq; static int result; static int realdepth; static int dx[] = {-1,0,1,0}; static int dy[] = {0,1,0,-1}; static int visit[][]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); garo = sc.nextInt(); sero = sc.nextInt(); pq = new PriorityQueue(); sc.nextLine(); arr= new int[sero][garo]; visit= new int[sero][garo]; for(int i=0;i<sero;i++) { String s= sc.nextLine(); for(int j=0;j<garo;j++) { arr[i][j]=s.charAt(j)-'0'; } } for(int i=0;i<sero;i++) { for(int j=0;j<garo;j++) { visit[i][j]=Integer.MAX_VALUE; } } pq.add(new pos(0,0,0)); visit[0][0]=0; BFS(); System.out.println(result); //출력 } public static void BFS() { while(!pq.isEmpty()) { pos temp = pq.poll(); int x = temp.x; int y = temp.y; int count= temp.count; if(x==sero-1&&y==garo-1) { result = count; return; } for(int i=0;i<4;i++) { int rx = x+dx[i]; int ry = y+dy[i]; if(rx<0||ry<0||rx>=sero||ry>=garo) { continue; } if(visit[rx][ry]>visit[x][y]+arr[rx][ry]) { visit[rx][ry] = visit[x][y]+arr[rx][ry]; pq.add(new pos(rx,ry,visit[rx][ry])); } } } } private static class pos implements Comparable{ int x; int y; int count; pos(int x, int y, int count){ this.x=x; this.y=y; this.count=count; } public int compareTo(pos o) { return this.count
'코딩테스트(백준)' 카테고리의 다른 글
[백준] 2638 : 치즈 -JAVA(자바) - 사좋배 공유 (0) | 2020.02.27 |
---|---|
[백준] 2636 : 치즈 -JAVA(자바) - 사좋배 공유 (0) | 2020.02.27 |
[백준] 5014 : 스타트링크 - JAVA(자바) - 사좋배 공유 (0) | 2020.02.24 |
[백준] 1600 : 말이 되고픈 원숭이 -JAVA(자바) - 사좋배 공유 (0) | 2020.02.24 |
[백준] 2589 : 보물섬 -JAVA(자바) - 사좋배 공유 (0) | 2020.02.23 |