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

블로그 메뉴

  • 홈

공지사항

인기 글

태그

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

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
쵼쥬

쵼쥬의 개발공부 TIL

새로운 게임 2
코딩테스트/[백준] 코딩테스트 연습

새로운 게임 2

2022. 5. 3. 10:25


package com.company;

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

public class Main {
    static int[][] d = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
    static int[][] horse, arr;
    static int N, K, answer;
    static boolean check;
    static ArrayList<Integer>[][] list;

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

        N = Integer.parseInt(st.nextToken());
        K = Integer.parseInt(st.nextToken());
        arr = new int[N + 2][N + 2];
        horse = new int[K][3];
        list = new ArrayList[N + 1][N + 1];
        check = false;
        answer = -1;

        for (int i = 0; i < N + 1; i++) {
            for (int j = 0; j < N + 1; j++) {
                list[i][j] = new ArrayList<>();
            }
        }

        for (int[] i : arr) {
            Arrays.fill(i, 2);
        }

        for (int i = 1; i <= N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 1; j <= N; j++) {
                arr[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        for (int i = 0; i < K; i++) {
            st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());
            int direct = Integer.parseInt(st.nextToken()) - 1;
            list[x][y].add(i);
            horse[i] = new int[]{x, y, direct};
        }

        outer:
        for (int i = 1; i <= 1000; i++) {
            for (int j = 0; j < K; j++) {
                moveHorse(j);
                if (check) {
                    answer = i;
                    break outer;
                }
            }
        }
        System.out.println(answer);
    }

    static void moveHorse(int i) {
        int nextX = horse[i][0] + d[horse[i][2]][0];
        int nextY = horse[i][1] + d[horse[i][2]][1];

        if (arr[nextX][nextY] == 2) {
            nextX = nextX - 2 * d[horse[i][2]][0];
            nextY = nextY - 2 * d[horse[i][2]][1];
            horse[i][2] = horse[i][2] >= 2 ? 2 + (horse[i][2] + 1) % 2 : (horse[i][2] + 1) % 2;

            if (arr[nextX][nextY] == 2) {
                return;
            }
        }

        ArrayList<Integer> temp = findHorse(list[horse[i][0]][horse[i][1]], i, arr[nextX][nextY]);

        for (Integer index : temp) {
            horse[index][0] = nextX;
            horse[index][1] = nextY;
        }

        list[nextX][nextY].addAll(temp);
        if (list[nextX][nextY].size() >= 4) {
            check = true;
        }
    }

    static ArrayList<Integer> findHorse(ArrayList<Integer> list, int index, int color) {
        ArrayList<Integer> temp = new ArrayList<>();
        boolean check = false;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == index) {
                check = true;
            }
            if (check) {
                if (color == 1) {
                    temp.add(0, list.get(i));
                } else {
                    temp.add(list.get(i));
                }
                list.remove(i--);
            }
        }

        return temp;
    }
}

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

주사위 윷놀이 - 17825번  (0) 2022.05.05
원판돌리기 - 17822번  (0) 2022.05.03
게리맨더링 2 - 17779번  (0) 2022.04.27
연구소 3 -17142번  (0) 2022.04.25
미세먼지 안녕! - 17144번  (0) 2022.04.19
    '코딩테스트/[백준] 코딩테스트 연습' 카테고리의 다른 글
    • 주사위 윷놀이 - 17825번
    • 원판돌리기 - 17822번
    • 게리맨더링 2 - 17779번
    • 연구소 3 -17142번
    쵼쥬
    쵼쥬

    티스토리툴바