https://www.acmicpc.net/problem/16234
16234๋ฒ: ์ธ๊ตฌ ์ด๋
N×Nํฌ๊ธฐ์ ๋ ์ด ์๊ณ , ๋ ์ 1×1๊ฐ์ ์นธ์ผ๋ก ๋๋์ด์ ธ ์๋ค. ๊ฐ๊ฐ์ ๋ ์๋ ๋๋ผ๊ฐ ํ๋์ฉ ์กด์ฌํ๋ฉฐ, rํ c์ด์ ์๋ ๋๋ผ์๋ A[r][c]๋ช ์ด ์ด๊ณ ์๋ค. ์ธ์ ํ ๋๋ผ ์ฌ์ด์๋ ๊ตญ๊ฒฝ์ ์ด ์กด์ฌํ๋ค. ๋ชจ
www.acmicpc.net
ํ์ด์์
0. ํ๋ฃจ๋ง๋ค NxNํฌ๊ธฐ์ ๋ ์ ํ์ธํ๋ฉฐ ๊ตญ๊ฒฝ์ ํ๋ฌผ์๋์ง ํ์ธ
while(true){
if(!solve()){ // ๊ตญ๊ฒฝ ํ๋ฌผ ์ ์๋์ง ํ์ธํ๊ณ ๊ตญ๊ฒฝ ํ๋ฌผ๊ณ ์ธ๊ตฌ์ ๊ฐฑ์
break;
}
ans+=1;
}
- ํ๋ฃจ๋ง๋ค (0,0)์์ bfs๋ฅผ ์ํํ๋ฉฐ ๊ตญ๊ฒฝ์ ํ๋ฌผ๊ณ ์ธ๊ตฌ๋ฅผ ์ด๋์์ผฐ๋ค.
static boolean solve(){
visited = new boolean[N][N];
int flag = 0; // ๊ตญ๊ฒฝ์ ํ๋ฌผ์๋์ง ํ์ธํ๋ flag ๋ณ์
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(!visited[i][j]){
visited[i][j] = true;
int[] result = bfs(i,j); // bfs๋ฅผ ๋๋ฉด์ ๊ตญ๊ฒฝ์ ํ๋ฌผ๊ณ (๋์์ ๊ฐฏ์, ๋์ ์ธ๊ตฌ์ ํฉ) ์ ๋ฐํ
if(result[0] > 1){ // ๋ฒฝ์ ํ๋ฌธ ์ ์ด ์์ผ๋ฉด
spread(result[1]/result[0]); // history ํ ํ์ฉํด์ ์ธ๊ตฌ์ด๋ ์ํค๊ธฐ
flag = 1;
}
}
}
}
if(flag==1) return true;
return false;
}
- ๋ณดํต์ bfsํจ์์ ๋น์ทํ์ง๋ง ๋ฐํ๊ฐ์ผ๋ก (๊ตญ๊ฒฝ ํ๋ฌธ ๋์์ ๊ฐฏ์, ํ๋ฌธ ๋์ ์ธ๊ตฌ์์ ํฉ) ์ ๋ฆฌํดํด์คฌ๋ค.
- bfsํจ์ : ๊ตญ๊ฒฝ์ ํ๋ฌผ๊ธฐ
- spreadํจ์ : ํ๋ฌผ์ด์ง ๊ตญ๊ฐ ์ธ๊ตฌ์ ๊ฐฑ์
1. BFS๋ฅผ ์ํํ์ฌ ๊ตญ๊ฒฝ์ ํ๋ฌผ๊ธฐ
static int[] bfs(int r,int c){
Deque<int[]> q = new ArrayDeque<>();
history = new ArrayDeque<>(); // bfs์ํ ํ ๋ ์ด๋ ํ๋ ๊ฒฝ๋ก๋ฅผ ์ ์ฅํ๊ณ ์๋ ํ
q.offer(new int[]{r,c});
history.offer(new int[]{r,c});
int cnt = 0;
int sum = 0;
while(!q.isEmpty()){
int[] cur = q.pollFirst();
int cr = cur[0];
int cc = cur[1];
cnt+=1;
sum += arr[cr][cc];
for(int d=0;d<4;d++){
int nr = cr + dr[d];
int nc = cc + dc[d];
if(nr<0 || nr>=N) continue;
if(nc<0 || nc>=N) continue;
if(visited[nr][nc]) continue;
int gap = Math.abs(arr[cr][cc] - arr[nr][nc]);
if(gap<L || gap>R) continue;
visited[nr][nc]=true;
q.offer(new int[]{nr,nc});
history.offer(new int[]{nr,nc}); // ํ๊ฐ ๋ฐฉ๋ฌธํ ์์น๋ค ๋ค ์ ์ฅ
}
}
return new int[]{cnt,sum};
}
- ๊ตญ๊ฒฝ์ ํ๋ฌผ๋ฉด์, ๋ฐฉ๋ฌธํ ๋์์ ๊ฐฏ์(cnt)์ ์ธ๊ตฌ์์ ํฉ(sum)์ ์ ์ฅํ๊ณ ๋ฆฌํด
- historyํ์ ๋ฐฉ๋ฌธํ ์ขํ๋ค์ ์ ์ฅ (๊ตญ๊ฒฝ์ด ํ๋ฌผ์ด์ง ๊ตญ๊ฐ ์ธ๊ตฌ์ ๊ฐฑ์ ์ ์ํด์)
2. ๊ตญ๊ฒฝ์ด ํ๋ฌผ์ด์ง ๋๋ผ๋ค์ ์ธ๊ตฌ์๋ฅผ updateํด์ฃผ๊ธฐ (historyํ ํ์ฉ)
static boolean solve(){
visited = new boolean[N][N];
int flag = 0; // ๊ตญ๊ฒฝ์ ํ๋ฌผ์๋์ง ํ์ธํ๋ flag ๋ณ์
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(!visited[i][j]){
visited[i][j] = true;
int[] result = bfs(i,j); // bfs๋ฅผ ๋๋ฉด์ ๊ตญ๊ฒฝ์ ํ๋ฌผ๊ณ (๋์์ ๊ฐฏ์, ๋์ ์ธ๊ตฌ์ ํฉ) ์ ๋ฐํ
if(result[0] > 1){ // ๋ฒฝ์ ํ๋ฌธ ์ ์ด ์์ผ๋ฉด
spread(result[1]/result[0]); // history ํ ํ์ฉํด์ ์ธ๊ตฌ์ด๋ ์ํค๊ธฐ
flag = 1;
}
}
}
}
if(flag==1) return true;
return false;
}
- result[0] ๊ฐ ์ฆ cnt๊ฐ์ ํตํด์ ๊ตญ๊ฒฝ์ ํ๋ฌผ์๋ค๋ฉด, spread ํจ์์์ ์ธ๊ตฌ์๋ฅผ ๊ฐฑ์ ํด์คฌ๋ค.
- flag๋ณ์๋ฅผ ํตํด ์ด๋ฒ ํ๋ฃจ๋์ ๊ตญ๊ฒฝ์ ํ๋ฌธ ๊ฒฝ์ฐ๊ฐ ์๋ค๋ฉด true, ์๋๋ฉด false๋ฅผ ๋ฆฌํดํ๋ค.
static void spread(int val){
while(!history.isEmpty()){
int[] a = history.pollFirst();
arr[a[0]][a[1]] = val;
}
}
- bfs ํจ์์์ ์ ์ฅํด์คฌ๋ historyํ๋ฅผ ์ด์ฉ
bfs๋ฅผ ํ ๋ฒ ์ํํ๋ ๊ฒ์ด ์๋ (0,0)๋ถํฐ ๊ณ์ ์๋กญ๊ฒ ์ํํ๋ค๋ ์ ์์ ์๋ก์ด ๋ฌธ์ ์๋ค.
๋ ์ธ๊ตฌ์ ๊ฐฑ์ ์ ์ํด historyํ์ ๋ฐฉ๋ฌธํ ์ขํ๋ฅผ ์ ์ฅํด์ค์ผ ์๊ฐ์ด๊ณผ๋ฅผ ํผํ ์ ์์๋ค.
์ ์ฒด์ฝ๋
package com.mincheolsong;
import java.io.*;
import java.util.*;
public class Main {
static int N,L,R;
static int[][] arr;
static boolean[][] visited; // bfs ์ํ ์ ์ฌ์ฉํ visited ๋ฐฐ์ด
static int[] dr = {-1,0,1,0};
static int[] dc = {0,1,0,-1};
static int ans;
static Deque<int[]> history;
static int[] bfs(int r,int c){
Deque<int[]> q = new ArrayDeque<>();
history = new ArrayDeque<>(); // bfs์ํ ํ ๋ ์ด๋ ํ๋ ๊ฒฝ๋ก๋ฅผ ์ ์ฅํ๊ณ ์๋ ํ
q.offer(new int[]{r,c});
history.offer(new int[]{r,c});
int cnt = 0;
int sum = 0;
while(!q.isEmpty()){
int[] cur = q.pollFirst();
int cr = cur[0];
int cc = cur[1];
cnt+=1;
sum += arr[cr][cc];
for(int d=0;d<4;d++){
int nr = cr + dr[d];
int nc = cc + dc[d];
if(nr<0 || nr>=N) continue;
if(nc<0 || nc>=N) continue;
if(visited[nr][nc]) continue;
int gap = Math.abs(arr[cr][cc] - arr[nr][nc]);
if(gap<L || gap>R) continue;
visited[nr][nc]=true;
q.offer(new int[]{nr,nc});
history.offer(new int[]{nr,nc}); // ํ๊ฐ ๋ฐฉ๋ฌธํ ์์น๋ค ๋ค ์ ์ฅ
}
}
return new int[]{cnt,sum};
}
static void spread(int val){
while(!history.isEmpty()){
int[] a = history.pollFirst();
arr[a[0]][a[1]] = val;
}
}
static boolean solve(){
visited = new boolean[N][N];
int flag = 0; // ๊ตญ๊ฒฝ์ ํ๋ฌผ์๋์ง ํ์ธํ๋ flag ๋ณ์
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(!visited[i][j]){
visited[i][j] = true;
int[] result = bfs(i,j); // bfs๋ฅผ ๋๋ฉด์ ๊ตญ๊ฒฝ์ ํ๋ฌผ๊ณ (๋์์ ๊ฐฏ์, ๋์ ์ธ๊ตฌ์ ํฉ) ์ ๋ฐํ
if(result[0] > 1){ // ๋ฒฝ์ ํ๋ฌธ ์ ์ด ์์ผ๋ฉด
spread(result[1]/result[0]); // history ํ ํ์ฉํด์ ์ธ๊ตฌ์ด๋ ์ํค๊ธฐ
flag = 1;
}
}
}
}
if(flag==1) return true;
return false;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
L = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
arr = new int[N][N];
ans = 0;
for(int i=0;i<N;i++){
st = new StringTokenizer(br.readLine());
for(int j=0;j<N;j++){
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
while(true){
if(!solve()){ // ๊ตญ๊ฒฝ ํ๋ฌผ ์ ์๋์ง ํ์ธํ๊ณ ๊ตญ๊ฒฝ ํ๋ฌผ๊ณ ์ธ๊ตฌ์ ๊ฐฑ์
break;
}
ans+=1;
}
System.out.println(ans);
}
}
'๐์ฝ๋ฉํ ์คํธ:CodingTest' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๊ตฌํ, ๋ฐฑํธ๋ํน (15684 ์ฌ๋ค๋ฆฌ ์กฐ์) (0) | 2024.02.16 |
---|---|
๋ฐฑํธ๋ํน(14890 ๊ฒฝ์ฌ๋ก) (2) | 2024.02.14 |
๋ ์ง์ ์ด ๋์์, ๋ฒฝ์ ๋ฟ๊ธฐ ์ ๊น์ง ์์ง์ด๋ bfs (๋ฐฑ์ค 13460 ๊ตฌ์ฌํ์ถ2) (1) | 2024.01.21 |
๋ฒจ๋ง ํฌ๋ ์๊ณ ๋ฆฌ์ฆ (0) | 2024.01.12 |
์๋ก์ ์งํฉ ์๋ฃ๊ตฌ์กฐ (Union Find) (๋ฐฑ์ค 1043๋ฒ) (0) | 2024.01.03 |