diff --git a/Basic_Of_Algorithm/src/ssafy_avanced_study/no_1248.java b/Basic_Of_Algorithm/src/ssafy_avanced_study/no_1248.java new file mode 100644 index 0000000..9ce4e96 --- /dev/null +++ b/Basic_Of_Algorithm/src/ssafy_avanced_study/no_1248.java @@ -0,0 +1,80 @@ +package ssafy_avanced_study; + + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.Queue; +import java.util.LinkedList; + +public class no_1248 { + + + + + 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 size =Integer.parseInt(br.readLine()); + + int[][]map = new int[size][size]; + + int answer = Integer.MAX_VALUE; + + + for(int i=0;i 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; + + 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}); + } + + } + + } + + System.out.println("#"+test_case+" "+answer); + + + + } + + } + +} \ No newline at end of file diff --git a/Basic_Of_Algorithm/src/ssafy_avanced_study/no_1249.java b/Basic_Of_Algorithm/src/ssafy_avanced_study/no_1249.java index 16557c5..6882c78 100644 --- a/Basic_Of_Algorithm/src/ssafy_avanced_study/no_1249.java +++ b/Basic_Of_Algorithm/src/ssafy_avanced_study/no_1249.java @@ -8,7 +8,7 @@ public class no_1249 { - static int answer = Integer.MAX_VALUE; + public static void main(String[] args) throws Exception { @@ -24,63 +24,57 @@ public static void main(String[] args) throws Exception { int[][]map = new int[size][size]; - Queue q = new LinkedList<>(); - - q.add(new int[] {0,0}); + int answer = Integer.MAX_VALUE; + for(int i=0;i q = new LinkedList<>(); + int[][]mov = {{1,0},{0,1},{-1,0},{0,-1}}; int[][]temp = new int[size][size]; - temp[0][0] = 0; - - - - - dfs(0,0,temp,map,size); - - System.out.println(answer); - - } - - - } - static void dfs(int i,int j,int[][]temp,int[][]map,int size) { - - - if(map[i][j]>=answer) return; - - int [][] move = {{1,0},{0,1},{-1,0},{0,-1}}; - - - if(i==size-1 && j ==size-1) { - answer = Math.min(answer, temp[size-1][size-1]); - return; - } - - for(int d=0;d<4;d++) { - - int newI = i + move[d][0]; - int newJ = j + move[d][1]; + boolean[][]visited = new boolean[size][size]; + q.add(new int[] {0,0}); - if(newI<0||newJ<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}); + } + + } + + } + System.out.println("#"+test_case+" "+answer); - if(temp[newI][newJ]==0) { - temp[newI][newJ] = temp[i][j] + map[newI][newJ]; - dfs(newI,newJ,temp,map,size); - temp[newI][newJ] = 0; - } } + } } \ No newline at end of file