[백준] 4485 녹색 옷 입은 애가 젤다지? (C++)

2022. 5. 28. 19:24·Algorithm/Solution
728x90

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

 

4485번: 녹색 옷 입은 애가 젤다지?

젤다의 전설 게임에서 화폐의 단위는 루피(rupee)다. 그런데 간혹 '도둑루피'라 불리는 검정색 루피도 존재하는데, 이걸 획득하면 오히려 소지한 루피가 감소하게 된다! 젤다의 전설 시리즈의 주

www.acmicpc.net


난이도 : 골드 4

 

풀이과정 :

다익스트라를 이용한 문제

2차원 배열에서 다익스트라 알고리즘을 활용하면 되는 문제였다.

도둑루피 값은 2차원 배열에 저장하고, 

우선순위 큐는 다중 페어를 이용하여 pair<int , pair<int,int>> (pair<가중치, pair<x,y>> 형태로 넣어주도록 했다.

가중치 dist 배열은 2차원 벡터 형태로 선언하였다.

다익스트라를 안다면 2차원 형태를 어떻게 구현하느냐가 주요 문제였음

 

코드 :

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#define INF 99999999
using namespace std;
int n;
int v[130][130];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

int main()
{
    int cnt = 1;
    while(true){
        cin >> n;
        if(n==0) return 0;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cin >> v[i][j];
            }
        }
        
        priority_queue<pair<int, pair<int,int>>> pq;
        vector <vector <int>> dist(n+1,vector <int>(n+1, INF));
        dist[0][0] = v[0][0];
        pq.push(make_pair(-v[0][0],make_pair(0,0)));
        
        while(!pq.empty()){
            pair<int,pair<int,int>> p = pq.top();
            int cost = -p.first;
            pair<int,int> here = p.second;
            
            pq.pop();
            
            if(cost > dist[here.first][here.second]) continue;
            
            for(int i=0;i<4;i++){
                int nx = here.first + dx[i];
                int ny = here.second + dy[i];
                if(nx>=0 && ny>=0 && nx<n && ny<n){
                    pair<int,int> next = make_pair(nx,ny);
                    int nextcost = v[nx][ny];
                    if (dist[next.first][next.second] > nextcost + cost ){
                        dist[next.first][next.second] = nextcost + cost;
                        pq.push(make_pair(-dist[next.first][next.second], make_pair(next.first, next.second)));
                    }
                }
            }
        }   
        cout << "Problem " << cnt <<": " << dist[n-1][n-1] << "\n";
        cnt++;    
        for(int i=0;i<130;i++){
            memset(v[i], 0, sizeof(int)*130);
        }
    }
}

 

728x90
저작자표시 비영리 (새창열림)

'Algorithm > Solution' 카테고리의 다른 글

[백준] 2110 공유기 설치(C++)  (0) 2022.06.08
[백준] 14500번 테트로미노(C++)  (0) 2022.06.02
[백준] 11779 최소 비용 구하기2(C++)  (0) 2022.05.11
[백준] 1005번 ACM Craft(C++)  (0) 2022.05.10
[백준] 2589번 보물섬 (C++)  (0) 2022.05.08
'Algorithm/Solution' 카테고리의 다른 글
  • [백준] 2110 공유기 설치(C++)
  • [백준] 14500번 테트로미노(C++)
  • [백준] 11779 최소 비용 구하기2(C++)
  • [백준] 1005번 ACM Craft(C++)
BeNI
BeNI
코딩하는 블로그
  • BeNI
    코딩못하는컴공
    BeNI
  • 전체
    오늘
    어제
    • Menu (253)
      • My profile (1)
      • 회고 | 후기 (8)
      • Frontend (65)
        • Article (11)
        • Study (35)
        • 프로그래머스 FE 데브코스 (19)
      • Backend (0)
      • Algorithm (58)
        • Solution (46)
        • Study (12)
      • Major (111)
        • C&C++ (23)
        • Java (20)
        • Data Structure (14)
        • Computer Network (12)
        • Database (15)
        • Linux (6)
        • Architecture (3)
        • Lisp (15)
        • OS (1)
        • Security (2)
      • etc (2)
  • 링크

    • 깃허브
    • 방명록
  • 인기 글

  • 최근 댓글

  • 최근 글

  • 태그

    react
    백준
    lisp
    Algorithm
    프로그래머스
    리팩토링
    데브코스
    자료구조
    파일처리
    C++
  • hELLO· Designed By정상우.v4.10.2
BeNI
[백준] 4485 녹색 옷 입은 애가 젤다지? (C++)
상단으로

티스토리툴바