728x90
๐๋ฌธ์ ๋งํฌ
https://school.programmers.co.kr/learn/courses/30/lessons/250136?language=java
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก, ์ฑ์ฉ๊น์ง Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
๐ป์ฝ๋
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
import java.util.Queue;
import java.util.LinkedList;
class Solution {
static int[] totalCount;
public int solution(int[][] land) {
int answer = 0;
int n = land.length;
int m = land[0].length;
totalCount = new int[m];
boolean[][] visited = new boolean[n][m];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(!visited[j][i] && land[j][i] == 1){//์์ ๊ฐ ์์
bfs(n, m, j, i, land, visited);//ํ์ฌ ์์น์์ ์์ ํ์
}
}
answer = Arrays.stream(totalCount).max().getAsInt();
}
return answer;
}
static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, 1, 0, -1};
public void bfs(int n, int m, int x, int y, int[][] land, boolean[][] visited){
int count = 0;
Queue<int[]> q = new LinkedList<>();
Set<Integer> set = new HashSet<>();
q.add(new int[]{x, y});
visited[x][y] = true;
count++;
while(!q.isEmpty()){
int[] cur = q.poll();
set.add(cur[1]);
for(int i = 0; i < 4; i++){
int nx = cur[0] + dx[i];
int ny = cur[1] + dy[i];
if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if(!visited[nx][ny] && land[nx][ny] == 1){
q.add(new int[]{nx, ny});
visited[nx][ny] = true;
count++;
}
}
}
for (int index : set) {
totalCount[index] += count;
}
}
}
728x90
'Algorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithm/Programmers] ๋ฌด์ธ๋ ์ฌํ - 154540 (L2/JAVA) (0) | 2023.08.03 |
---|---|
[Algorithm/Programmers] ๊ด๋ฌผ ์บ๊ธฐ - 172927 (L2/JAVA) (0) | 2023.07.12 |
[Algorithm/Programmers] ํธ๋ ํ์ดํธ ๋ํ - 134240 (L1/JAVA) (0) | 2023.06.24 |
[Algorithm/Programmers] ์ฝ๋ผ ๋ฌธ์ - 132267 (L1/JAVA) (0) | 2023.06.18 |
[Algorithm/Programmers] ๊ณผ์ผ ์ฅ์ - 135808 (L1/JAVA) (1) | 2023.06.17 |