https://www.acmicpc.net/problem/2667
2667번: 단지번호붙이기
<그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여
www.acmicpc.net
import java.util.*;
public class Main {
static int N;
static int[][] map;
static boolean[][] visited;
static int[] dx = {0, 0, -1, 1}; // x좌표 상하좌우
static int[] dy = {-1, 1, 0, 0}; // y좌표 상하좌우
static int complex = 0; // 단지 수
static int home = 0; // 집 수
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
map = new int[N][N];
visited = new boolean[N][N];
for(int i = 0; i < N; i++) {
String num = sc.next();
for(int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(""+num.charAt(j));
}
}
List<Integer> homeList = new ArrayList<>(); // 집 수 모아줄 리스트
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(map[i][j] == 1 && !visited[i][j]) { // 값이 1이거나 아직 방문x
dfs(i, j); // 상하좌우 확인
complex++; // 한번 돌고 오면 단지 수 올려줌
homeList.add(home);
home = 0;
}
}
}
Collections.sort(homeList);
System.out.println(complex);
for(int i = 0; i < homeList.size(); i++) {
System.out.println(homeList.get(i));
}
}
static void dfs(int x, int y) {
visited[x][y] = true; // 방문 표시
home++; // 집 수 더해줌
for (int i = 0; i < 4; i++) {
int nextX = x + dx[i];
int nextY = y + dy[i];
if (nextX >= 0 && nextX < N && nextY >= 0 && nextY < N) {
if (map[nextX][nextY] == 1 && !visited[nextX][nextY]) {
dfs(nextX, nextY);
}
}
}
}
}
상하좌우로 다 검사해야할때 dx, dy로 돌아주는거 잊지말기!!
알거같으면서도 모르겠는 dfs,,, 많이 풀고 익숙해져야겠다
+
2024.02.14
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
static int N;
static int[][] map;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static int count = 0; // 단지 내 집의 수
static List<Integer> home; // count 모아줄 리스트
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];
home = new ArrayList<>();
for(int i = 0; i < N; i++) {
String[] str = br.readLine().split("");
for(int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(str[j]);
}
}
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(map[i][j] == 1) {
check(i, j);
home.add(count);
}
count = 0;
}
}
Collections.sort(home);
System.out.println(home.size());
for(int i = 0; i < home.size(); i++) {
System.out.println(home.get(i));
}
}
static void check(int x, int y) {
count++;
map[x][y] = 0;
for(int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(nx >= 0 && nx < N && ny >= 0 && ny < N) {
if(map[nx][ny] == 1) check(nx, ny);
}
}
}
}
약 한달전에 혼자 풀었던 문제인데 이번에 코테 스터디 과제로 다시 풀게 됐다
이전에 남겼던 말을 보니 상하좌우로 도는거도 익숙하지 않았고 dfs도 익숙하지 않았던거 같은데 이제는 꽤 익숙해져서 바로 풀 수 있는 유형의 문제가 됐고 더 간단하게 빠르게 풀 수 있게 됐다 그때보다 나아진게 스스로도 느껴져서 굉장히 뿌듯하고 신기하다 앞으로도 열심히 해야지 😎😎

'코딩테스트-알고리즘 > 백준 BOJ' 카테고리의 다른 글
[백준 BOJ/Silver II] 4963번 : 섬의 개수 (0) | 2024.01.18 |
---|---|
[백준 BOJ/Silver II] 1012번 : 유기농 배추 (0) | 2024.01.18 |
[백준 BOJ/Silver V] 11651번 : 좌표 정렬하기 2 (0) | 2024.01.17 |
[백준 BOJ/Silver V] 11650번 : 좌표 정렬하기 (1) | 2024.01.15 |
[백준 BOJ/Silver V] 1181번 : 단어 정렬 (2) | 2024.01.14 |