- 생각
맵에서 1은 섬을 나타내고 0은 바다를 나타낸다 섬은 상하좌우 대각선 총 8방향으로 이어져있을때, 주어진 배열의 섬의 수를 구하는 문제이다. bfs를 이용하여 8방향을 탐색하면 될 것 같다라고 생각했다.
- 코드
정답 코드 : dfs를 통해 구현하였다. check되지 않고 1인 수를 발견하면 주변에 포함된 1의 좌표를 check해주며 result+1을 해준다.
import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
public class Main {
static final int[][] xy = {{0, 1}, {0, -1}, {1, 0}, {1, 1}, {1, -1}, {-1, 0}, {-1, 1}, {-1, -1}};
static boolean[][] islands;
static StringBuilder sb;
public static void main(String[] args) throws Exception {
SetData();
System.out.print(sb);
}
// 데이터
private static void SetData() throws Exception {
InputReader in = new InputReader(System.in);
sb = new StringBuilder();
while(true) {
int x = in.nextInt();
int y = in.nextInt();
if(x == 0 && y == 0) {
return;
}
islands = new boolean[y + 2][x + 2];
for(int i = 1; i <= y; i++) {
for(int j = 1; j <= x; j++) {
islands[i][j] = in.nextInt() > 0;
}
}
int result = 0;
for(int i = 1; i <= y; i++) {
for(int j = 1; j <= x; j++) {
if(islands[i][j]) {
result++;
dfs(i, j, islands);
}
}
}
sb.append(result + "\n");
}
}
private static void dfs(int x, int y, boolean[][] islands) {
for(int[] XY : xy) {
int r = x + XY[0];
int c = y + XY[1];
if(islands[r][c]) {
islands[r][c] = false;
dfs(r, c, islands);
}
}
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
'algorithm' 카테고리의 다른 글
[BOJ/JAVA] 백준 17136번 : 색종이 붙이기 (0) | 2021.02.26 |
---|---|
[BOJ/JAVA] 백준 13977번 : 이항 계수와 쿼리 (0) | 2021.02.25 |
[BOJ/JAVA] 백준 5557번 : 1학년 (0) | 2021.02.23 |
[BOJ/JAVA] 백준 1027번 : 고층 건물 (0) | 2021.02.22 |
[BOJ/JAVA] 백준 1012번 : 유기농 배추 (0) | 2021.02.19 |