BOJ#2667 단지번호붙이기
* 문제
https://www.acmicpc.net/problem/2667
* 풀이
dfs
난이도 : 하
추천 문제 (2667번 다음에 풀어보면 좋을 문제) :
2146번 - 다리 만들기 https://www.acmicpc.net/problem/2146
* 나의 코드
https://github.com/stack07142/BOJ/blob/master/BOJ%232667_Numbering/src/Main.java
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
/**
* BOJ#2667 단지번호붙이기
* https://www.acmicpc.net/problem/2667
*/
public class Main {
static final int[] dRow = {0, -1, 0, 1};
static final int[] dCol = {-1, 0, 1, 0};
static int N;
static int[][] map = new int[25][25];
static boolean[][] visited = new boolean[25][25];
static ArrayList<Integer> a = new ArrayList<Integer>();
public static void main(String[] args) throws IOException {
// input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
String s = br.readLine();
for (int j = 0; j < N; j++) {
map[i][j] = s.charAt(j) - '0';
}
}
// solve
int name = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!visited[i][j] && map[i][j] != 0) {
name++;
int cnt = dfs(i, j, name, 0);
a.add(cnt);
}
}
}
Collections.sort(a);
System.out.println(name);
for (int x : a) {
System.out.println(x);
}
}
static int dfs(int row, int col, int name, int cnt) {
cnt++;
visited[row][col] = true;
for (int i = 0; i < 4; i++) {
int nextRow = row + dRow[i];
int nextCol = col + dCol[i];
if (nextRow < 0 || nextRow >= N || nextCol < 0 || nextCol >= N) continue;
if (map[nextRow][nextCol] == 0) continue;
if (visited[nextRow][nextCol]) continue;
cnt = dfs(nextRow, nextCol, name, cnt);
}
return cnt;
}
}
'Algorithm > 그래프 탐색' 카테고리의 다른 글
BOJ#1726 로봇 (4) | 2017.04.12 |
---|---|
BOJ#7569 토마토 (0) | 2017.04.11 |
BOJ#2589 보물섬 (0) | 2017.04.10 |
BOJ#2146 다리 만들기 (0) | 2017.04.10 |
BOJ#1182 부분집합의 합 (0) | 2017.04.07 |