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

블로그 메뉴

  • 홈

공지사항

인기 글

태그

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

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
쵼쥬

쵼쥬의 개발공부 TIL

뱀 - 3190번
코딩테스트/[백준] 코딩테스트 연습

뱀 - 3190번

2021. 11. 2. 15:16


풀이 방법

문제에 나온대로 구현을 통해 풀이했다. 

board 판을 (N + 2) * (N + 2) 크기로 만들어서 벽을 생성해 주었고

way 배열에는 오른쪽, 아래, 왼쪽, 위 방향 순서대로 경로를 넣어주었고

map에 방향 전환 정보를 넣고 Queue로 뱀이 현재 위치한 정보를 담아주었다.

tail 함수는 뱀 머리가 뱀 몸통에 닿는지 확인하는 함수이다.

 

내 코드

package com.company;

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

public class Main {
    static int N, K, L;
    static int[][] board;
    static int[][] way = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    static HashMap<Integer, String> map = new HashMap<>();
    static Queue<int[]> q = new LinkedList<>();

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

        N = Integer.parseInt(br.readLine());
        K = Integer.parseInt(br.readLine());
        board = new int[N + 2][N + 2];

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

        L = Integer.parseInt(br.readLine());

        for (int i = 0; i < L; i++) {
            st = new StringTokenizer(br.readLine());
            map.put(Integer.parseInt(st.nextToken()), st.nextToken());
        }
        int[] current = {1, 1};
        int direction = 0;
        int sec = 0;

        q.add(current);

        while (true) {
            sec++;
            String s = map.getOrDefault(sec, "not exist");

            int[] next = {current[0] + way[direction][0], current[1] + way[direction][1]};

            if (board[next[0]][next[1]] == 0 || tail(next)) {
                break;
            } else if (board[next[0]][next[1]] == 1) {
                q.poll();
            } else if (board[next[0]][next[1]] == 2) {
                board[next[0]][next[1]] = 1;
            }

            q.add(next);

            current[0] = next[0];
            current[1] = next[1];

            if (s.equals("D")) {
                direction = (direction + 1) % 4;
            } else if (s.equals("L")) {
                direction = (direction + 3) % 4;
            }
        }

        System.out.println(sec);
    }

    static boolean tail(int[] next) {
        for (int[] array : q) {
            if (array[0] == next[0] && array[1] == next[1])
                return true;
        }
        return false;
    }
}

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

십자가 2개 놓기 - 17085번  (0) 2021.11.05
Coins - 3067번  (0) 2021.11.04
A -> B - 16953번  (0) 2021.11.02
특정한 최단 경로 - 1504번  (0) 2021.11.01
독서실 거리두기 - 20665번  (0) 2021.10.27
    '코딩테스트/[백준] 코딩테스트 연습' 카테고리의 다른 글
    • 십자가 2개 놓기 - 17085번
    • Coins - 3067번
    • A -> B - 16953번
    • 특정한 최단 경로 - 1504번
    쵼쥬
    쵼쥬

    티스토리툴바