- 난이도: Lv2
프로그래머스 링크: https://school.programmers.co.kr/learn/courses/30/lessons/250136
풀이 링크(GitHub): hayannn/CodingTest_Java/프로그래머스/2/250136. [PCCP 기출문제] 2번 / 석유 시추
풀이 시간 : 37분
import java.util.*;
class Solution {
static int N;
static int M;
static boolean[][] visited;
static int[] arr;
static int count;
static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, 1, 0, -1};
static int[][] map;
static int answer;
public int solution(int[][] land) {
N = land[0].length;
M = land.length;
answer = 0;
map = land;
arr = new int[M];
visited = new boolean[M][N];
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
if(visited[i][j] || map[i][j] == 0){
continue;
}
bfs(i, j);
}
}
int answer = 0;
for(int i=0; i<M; i++){
answer = Math.max(answer, arr[i]);
}
return answer;
}
public static int bfs(int x, int y){
Queue<int[]> q = new LinkedList<>();
Set<Integer> set = new LinkedHashSet<>();
q.offer(new int[]{x, y});
visited[x][y] = true;
int count = 1;
while(!q.isEmpty()){
int[] current = q.poll();
set.add(current[0]);
for(int i=0; i<4; i++){
int cx = current[0] + dx[i];
int cy = current[1] + dy[i];
if(!(x>=0 && x<N && y>=0 && y<M) || map[x][y] == 0 || visited[x][y]){
continue;
}
visited[x][y] = true;
count++;
q.offer(new int[]{x, y});
}
}
for(int current : set){
arr[current] += count;
}
return count;
}
}
//before
...
arr = new int[M];
visited = new boolean[M][N];
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
if(visited[i][j] || map[i][j] == 0){
continue;
}
bfs(i, j);
}
}
...
for(int i=0; i<4; i++){
int cx = current[0] + dx[i];
int cy = current[1] + dy[i];
if(!(x>=0 && x<N && y>=0 && y<M) || map[x][y] == 0 || visited[x][y]){
continue;
}
visited[x][y] = true;
count++;
q.offer(new int[]{x, y});
}
}
for(int current : set){
arr[current] += count;
}
return count;
}
}
//after
...
arr = new int[M];
visited = new boolean[N][M];
for(int j=0; j<M; j++){
for(int i=0; i<N; i++){
if(visited[i][j] || map[i][j] == 0){
continue;
}
bfs(i, j);
}
}
...
for(int i=0; i<4; i++){
int nx = current[0] + dx[i];
int ny = current[1] + dy[i];
if(nx<0 || nx>=N || ny<0 || ny>=M || map[nx][ny] == 0 || visited[nx][ny]){
continue;
}
visited[nx][ny] = true;
count++;
q.offer(new int[]{nx, ny});
}
}
for(int current : set){
arr[current] += count;
}
return count;
}
}
풀이 시간 : 1시간 16분(첫 풀이 시간 포함)
import java.util.*;
class Solution {
static int N;
static int M;
static boolean[][] visited;
static int[] arr;
static int count;
static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, 1, 0, -1};
static int[][] map;
static int answer;
public int solution(int[][] land) {
M = land[0].length;
N = land.length;
answer = 0;
map = land;
arr = new int[M];
visited = new boolean[N][M];
for(int j=0; j<M; j++){
for(int i=0; i<N; i++){
if(visited[i][j] || map[i][j] == 0){
continue;
}
bfs(i, j);
}
}
int answer = 0;
for(int i=0; i<M; i++){
answer = Math.max(answer, arr[i]);
}
return answer;
}
public static int bfs(int x, int y){
Queue<int[]> q = new LinkedList<>();
Set<Integer> set = new LinkedHashSet<>();
q.offer(new int[]{x, y});
visited[x][y] = true;
int count = 1;
while(!q.isEmpty()){
int[] current = q.poll();
set.add(current[1]);
for(int i=0; i<4; i++){
int nx = current[0] + dx[i];
int ny = current[1] + dy[i];
if(nx<0 || nx>=N || ny<0 || ny>=M || map[nx][ny] == 0 || visited[nx][ny]){
continue;
}
visited[nx][ny] = true;
count++;
q.offer(new int[]{nx, ny});
}
}
for(int current : set){
arr[current] += count;
}
return count;
}
}