Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Basic_Of_Algorithm/src/ssafy_avanced_study/no_1248.java
Original file line number Diff line number Diff line change
@@ -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<size;i++) {
String s = br.readLine();
for(int j=0;j<size;j++) {
map[i][j] = Integer.parseInt(""+s.charAt(j));
}
}

Queue<int[]> 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 <ni|| size-1 < nj) continue;

if(!visited[ni][nj] || temp[ni][nj] > 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);



}

}

}
82 changes: 38 additions & 44 deletions Basic_Of_Algorithm/src/ssafy_avanced_study/no_1249.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class no_1249 {

static int answer = Integer.MAX_VALUE;



public static void main(String[] args) throws Exception {
Expand All @@ -24,63 +24,57 @@ public static void main(String[] args) throws Exception {

int[][]map = new int[size][size];

Queue<int []> q = new LinkedList<>();

q.add(new int[] {0,0});
int answer = Integer.MAX_VALUE;


for(int i=0;i<size;i++) {
String str =br.readLine();
String s = br.readLine();
for(int j=0;j<size;j++) {
map[i][j] = Integer.parseInt(""+str.charAt(j));
map[i][j] = Integer.parseInt(""+s.charAt(j));
}
}



Queue<int[]> 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<newI||size-1<newJ) continue;
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 <ni|| size-1 < nj) continue;

if(!visited[ni][nj] || temp[ni][nj] > 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;
}


}

}

}