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
55 changes: 55 additions & 0 deletions Basic_Of_Algorithm/src/test/bfs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package test;

import java.util.Queue;
import java.util.LinkedList;

public class bfs {

public static void main(String[] args) {

int[][]maps = {{1,0,1,1,1},{1,0,1,0,1},{1,0,1,1,1},{1,1,1,0,1},{0,0,0,0,1}};

int answer = 0;


int[][]temp = new int[maps.length][maps[0].length];

temp[0][0] = 1;

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

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

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

while(!q.isEmpty()) {

int [] cur = q.poll();

int cI = cur[0];
int cJ = cur[1];

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

int newI = cI + move[d][0];
int newJ = cJ + move[d][1];


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

if(temp[newI][newJ]==0 && maps[newI][newJ]==1) {
q.add(new int[] {newI,newJ});
temp[newI][newJ] = temp[cI][cJ]+1;
}


}

}


answer = temp[maps.length-1][temp[0].length-1];

System.out.println(answer);
}
}
41 changes: 41 additions & 0 deletions Basic_Of_Algorithm/src/test/dddd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package test;
import java.util.*;

public class dddd {

public static void main(String[] args) {


int[][] graph = {{}, {2,3,8}, {1,6,8}, {1,5}, {5,7}, {3,4,7}, {2}, {4,5}, {1,2}};
// 방문처리를 위한 boolean배열 선언
boolean[] visited = new boolean[9];
System.out.println(bfs(1, graph, visited));
//출력 내용 : 1 -> 2 -> 3 -> 8 -> 6 -> 5 -> 4 -> 7 ->
}
static String bfs(int start, int[][] graph, boolean[] visited) {
// 탐색 순서를 출력하기 위한 용도
StringBuilder sb = new StringBuilder();
// BFS에 사용할 큐를 생성해줍니다.
Queue<Integer> q = new LinkedList<Integer>();
// 큐에 BFS를 시작 할 노드 번호를 넣어줍니다.
q.offer(start);
// 시작노드 방문처리
visited[start] = true;
// 큐가 빌 때까지 반복
while(!q.isEmpty()) {
int nodeIndex = q.poll();
sb.append(nodeIndex + " -> ");
//큐에서 꺼낸 노드와 연결된 노드들 체크
for(int i=0; i<graph[nodeIndex].length; i++) {
int temp = graph[nodeIndex][i];
// 방문하지 않았으면 방문처리 후 큐에 넣기
if(!visited[temp]) {
visited[temp] = true;
q.offer(temp);
}
}
}
// 탐색순서 리턴
return sb.toString() ;
}
}
16 changes: 16 additions & 0 deletions Basic_Of_Algorithm/src/test/dfs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test;

public class dfs {

public static void main(String[] args) {

int[][]maps = {{1,0,1,1,1},{1,0,1,0,1},{1,0,1,1,1},{1,1,1,0,1},{0,0,0,0,1}};

int answer = 0;





}
}