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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
67 changes: 67 additions & 0 deletions source/KMJ/그래프탐색/BJ2667.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
## BJ2667
### 단지번호 붙이기
```java
import java.io.*;
import java.util.*;

public class BJ2667 {
private static int N; //지도 크기
private static int[][] map;
private static boolean[][] visited; //방문 여부 체크

private static int[] complex; //단지 속하는 집 개수 저장
private static int complexNum = 0; //단지 번호

private static int[] dx = {-1, 1, 0, 0}; //좌우위아래 순서
private static int[] dy = {0, 0,-1, 1};

public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());

map = new int[N][N];
visited = new boolean[N][N];
complex = new int[N*N];

//map 입력
for(int i=0; i<N; i++){
String input = br.readLine();
for(int j=0; j<N; j++){
map[i][j] = input.charAt(j) - '0'; //해당 문자열의 인덱스 값 int형
}
}

for(int i=0; i<N;i++){
for(int j=0; j<N; j++){
if(map[i][j] == 1 && !visited[i][j]){
complexNum++;
dfs(i, j);
}
}
}
System.out.println(complexNum);
Arrays.sort(complex, 1, complexNum+1);

for(int i=1; i<=complexNum; i++)
System.out.println(complex[i]);

br.close();

}

static void dfs(int x, int y){
visited[x][y] = true; //방문함
complex[complexNum]+=1;

for(int i=0; i<4; i++){
int xx = x+dx[i];
int yy = y+dy[i];

if(xx >=0 && xx<N && yy>=0 && yy<N){
if(map[xx][yy] == 1 && !visited[xx][yy]){
dfs(xx, yy);
}
}
}
}
}```
92 changes: 92 additions & 0 deletions source/KMJ/그래프탐색/bj1260.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@



## bj

```java
//package p1260; //error occurs

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class DFS_BFS {

//DFS 구현
public static void dfs(int start, LinkedList<Integer>[] adj, boolean[] visited) {
visited[start] = true;
System.out.print(start + " ");

Iterator<Integer> iter = adj[start].listIterator();
while (iter.hasNext()) {
int nextStart = iter.next();
if (!visited[nextStart]) {
dfs(nextStart, adj, visited);
}

}

}
//BFS 구현 - bfs는 queue(FIFO 형식)로 구현
public static void bfs(int start, LinkedList<Integer>[] adj, boolean[] visited) {

Queue<Integer> queue = new LinkedList<Integer>();
visited[start] = true;
queue.add(start); // queue에 값 넣기

while (queue.size() != 0) {
start = queue.poll(); // queue의 첫 번째 값 꺼내기
System.out.print(start + " ");

Iterator<Integer> iter = adj[start].listIterator();
while (iter.hasNext()) { // 인접한 정점 존재하면
int nextStart = iter.next();
if (!visited[nextStart]) {
visited[nextStart] = true;
queue.add(nextStart); // queue에 값 넣기
}
}
}
}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt(); // 정점 개수
int m = sc.nextInt(); // 간선 개수
int v = sc.nextInt(); // 시작 정점 번호

boolean[] visited = new boolean[n + 1]; // 방문 확인 배열

LinkedList<Integer>[] adjList = new LinkedList[n + 1]; // adjacent list로 표현

for (int i = 0; i <= n; i++) { //각 인접노드와 연결된 노드도 linked list로 정의
adjList[i] = new LinkedList<Integer>();
}

for (int i = 0; i < m; i++) { // 간선 입력 및 저장
int m1 = sc.nextInt();
int m2 = sc.nextInt();
adjList[m1].add(m2); // 양방향
adjList[m2].add(m1);
}

for (int i = 1; i <= n; i++) { // 정점 번호 오름차순 배치
Collections.sort(adjList[i]);
}

dfs(v, adjList, visited); // dfs 실행

for (int i = 0; i <= n; i++) { //array 초기화
visited[i] = false;
}

System.out.println();

bfs(v, adjList, visited); // bfs 실행
}
}
```