[2017 Kakao Code予選]Kakao友人彩書(JAVA)
18572 ワード
質問する
に答える
DFSでアクセスしましたが、答えが見つからず、BFSで解決するのがもっと効果的な問題であることに気づきました…!また,QueueよりもStackの方が有効な方法である.
すべての座標
maxSizeOfOneArea
が更新される.コード#コード#
import java.util.*;
class Solution {
private static final int NULL = 0;
private static final int[] dx = {1, -1, 0, 0};
private static final int[] dy = {0, 0, 1, -1};
private int[][] pic;
private int width, height;
private boolean[][] visited;
private int numberOfArea;
private int maxSizeOfOneArea;
public int[] solution(int m, int n, int[][] picture) {
this.pic = picture;
this.height = m;
this.width = n;
this.visited = new boolean[height][width];
for (boolean[] each : visited) Arrays.fill(each, false);
bfs();
return new int[]{this.numberOfArea, this.maxSizeOfOneArea};
}
private void bfs() {
Stack<Point> stack = new Stack<>();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int count = 0;
if (!visited[y][x] && pic[y][x] != NULL) {
visited[y][x] = true;
stack.push(new Point(y, x));
count++;
numberOfArea++;
}
while (!stack.isEmpty()) {
Point current = stack.pop();
for (int i = 0; i < dy.length; i++) {
Point next = new Point(current.y + dy[i], current.x + dx[i]);
if (next.isPointInBound(width, height) && !visited[next.y][next.x] && pic[next.y][next.x] == pic[current.y][current.x]) {
visited[next.y][next.x] = true;
stack.push(next);
count++;
}
}
}
maxSizeOfOneArea = Math.max(count, maxSizeOfOneArea);
}
}
}
}
class Point {
int y;
int x;
public Point(int y, int x) {
this.y = y;
this.x = x;
}
public boolean isPointInBound(int width, int height) {
return isXInBound(width) && isYInBound(height);
}
private boolean isXInBound(int width) {
return this.x >= 0 && this.x < width;
}
private boolean isYInBound(int height) {
return this.y >= 0 && this.y < height;
}
}
リファレンス
[プログラマー][JAVA]カカカフコンタクトレンズ彩本
Reference
この問題について([2017 Kakao Code予選]Kakao友人彩書(JAVA)), 我々は、より多くの情報をここで見つけました https://velog.io/@jwkim/2017-kakao-code-coloring-bookテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol