Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- SpringBatch
- 트리맵
- JPA
- 구현
- 산업은행it
- fatch
- BFS
- CS
- DB replication
- flyway
- 임베디드타입
- 그래프탐색
- 외래키제약조건위반
- 프로그래머스
- 컴퓨터구조
- 폰켓몬
- Spring JPA
- 트리셋
- springboot
- 해시
- 스케일아웃
- 운영체제
- 백준
- 프로젝트
- 산업은행청년인턴
- 파이널프로젝트
- 2178
- CPU스케줄링
- findById
- 코테
Archives
- Today
- Total
나 JAVA 봐라
[코드트리] 마을 구분하기 문제 본문
카테고리는 DFS로 분류 되긴 했지만, DFS/BFS 둘다 가능할 것 같아서 두 방식으로 모두 풀어봤다.
Code Tree | Learning to Code with Confidence
A super-comprehensive, meticulously arranged Coding Learning Curriculum engineered by Algorithm Experts composed of former International Olympiad in Informatics (IOI) medalists.
www.codetree.ai
DFS
// BFS, DFS 두 방식을 고민했음... -> 두 가지 다 풀어봐야겠다.
import java.util.*;
public class Main {
public static int n,people_cnt;
public static int dx[] = {0,1,0,-1};
public static int dy[] = {1,0,-1,0};
public static int grid[][];
public static boolean visit[][];
public static ArrayList<Integer> cnt_list = new ArrayList<>();
public static boolean inRange(int x, int y){
return x >= 0 && x < n && y >= 0 && y <n;
}
public static boolean canGo(int x, int y){
return inRange(x,y) && !visit[x][y] && (grid[x][y]==1);
}
public static void dfs(int x, int y){
// 상하좌우 탐색하기
for (int i =0; i < 4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if (canGo(nx,ny)){
people_cnt++;
visit[nx][ny] = true;
dfs(nx,ny);
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
grid = new int[n][n];
visit = new boolean[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
grid[i][j] = sc.nextInt();
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (canGo(i,j)){
visit[i][j] = true;
people_cnt = 1;
dfs(i,j);
cnt_list.add(people_cnt);
}
}
}
Collections.sort(cnt_list);
System.out.println(cnt_list.size());
for (Integer value : cnt_list){
System.out.println(value);
}
}
}
BFS
import java.util.*;
public class Main {
public static int n, people_cnt;
public static int dx[] = {0, 1, 0, -1};
public static int dy[] = {1, 0, -1, 0};
public static int grid[][];
public static boolean visit[][];
public static ArrayList<Integer> cnt_list = new ArrayList<>();
public static boolean inRange(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < n;
}
public static boolean canGo(int x, int y) {
return inRange(x, y) && !visit[x][y] && (grid[x][y] == 1);
}
public static void bfs(int x, int y) {
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{x, y});
visit[x][y] = true;
people_cnt = 1;
while (!queue.isEmpty()) {
int[] current = queue.poll();
int curX = current[0], curY = current[1];
for (int i = 0; i < 4; i++) {
int nx = curX + dx[i];
int ny = curY + dy[i];
if (canGo(nx, ny)) {
queue.add(new int[]{nx, ny});
visit[nx][ny] = true;
people_cnt++;
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
grid = new int[n][n];
visit = new boolean[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
grid[i][j] = sc.nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (canGo(i, j)) {
bfs(i, j);
cnt_list.add(people_cnt);
}
}
}
Collections.sort(cnt_list);
System.out.println(cnt_list.size());
for (Integer value : cnt_list) {
System.out.println(value);
}
}
}
'코딩테스트 > 그래프 탐색' 카테고리의 다른 글
[프로그래머스] 여행경로 (0) | 2024.07.17 |
---|---|
[프로그래머스] 전력망을 둘로 나누기 (4) | 2024.07.15 |
[프로그래머스] 피로도 (0) | 2024.07.15 |
[백준] 15686번 치킨 배달 (0) | 2024.03.18 |
[백준] 14940번 쉬운 최단거리 (2) | 2024.02.05 |