Skip to content
Open
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
83 changes: 83 additions & 0 deletions source/KJH/그래프탐색/220524/bj3184.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
## 양

dfs/bfs 다시 시작하겠습니다..

```java
import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class Main{
static int R,C;
static char[][] maps;
static boolean[][] visited;
static int dx[] = {1,0,-1,0};
static int dy[] = {0,1,0,-1};
static Queue<int[]> queue = new LinkedList<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
visited =new boolean[R][C];
maps = new char[R][C];

int vcount = 0;
int ocount =0;
for(int i=0; i<R; i++){
String s = br.readLine();
for(int j=0; j<C; j++){
char x = s.charAt(j);
maps[i][j] = x;


}
}


for(int i=0; i<R; i++){
for(int j=0; j<C; j++){
if(!visited[i][j] && maps[i][j] == 'V'){
dfs(i,j);
}
}
}
}

public static void dfs(int x ,int y){
visited[x][y] = true;
int ocount =0;
int vcount =0;
queue.add(new int[]{x,y});
while(!queue.isEmpty()){
int[] now = queue.poll();
int nowX = now[0];
int nowY = now[1];

for(int i=0; i<4; i++){
int nX = nowX + dx[i];
int nY = nowY + dy[i];

if(nX >=0 && nX < R && nY >=0 && nY < C){
if(maps[nX][nY] == 'V' && !visited[nX][nY]){
queue.add(new int[]{nX,nY});
vistied[nX][nY] = true;
vcount +=1;
}
else if(maps[nX][nY] == '#'){
continue;
}
else if(maps[nX][nY] == '.' && !visited[nX][nY]){
queue.add(new int[]{nX,nY});
visited[nX][nY] = true;
}
else if(maps[nX][nY] == 'O'){
ocount += 1;
}
}

}
}
}
}
```
57 changes: 57 additions & 0 deletions source/KJH/그래프탐색/220525/bj2606.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
## 바이러스

예전에 풀었던 문제라 쉬웠따
bfs지만, queue를 사용하지 않고 재귀를 통해 풀었다.

```java

import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;

public class Main{

static int n,m;
static ArrayList<Integer>[] arrayList;
static boolean[] visited;
static int count=0;
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

n = Integer.parseInt(br.readLine()); //컴퓨터 수
m = Integer.parseInt(br.readLine()); //컴퓨터 번호 쌍
arrayList = new ArrayList[n+1];
visited = new boolean[n+1];

for(int i=1; i<=n; i++){
arrayList[i] = new ArrayList<Integer>();
}

for(int i=0; i<m; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
arrayList[x].add(y);
arrayList[y].add(x);
}

bfs(1);
System.out.println(count);
}

public static void bfs(int num){
visited[num] = true;

for(int x : arrayList[num]){
if(!visited[x]){
count+=1;
bfs(x);
}
}


}
}
```
52 changes: 52 additions & 0 deletions source/KJH/그래프탐색/220527/bj11403.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## 경로 찾기

```javascript
import java.io.BufferedReader;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
int[][] arr = new int[n][n];

for(int i=0; i<n; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j=0; j<n; j++){
arr[i][j] = Integer.parseInt(st.nextToken());

}
}

for(int k=0; k<n; k++){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(arr[i][k] ==1 && arr[k][j] == 1){
arr[i][j] =1;
}
}
}
}

StringBuilder sb = new StringBuilder();
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
sb.append(arr[i][j] + " ");
}
sb.append("\n");
}

bw.write(sb.toString());
bw.flush();
bw.close();
br.close();

}
}
```
49 changes: 49 additions & 0 deletions source/KJH/그래프탐색/220529/bj11725.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## 트리의 부모찾기

parents 생각하는 부분이 어려웠다.

```java
import java.util.*;
import java.io.*;
public class Main
{
static boolean visited[];
static int N;
static ArrayList<Integer>[] arr;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
arr = new ArrayList[N+1];
visited = new boolean[N+1];
for(int i=1; i<N+1; i++){
arr[i] = new ArrayList();
}

for(int i=0; i<N-1; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());

arr[a].add(b);
arr[b].add(a);
}

int[] parents = new int[N+1];

dfs(parents, 1,0);

for(int j=2; j<=N; j++) System.out.println(parents[j]);

}
public static void dfs(int[] parents, int start,int parent){
parents[start] = parent; // 1부터 시작하기 때문에 1은 먼저 루트로 만들어준다
for(int x: arr[start]){
if(x != parent){
dfs(parents, x, start); //1을 통해 찾은 다음것인기 때문에 1이 부모가된다

}
}
}
}

```
76 changes: 76 additions & 0 deletions source/KJH/그래프탐색/220606/bj7562.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
## 나이트의 이동

class 만드는것 참고해서 만들어보았습니다.

```java
import java.io.*;
import java.util.*;

public class Main{
static int l;
static int dx[] = {-2,-1,1,2,-2,-1,1,2};
static int dy[] = {1,2,2,1,-1,-2,-2,-1};
static Point now, goal;
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());


ArrayList<Integer> arrlist = new ArrayList<>();

for(int i=0; i<T; i++){
l = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
now = new Point(Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()), 0);
st = new StringTokenizer(br.readLine());
goal = new Point(Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()), 0);

bfs(now);

}


}

static void bfs(Point p){
boolean visited[][] = new boolean[l][l];
Queue<Point> queue = new LinkedList<>();
visited[p.r][p.c] = true;
queue.add(p);

while(!queue.isEmpty()){
Point tmp = queue.poll();
int tmpX = tmp.r;
int tmpY = tmp.c;
int cnt = tmp.cnt;

if(tmpX == goal.r && tmpY == goal.c){
System.out.println(tmp.cnt);
return;
}
for(int i=0; i<8; i++){
int nx = tmpX + dx[i];
int ny = tmpY + dy[i];

if(nx >= 0 && nx < l && ny>= 0 && ny< l && !visited[nx][ny]){
queue.add(new Point(nx, ny, cnt+1));
visited[nx][ny] = true;
}
}
}
}

static class Point{
int r, c, cnt;

public Point(int r, int c, int cnt){
this.r = r;
this.c = c;
this.cnt = cnt;
}
}
}

```