쵼쥬
쵼쥬의 개발공부 TIL
쵼쥬
전체 방문자
오늘
어제
  • 분류 전체보기 (276)
    • 코딩테스트 (192)
      • [알고리즘] 알고리즘 정리 (7)
      • [백준] 코딩테스트 연습 (126)
      • [프로그래머스] 코딩테스트 연습 (59)
    • Spring (71)
      • [인프런] 스프링 핵심 원리- 기본편 (9)
      • [인프런] 스프링 MVC 1 (6)
      • [인프런] 스프링 MVC 2 (4)
      • [인프런] 실전! 스프링 부트와 JPA 활용1 (7)
      • [인프런] 실전! 스프링 부트와 JPA 활용2 (5)
      • [인프런] 실전! 스프링 데이터 JPA (7)
      • [인프런] 실전! Querydsl (7)
      • JWT (5)
      • [인프런] Spring Cloud (17)
      • [인프런] Spring Batch (4)
    • Java (6)
      • [Java8] 모던인자바액션 (4)
      • [부스트코스] 웹 백엔드 (2)
      • [패스트캠퍼스] JAVA STREAM (0)
    • CS (6)
      • 디자인 패턴과 프로그래밍 패터다임 (2)
      • 네트워크 (4)

블로그 메뉴

  • 홈

공지사항

인기 글

태그

  • 부스트코스
  • 코딩테스트
  • 프로그래머스
  • 자바
  • 인프런
  • spring
  • 백준
  • 누적합
  • 백분
  • 비트마스킹
  • jpa
  • Spring Data JPA
  • 타임리프
  • 스프링
  • 위클리 챌린지
  • querydsl
  • 알고리즘
  • BFS
  • MVC
  • 구현

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
쵼쥬

쵼쥬의 개발공부 TIL

연구소 3 -17142번
코딩테스트/[백준] 코딩테스트 연습

연구소 3 -17142번

2022. 4. 25. 16:33


package com.company;

import java.io.*;
import java.util.*;

public class Main {
    static ArrayList<int[]> virus;
    static int[][] arr;
    static int[][] d = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    static int N, M, count, answer;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        virus = new ArrayList<>();

        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        count = 0;
        answer = 100000;

        arr = new int[N][N];
        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()) - 2;
                if (arr[i][j] == 0) {
                    virus.add(new int[]{i, j, 1});
                }
                if (arr[i][j] == -2) {
                    count++;
                }
            }
        }

        com(0, new ArrayList<>());

        System.out.println(answer == 100000 ? -1 : answer);
    }

    static void com(int index, ArrayList<int[]> list) {

        if (list.size() == M) {

            bfs(list);
            return;
        }

        for (int i = index; i < virus.size(); i++) {
            list.add(virus.get(i));
            com(i + 1, list);
            list.remove(list.size() - 1);
        }
    }


    static void bfs(ArrayList<int[]> list) {
        int[][] temp = new int[N][N];
        PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> o1[2] - o2[2]);
        pq.addAll(list);

        for (int i = 0; i < N; i++) {
            temp[i] = arr[i].clone();
        }

        for (int[] i : list) {
            temp[i[0]][i[1]] = 1;
        }


        int cnt = 0;
        int time = 0;

        while (!pq.isEmpty()) {
            if (cnt == count) {
                break;
            }

            int[] node = pq.poll();
            time = Math.max(temp[node[0]][node[1]], time);

            for (int[] i : d) {
                int nextX = node[0] + i[0];
                int nextY = node[1] + i[1];

                if (nextX >= 0 && nextY >= 0 && nextX < N && nextY < N
                        && (temp[nextX][nextY] == -2 || temp[nextX][nextY] == 0)) {
                    pq.add(new int[]{nextX, nextY, node[2] + 1});
                    if (temp[nextX][nextY] == -2) {
                        cnt++;
                    }
                    temp[nextX][nextY] = temp[node[0]][node[1]] + 1;
                }
            }
        }

        if (cnt == count) {
            answer = Math.min(time, answer);
        }
    }
}

'코딩테스트 > [백준] 코딩테스트 연습' 카테고리의 다른 글

새로운 게임 2  (0) 2022.05.03
게리맨더링 2 - 17779번  (0) 2022.04.27
미세먼지 안녕! - 17144번  (0) 2022.04.19
나무 재테크 - 16235번  (0) 2022.04.18
빚 - 10427번  (0) 2022.04.16
    '코딩테스트/[백준] 코딩테스트 연습' 카테고리의 다른 글
    • 새로운 게임 2
    • 게리맨더링 2 - 17779번
    • 미세먼지 안녕! - 17144번
    • 나무 재테크 - 16235번
    쵼쥬
    쵼쥬

    티스토리툴바