diff --git "a/0224LJH/202508/29 BOJ \354\210\230\354\230\201\354\236\245 \353\247\214\353\223\244\352\270\260" "b/0224LJH/202508/29 BOJ \354\210\230\354\230\201\354\236\245 \353\247\214\353\223\244\352\270\260" new file mode 100644 index 00000000..e85487e2 --- /dev/null +++ "b/0224LJH/202508/29 BOJ \354\210\230\354\230\201\354\236\245 \353\247\214\353\223\244\352\270\260" @@ -0,0 +1,116 @@ +```java +import java.awt.Point; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + + +public class Main { + + static boolean[][] visited; + static int[][] arr; + static int[] dy = {-1,0,1,0}; + static int[] dx = {0,1,0,-1}; + + static Queue q = new LinkedList<>(); + static Queue temp = new LinkedList<>(); + + static int height,width,ans; + static boolean isAvailable; + + + + public static void main(String[] args) throws IOException { + + init(); + process(); + print(); + + } + + public static void init() throws IOException { + BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + height = Integer.parseInt(st.nextToken()); + width = Integer.parseInt(st.nextToken()); + ans = 0; + + arr = new int[height][width]; + visited = new boolean[height][width]; + + for (int i = 0 ; i < height; i++) { + String[] input = br.readLine().split(""); + for (int j = 0; j < width; j++) { + arr[i][j] = Integer.parseInt(input[j]); + System.out.print(arr[i][j]+ " "); + } + System.out.println(); + } + + } + + public static void process() { + for (int water = 2; water <= 9; water++) { + + visited = new boolean[height][width]; + + + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + if (visited[i][j]) continue; + if (arr[i][j] >= water) continue; + + isAvailable = true; + q.clear(); + temp.clear(); + q.add(new Point(j,i)); + temp.add(new Point(j,i)); + bfs(water); + + if (isAvailable) { + while(!temp.isEmpty()) { + Point p = temp.poll(); + + ans += water - arr[p.y][p.x]; + arr[p.y][p.x] = water; + } + } + + } + } + } + + } + + public static void bfs(int water) { + while(!q.isEmpty()) { + Point p = q.poll(); + + for (int i = 0; i < 4; i++) { + int ny = p.y + dy[i]; + int nx = p.x + dx[i]; + + + if (ny < 0 || nx < 0 || ny >= height || nx >= width) { + isAvailable = false; + continue; + } + + if (visited[ny][nx] || arr[ny][nx] >= water) continue; + + visited[ny][nx] = true; + q.add(new Point(nx,ny)); + temp.add(new Point(nx,ny)); + } + } + } + + + + + public static void print() { + System.out.println(ans); + } +} +```