Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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);
}
}


}
}
```