From 93d564d60df4e845c4630b323212497158091efe Mon Sep 17 00:00:00 2001 From: monsileI Date: Sun, 31 Jul 2022 23:16:20 +0900 Subject: [PATCH] =?UTF-8?q?sw=20=EC=97=AD=EB=9F=89=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20bfs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/ssafy_avanced_study/no_1226.java | 96 ++++++++++--------- .../src/ssafy_avanced_study/no_1249.java | 56 +++-------- 2 files changed, 65 insertions(+), 87 deletions(-) diff --git a/Basic_Of_Algorithm/src/ssafy_avanced_study/no_1226.java b/Basic_Of_Algorithm/src/ssafy_avanced_study/no_1226.java index 1874fc5..524de28 100644 --- a/Basic_Of_Algorithm/src/ssafy_avanced_study/no_1226.java +++ b/Basic_Of_Algorithm/src/ssafy_avanced_study/no_1226.java @@ -7,81 +7,91 @@ public class no_1226 { - static int answer = 0; - static int [][] move = {{1,0},{0,1},{-1,0},{0,-1}}; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - int T; - T= Integer.parseInt(br.readLine()); + /* 여러 개의 테스트 케이스가 주어지므로, 각각을 처리합니다. */ - - for(int test_case = 1; test_case <= T; test_case++) + + int[][]move = {{1,0},{0,1},{-1,0},{0,-1}}; + + for(int test_case = 1; test_case <= 10; test_case++) { + Integer.parseInt(br.readLine()); + int[][]map = new int[16][16]; Queue q = new LinkedList<>(); - q.add(new int[] {0,0}); int []start = new int[2]; int []end = new int[2]; + for(int i=0;i<16;i++) { - String str =br.readLine(); + String s = br.readLine(); for(int j=0;j<16;j++) { - map[i][j] = Integer.parseInt(""+str.charAt(j)); - if(map[i][j]==2) { - start[0] = i; - start[1] = j; - }else if(map[i][j]==3) { - end[0] = i; - end[1] = j; - } + map[i][j] = Integer.parseInt(""+s.charAt(j)); + } } + StringBuilder sb = new StringBuilder(); + boolean[][]visited = new boolean[16][16]; + - int[][]temp = new int[16][16]; - temp[start[0]][start[1]] = 0; - + q.add(new int[] {1,1}); + + boolean isTrue = false; - dfs(start[0],start[1],temp,end,map); + while(!q.isEmpty()) { + + int [] c = q.poll(); + int i = c[0]; + int j = c[1]; + + + + + + for(int d=0;d<4;d++) { + + int ni = i+move[d][0]; + int nj = j+move[d][1]; + + if(ni<0||nj<0||15 q = new LinkedList<>(); - - int[][]mov = {{1,0},{0,1},{-1,0},{0,-1}}; - int[][]temp = new int[size][size]; - boolean[][]visited = new boolean[size][size]; - q.add(new int[] {0,0}); - - while(!q.isEmpty()) { - - int []c = q.poll(); - int i = c[0]; - int j = c[1]; - - if(i==size-1 && j==size-1) { - answer = Math.min(answer, temp[i][j]); - continue; - } - if(answer<=temp[i][j]) continue; + int min = Integer.MAX_VALUE; + map[0][0] = Integer.parseInt(br.readLine()); + map[0][1] = Integer.parseInt(br.readLine()); + map[n+1][0] = Integer.parseInt(br.readLine()); + map[n+1][1] = Integer.parseInt(br.readLine()); - for(int d=0;d<4;d++) { - int ni = i + mov[d][0]; - int nj = j + mov[d][1]; - - if(ni<0 || nj<0|| size-1 temp[i][j] + map[ni][nj]) { - visited[ni][nj] = true; - temp[ni][nj] = temp[i][j] + map[ni][nj]; - q.add(new int[] {ni,nj}); - } - - } + for(int i=0;i<=n;i++) { + map[i][0] = Integer.parseInt(br.readLine()); + map[i][1] = Integer.parseInt(br.readLine()); } - System.out.println("#"+test_case+" "+answer);