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
89 changes: 40 additions & 49 deletions Basic_Of_Algorithm/src/ssafy_avanced_study/no_1226.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

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));
Expand All @@ -25,72 +28,60 @@ public static void main(String[] args) throws Exception {

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();
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;
}
}
}



int[][]temp = new int[16][16];
temp[0][0] = 0;
int [][] move = {{1,0},{0,1},{-1,0},{0,-1}};
temp[start[0]][start[1]] = 0;

int answer = Integer.MAX_VALUE;
int answerI = 0;
int answerJ = 0;

for(int i=0;i<16;i++) {
for(int j=0;j<16;j++) {
if(map[i][j]==2) {
temp[i][j] = 0;
q.add(new int[] {i,j});
}else if(map[i][j]==3) {
answerI = i;
answerJ = j;
}

}
}
dfs(start[0],start[1],temp,end,map);

System.out.println(q.poll()[0]);
System.out.println(q.poll()[1]);

while(!q.isEmpty()) {

int [] cur = q.poll();
int i = cur[0];
int j = cur[1];

for(int d=0;d<4;d++) {

int newI = i + move[d][0];
int newJ = j + move[d][1];


if(newI<0||newJ<0||15<newI||15<newJ) continue;

if(map[newI][newJ]==0 && temp[newI][newJ]==0) {
temp[newI][newJ] = temp[i][j] + 1;
q.add(new int[] {newI,newJ});
}




}



}

if(temp[answerI][answerJ] !=0) answer = 1;
else answer = 0;
}


}
static void dfs(int i,int j,int[][]temp,int[]end,int[][]map) {


if(i==end[0] && j==end[1]) {
answer = 1;
return;
}

for(int d=0;d<4;d++) {
int newI = i + move[d][0];
int newJ = j + move[d][1];

if(newI<0||newJ<0||temp.length-1<newI||temp[0].length-1<newJ) continue;

System.out.println(answer);
if(temp[newI][newJ]==0 && map[newI][newJ]==0) {
temp[newI][newJ] = temp[i][j] + 1;
dfs(newI, newJ, temp, end, map);
temp[newI][newJ] = 0;
}
}



}

}
23 changes: 23 additions & 0 deletions Basic_Of_Algorithm/src/ssafy_avanced_study/no_1247.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ssafy_avanced_study;

import java.util.Scanner;

public class no_1247 {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int T;
T=sc.nextInt();
/*
여러 개의 테스트 케이스가 주어지므로, 각각을 처리합니다.
*/

for(int test_case = 1; test_case <= T; test_case++){



}
}
}
71 changes: 36 additions & 35 deletions Basic_Of_Algorithm/src/ssafy_avanced_study/no_1249.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

public class no_1249 {

static int answer = Integer.MAX_VALUE;


public static void main(String[] args) throws Exception {

Expand Down Expand Up @@ -38,48 +40,47 @@ public static void main(String[] args) throws Exception {

int[][]temp = new int[size][size];
temp[0][0] = 0;
int [][] move = {{1,0},{0,1},{-1,0},{0,-1}};

int answer = Integer.MAX_VALUE;

while(!q.isEmpty()) {

int [] cur = q.poll();
int i = cur[0];
int j = cur[1];

for(int d=0;d<4;d++) {

int newI = i + move[d][0];
int newJ = j + move[d][1];


if(newI<0||newJ<0||size-1<newI||size-1<newJ) continue;


temp[newI][newJ] = map[newI][newJ] + temp[i][j];

q.add(new int[] {newI,newJ});

if(newI==size-1 && newJ==size-1) {

answer = Math.min(answer, temp[newI][newJ]);
}



}



}




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];

if(newI<0||newJ<0||size-1<newI||size-1<newJ) continue;


if(temp[newI][newJ]==0) {
temp[newI][newJ] = temp[i][j] + map[newI][newJ];
dfs(newI,newJ,temp,map,size);
temp[newI][newJ] = 0;
}


}
}

}