쵼쥬
쵼쥬의 개발공부 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)

블로그 메뉴

  • 홈

공지사항

인기 글

태그

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

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
쵼쥬

쵼쥬의 개발공부 TIL

게리맨더링 2 - 17779번
코딩테스트/[백준] 코딩테스트 연습

게리맨더링 2 - 17779번

2022. 4. 27. 13:40


package com.company;

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

public class Main {

    static int[][] arr, check, startPoint;
    static boolean[][] visited;
    static int[][] d = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    static int N, answer;

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

        answer = Integer.MAX_VALUE;
        N = Integer.parseInt(br.readLine());
        arr = new int[N][N];
        check = new int[N][N];
        visited = new boolean[N][N];
        startPoint = new int[][]{{0, 0}, {0, N - 1}, {N - 1, 0}, {N - 1, N - 1}, {0, 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());
            }
        }

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                findD(i, j);
            }
        }

        System.out.println(answer);
    }

    static void findD(int x, int y) {
        for (int d1 = 1; d1 < N; d1++) {
            for (int d2 = 1; d2 < N; d2++) {
                if (x + d1 + d2 < N && 0 <= y - d1 && y + d2 < N) {
                    check = new int[N][N];
                    visited = new boolean[N][N];
                    startPoint[4][0] = x+1;
                    startPoint[4][1] = y;

                    divide(x, y, d1, d2);

                    int max = 0;
                    int min = Integer.MAX_VALUE;

                    for (int i = 0; i < 5; i++) {
                        int a = population(startPoint[i][0], startPoint[i][1], i + 1);
                        max = Math.max(a, max);
                        min = Math.min(a, min);
                    }
                    answer = Math.min(max - min, answer);
                }
            }
        }
    }

    static void divide(int x, int y, int d1, int d2) {
        for (int i = 0; i <= d1; i++) {
            check[x + i][y - i] = 5;
            check[x + d2 + i][y + d2 - i] = 5;
        }
        for (int i = 0; i <= d2; i++) {
            check[x + i][y + i] = 5;
            check[x + d1 + i][y - d1 + i] = 5;
        }

        int temp = 1;
        while (x - temp >= 0) {
            check[x - temp++][y] = 1;
        }

        temp = 1;
        while (y + d2 + temp < N) {
            check[x + d2][y + d2 + temp++] = 2;
        }

        temp = 1;
        while (y - d1 - temp >= 0) {
            check[x + d1][y - d1 - temp++] = 3;
        }

        temp = 1;
        while (x + d2 + d1 + temp < N) {
            check[x + d2 + d1 + temp++][y + d2 - d1] = 4;
        }
    }

    static int population(int x, int y, int num) {
        int count = 0;
        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{x, y});
        visited[x][y] = true;

        while (!q.isEmpty()) {
            int[] node = q.poll();
            count += arr[node[0]][node[1]];
            if (num != 5 && check[node[0]][node[1]] == num) {
                continue;
            }
            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
                        && !visited[nextX][nextY] && (check[nextX][nextY] == num || check[nextX][nextY] == 0)) {
                    q.add(new int[]{nextX, nextY});
                    visited[nextX][nextY] = true;
                }
            }
        }

        return count;
    }
}

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

원판돌리기 - 17822번  (0) 2022.05.03
새로운 게임 2  (0) 2022.05.03
연구소 3 -17142번  (0) 2022.04.25
미세먼지 안녕! - 17144번  (0) 2022.04.19
나무 재테크 - 16235번  (0) 2022.04.18
    '코딩테스트/[백준] 코딩테스트 연습' 카테고리의 다른 글
    • 원판돌리기 - 17822번
    • 새로운 게임 2
    • 연구소 3 -17142번
    • 미세먼지 안녕! - 17144번
    쵼쥬
    쵼쥬

    티스토리툴바