쵼쥬
쵼쥬의 개발공부 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
  • 위클리 챌린지
  • MVC
  • 백분
  • 타임리프
  • 누적합
  • querydsl
  • 인프런
  • 비트마스킹
  • 스프링
  • 자바
  • BFS
  • 알고리즘
  • 구현
  • 프로그래머스

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
쵼쥬

쵼쥬의 개발공부 TIL

경주로 건설
코딩테스트/[프로그래머스] 코딩테스트 연습

경주로 건설

2022. 3. 4. 16:54


내 코드

package com.company;

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

public class Solution {

    public int solution(int[][] board) {
        int answer = Integer.MAX_VALUE;
        int N = board.length;

        // 방향 아래 0, 오른쪽 1, 위 2, 왼쪽 3
        int[][] d = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
        int[][] arr = new int[N][N];
        boolean[][][] visited = new boolean[N][N][4];

        for (int i = 0; i < N; i++) {
            Arrays.fill(arr[i], Integer.MAX_VALUE);
        }

        PriorityQueue<Node> pq = new PriorityQueue<Node>((o1, o2) -> o1.cost - o2.cost);

        if (board[0][1] != 1) {
            pq.add(new Node(0, 1, 1, 100));
            arr[0][1] = 100;
        }
        if (board[1][0] != 1) {
            pq.add(new Node(1, 0, 0, 100));
            arr[1][0] = 100;
        }

        while (!pq.isEmpty()) {
            Node node = pq.poll();
            if (board[node.x][node.y] == 1)
                continue;

            if (node.x == N - 1 && node.y == N - 1) {
                answer = node.cost;
                break;
            }

            for (int i = 0; i < 4; i++) {
                int x = node.x + d[i][0];
                int y = node.y + d[i][1];
                int cost = node.cost + (node.direct == i ? 100 : 600);
                if (x >= 0 && x < N && y >= 0 && y < N && (arr[x][y] >= cost || !visited[x][y][i])) {
                    pq.add(new Node(x, y, i, cost));
                    arr[x][y] = cost;
                    visited[x][y][i] = true;
                }
            }
        }

        return answer;
    }

    static class Node {
        int x, y, direct, cost;

        public Node(int x, int y, int direct, int cost) {
            this.x = x;
            this.y = y;
            this.direct = direct;
            this.cost = cost;
        }
    }
}

'코딩테스트 > [프로그래머스] 코딩테스트 연습' 카테고리의 다른 글

미로탈출  (0) 2022.05.06
보석 쇼핑  (0) 2022.03.04
2 x n 타일링  (0) 2021.12.29
124 나라의 숫자  (0) 2021.12.28
구명보트  (0) 2021.11.23
    '코딩테스트/[프로그래머스] 코딩테스트 연습' 카테고리의 다른 글
    • 미로탈출
    • 보석 쇼핑
    • 2 x n 타일링
    • 124 나라의 숫자
    쵼쥬
    쵼쥬

    티스토리툴바