[백준/자바] 4485. 녹색 옷 입은 애가 젤다지?

2025. 2. 17. 01:30· Koala - 17기/코딩테스트 심화 스터디
목차
  1. 문제
  2. Algorithm
  3. Code

문제

 

https://www.acmicpc.net/problem/4485

 


Algorithm

- 다익스트라 알고리즘 사용했당

 


 

 

Code

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

public class Main {

	static class Node implements Comparable<Node>{
		int x;
		int y;
		int cost;
		
		public Node(int x, int y, int cost) {
			this.x = x;
			this.y = y;
			this.cost = cost;
			
		}

		@Override
		public int compareTo(Node o) {
			return this.cost - o.cost;
		}
	}
	static int n;
	static int[][] board;
	static int[][] vector = {{-1,0},{1,0},{0,-1},{0,1}};
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		String buff;
		int idx=1;
		while(!(buff = br.readLine()).equals("0")) {
			n = Integer.parseInt(buff);
			board = new int[n][n];
			
			StringTokenizer st = null;
			for(int i=0; i<n; i++) {
				st = new StringTokenizer(br.readLine());
				for(int j=0; j<n; j++) {
					board[i][j] = Integer.parseInt(st.nextToken());
				}
			}
			
			int cost = bfs();
			sb.append("Problem " + idx + ": " + cost+"\n");
			idx++;
		}
		System.out.println(sb.toString());
	}
	
	static int bfs() {
		Queue<Node> q = new PriorityQueue<>();
		int[][] move = new int[n][n];
		for(int i=0; i<n; i++) {
			Arrays.fill(move[i], Integer.MAX_VALUE);
		}
		
		q.add(new Node(0, 0, board[0][0]));
		move[0][0] = board[0][0];
		
		while(!q.isEmpty()) {
			Node node = q.poll();
			int cost = node.cost;
			
			if(node.x == n-1 && node.y == n-1) {
				return cost;
			}
			
			for(int i=0; i<4; i++) {
				int nx = node.x + vector[i][0];
				int ny = node.y + vector[i][1];
				
				if(nx<0 || nx>n-1 || ny<0 || ny>n-1) continue;
				
				if(cost+board[ny][nx] < move[ny][nx]) {
					move[ny][nx] =cost+board[ny][nx];
					q.add(new Node(nx,ny,cost+board[ny][nx]));
				}
				
			}
		}
		return -1;
	}
}

 

저작자표시 (새창열림)

'Koala - 17기 > 코딩테스트 심화 스터디' 카테고리의 다른 글

[백준/Python] 1504 특정한 최단 경로  (0) 2025.02.23
[백준/Python] 13424번 : 비밀 모임  (0) 2025.02.22
[백준/Python] 2644번 : 촌수 계산  (0) 2025.02.16
[백준/Python] 2583번 : 영역 구하기  (0) 2025.02.16
[백준/Python] 31575 도시와 비트코인  (0) 2025.02.16
  1. 문제
  2. Algorithm
  3. Code
'Koala - 17기/코딩테스트 심화 스터디' 카테고리의 다른 글
  • [백준/Python] 1504 특정한 최단 경로
  • [백준/Python] 13424번 : 비밀 모임
  • [백준/Python] 2644번 : 촌수 계산
  • [백준/Python] 2583번 : 영역 구하기
KauKoala
KauKoala
항공대 알고리즘 동아리 Koala 🥰
KauKoala
Koala
KauKoala
전체
오늘
어제
  • 분류 전체보기 (1889) N
    • 공지 게시판 (10)
    • 정보 게시판 (8)
    • Codeforce (15)
    • acm-icpc (6)
    • Koala - 1기 (16)
    • Koala - 2기 (111)
      • Programming Contest (1)
      • A반 (20)
      • B반 (39)
      • C반 (22)
      • 기초 강의 (18)
    • Koala - 3기 (10)
      • 기초 스터디 (7)
    • Koala - 4기 (67)
    • Koala - 5기 (144)
      • 기초 알고리즘 스터디 (75)
      • 코딩테스트 준비 스터디 (68)
    • Koala - 6기 (102)
      • 기초 알고리즘 스터디 (75)
      • 코딩테스트 준비 스터디 (25)
      • 모의 테스트 스터디 (1)
    • Koala - 7기 (167)
      • 기초 알고리즘 스터디 (97)
      • 코딩테스트 준비 스터디 (68)
      • 모의 테스트 스터디 (1)
    • Koala - 8기 (44)
      • 기초 알고리즘 스터디 (32)
      • 코딩테스트 준비 스터디 (10)
      • 코드포스 버츄얼 스터디 (0)
      • 프로그래머스 LV2 스터디 (0)
    • Koala - 9기 (205)
      • 기초 알고리즘 스터디 (138)
      • 코딩테스트 준비 스터디 (64)
      • 모의테스트 준비 스터디 (1)
    • Koala - 10기 (117)
      • 기초 알고리즘 스터디 (30)
      • 코딩테스트 준비 스터디 (86)
      • 모의테스트 준비 스터디 (1)
    • Koala - 11기 (151)
      • 기초 알고리즘 스터디 (46)
      • 코딩테스트 준비 스터디 (104)
      • 모의테스트 준비 스터디 (1)
    • Koala - 12기 (86)
      • 기초 알고리즘 스터디 (31)
      • 코딩테스트 준비 스터디 (55)
    • Koala - 13기 (119)
      • 기초 알고리즘 스터디 (52)
      • 코딩테스트 준비 스터디 (67)
    • Koala - 14기 (116)
      • 기초 알고리즘 스터디 (39)
      • 코딩테스트 준비 스터디 (77)
    • Koala - 15기 (138)
      • 기초 알고리즘 스터디 (73)
      • 코딩테스트 준비 스터디 (65)
    • Koala - 16기 (47)
      • 코딩테스트 기초 스터디 (16)
      • 코딩테스트 심화 스터디 (31)
    • Koala - 17기 (62)
      • 코딩테스트 기초 스터디 (15)
      • 코딩테스트 심화 스터디 (47)
    • Koala - 18기 (31)
      • 코딩테스트 기초 스터디 (11)
      • 코딩테스트 심화 스터디 (20)
    • Koala - 19기 (43) N
      • 코딩테스트 기초 스터디 (7)
      • 코딩테스트 심화 스터디 (36) N
    • Koala - 20기 (0)
      • 코딩테스트 기초 스터디 (0)
      • 코딩테스트 심화 스터디 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • 🐨항공대 알고리즘 학회 Koala 3기 모집
  • 🐨항공대 알고리즘 학회 Koala 2기 모집
  • 소모임 소개

인기 글

태그

  • BFS
  • dp
  • 파이썬
  • C++
  • BOJ
  • dfs
  • 백준
  • 백트래킹

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.3.0
KauKoala
[백준/자바] 4485. 녹색 옷 입은 애가 젤다지?
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.