-
[Softeer] 동계 테스트 시점 예측 (lv.3)ALGORITHM/SWEXPERT|SOFTEER 2021. 11. 2. 22:44
https://softeer.ai/practice/info.do?eventIdx=1&psProblemId=411&sw_prbl_sbms_sn=27754
2021
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798import java.util.*;import java.io.*;public class Main {public static int N, M, answer;public static int[][] arr;public static int[] dx = {-1, 0, 1, 0};public static int[] dy = {0, -1, 0, 1};public static void init() {Queue<int[]> q = new LinkedList<>();q.add(new int[]{0, 0});boolean[][] vtd = new boolean[N][M];vtd[0][0] = true;arr[0][0] = -1;while(!q.isEmpty()) {int[] tmp = q.poll();int x = tmp[0];int y = tmp[1];for(int i = 0; i < 4; i++) {int nx = x + dx[i];int ny = y + dy[i];if(nx < 0 || ny < 0 || nx >= N || ny >= M || vtd[nx][ny] || arr[nx][ny] == 1) continue;else {arr[nx][ny] = -1;vtd[nx][ny] = true;q.add(new int[]{nx, ny});}}}}public static boolean check() {for(int i = 0; i < N; i++) {for(int j = 0; j < M; j++) {if(arr[i][j] == 1) return false;}}return true;}public static void solve(int x, int y) {int count = 0;for(int i = 0; i < 4; i++) {int nx = x + dx[i];int ny = y + dy[i];if(nx < 0 || ny < 0 || nx >= N || ny >= M || arr[nx][ny] != -1) continue;else if(arr[nx][ny] == -1) {count++;}}if(count >= 2) arr[x][y] = 2;}public static void melting() {for(int i = 0; i < N; i++)for(int j = 0; j < M; j++)if(arr[i][j] == 2) arr[i][j] = -1;}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());M = Integer.parseInt(st.nextToken());arr = new int[N][M];answer = 0;for(int i = 0; i < N; i++) {st = new StringTokenizer(bf.readLine());for(int j = 0; j < M; j++) {arr[i][j] = Integer.parseInt(st.nextToken());}}while(true) {init();if(check()) break;for(int i = 0; i < N; i++) {for(int j = 0; j < M; j++) {if(arr[i][j] == 1) {solve(i, j);}}}melting();answer++;}System.out.println(answer);}}cs 'ALGORITHM > SWEXPERT|SOFTEER' 카테고리의 다른 글
[Softeer] 택배 마스터 광우 (lv.3) (0) 2021.11.02 [Softeer] 스마트 물류 (lv.3) (0) 2021.11.02 [Softeer] GBC (lv.2) (0) 2021.11.02 [Softeer] 장애물 인식 프로그램 (lv.2) (0) 2021.11.02 [Softeer] 8단 변속기 (lv.2) (0) 2021.11.02