๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ฒฝ์ฃผ๋กœ ๊ฑด์„ค 2020 ์นด์นด์˜ค ์ธํ„ด์‰ฝ level3

by Jouureee 2021. 5. 7.

๋ฌธ์ œ :

programmers.co.kr/learn/courses/30/lessons/67259

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ฒฝ์ฃผ๋กœ ๊ฑด์„ค

[[0,0,0,0,0,0,0,1],[0,0,0,0,0,0,0,0],[0,0,0,0,0,1,0,0],[0,0,0,0,1,0,0,0],[0,0,0,1,0,0,0,1],[0,0,1,0,0,0,1,0],[0,1,0,0,0,1,0,0],[1,0,0,0,0,0,0,0]] 3800 [[0,0,1,0],[0,0,0,0],[0,1,0,1],[1,0,0,0]] 2100 [[0,0,0,0,0,0],[0,1,1,1,1,0],[0,0,1,0,0,0],[1,0,0,1,0,1],[

programmers.co.kr

 

๋ถ„์„ :

์ด ๋ฌธ์ œ๋Š” ๋ฐฑ์ค€์—์„œ ํ’€์—ˆ๋˜ ๋‚ด๋ฆฌ๋ง‰๊ธธ ๋ฌธ์ œ์™€ ๋น„์Šทํ•˜๊ฒŒ dp + dfs์˜ ์กฐํ•ฉ๋ฌธ์ œ์˜€๋‹ค.

์šฐ์„  ๋ฏธ๋ฆฌ ๊ณ ํ•˜์ง€๋งŒ TC 24๋ฒˆ๋งŒ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ๋‹ค ใ…œ

 

1520๋ฒˆ: ๋‚ด๋ฆฌ๋ง‰ ๊ธธ

์—ฌํ–‰์„ ๋– ๋‚œ ์„ธ์ค€์ด๋Š” ์ง€๋„๋ฅผ ํ•˜๋‚˜ ๊ตฌํ•˜์˜€๋‹ค. ์ด ์ง€๋„๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ง์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ด๋ฉฐ ์—ฌ๋Ÿฌ ์นธ์œผ๋กœ ๋‚˜๋‰˜์–ด์ ธ ์žˆ๋‹ค. ํ•œ ์นธ์€ ํ•œ ์ง€์ ์„ ๋‚˜ํƒ€๋‚ด๋Š”๋ฐ ๊ฐ ์นธ์—๋Š” ๊ทธ ์ง€์ ์˜ ๋†’์ด๊ฐ€ ์“ฐ์—ฌ ์žˆ์œผ

www.acmicpc.net

๋‹ค๋งŒ ์ƒํ•˜, ์ขŒ์šฐ๋ฅผ ์ด๋™ํ• ๋•Œ ๋น„์šฉ์ด ๋‹ฌ๋ฆฌ ๋“ค์—ˆ๋Š”๋ฐ ์ด ์ ์—์„œ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•˜๋ฉด ์ข‹์„์ง€ ๋ชฐ๋ผ ํ•˜๋“œ์ฝ”๋”ฉ ํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์„ ์ฉ”์ฉ”๋งธ๋‹ค.

๊ทธ๋Ÿฌ๋˜ ์ค‘ ๋‹ค๋ฅธ ๋ถ„์˜ ์•„์ด๋””์–ด๋ฅผ ์ฐธ๊ณ ํ•ด ์•„ ์ด๋ ‡๊ฒŒ ์ ‘๊ทผํ•˜๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์ด ..! ๋ฅผ ๋ฐœ๊ฒฌํ–ˆ๋‹ค.

 

1. ๊ฐ’ ๋น„์šฉ ๊ณ„์‚ฐํ•˜๊ธฐ(์ง์„  ๋„๋กœ, ์ฝ”๋„ˆ ๋„๋กœ ?)

์ง์„  ๋„๋กœ, ์ฝ”๋„ˆ ๋„๋กœ ๋น„์šฉ 

์ƒํ•˜์ขŒ์šฐ๋ฅผ ๋Œ๊ธฐ ์œ„ํ•ด bfs์‹œ

int dx[] = { 0, 0, 1, -1 }; //up down left right
int dy[] = { 1, -1, 0, 0 };

์˜ ์ธ๋ฑ์Šค๋ฅผ ์ฐธ๊ณ ํ•ด ๋Œ ๊ฒƒ์ด๋‹ค. 

 

์ธ๋ฑ์Šค i๊ฐ€ 

 

์ƒํ•˜์ผ๋• 0, 1

์ขŒ์šฐ์ผ๋• 3, 2

 

์ฝ”๋„ˆ ๋„๋กœ๋Š” ์ƒํ•˜ -> ์ขŒ์šฐ ๋˜๋Š” ์ขŒ์šฐ -> ์ƒํ•˜๋กœ ์ฆ‰ ์ธ๋ฑ์Šค๊ฐ€ 0, 1 -> 2, 3์œผ๋กœ ๋˜๋Š” 2, 3 -> 0, 1๋กœ ๋ณ€ํ™”ํ• ๋•Œ ๋Œ๊ณ  + ์ง์„  ๋„๋กœ = 600 ์›์˜ ๋น„์šฉ์ด ๋Œ ๊ฒƒ์ด๋‹ค. 

 ์ฒ˜์Œ bfs๋ฅผ ๋Œ ์‹œ ๋น„์šฉ์€ -1๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ฃผ์—ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  before๊ณผ now ์ธ๋ฑ์Šค์˜ ๊ฐ’ ๋ณ€ํ™”๋ฅผ ๋น„๊ตํ•ด ์ ์ ˆํ•œ ๋น„์šฉ์„ ๊ณ„์‚ฐํ•ด ์ค€๋‹ค ..! 

 

2. DP ์ตœ์†Œ ๋น„์šฉ 

๊ณ„์‚ฐ๋œ ๋น„์šฉ์— ๋Œ€ํ•ด

dp [x][y]: (x, y)๊นŒ์ง€ ๋„๋กœ ๊ฑด์„คํ•˜๋Š”๋ฐ ๋“œ๋Š” ์ตœ์†Œ ๋น„์šฉ

์„ ์ตœ์†Œ ๋น„์šฉ์ด ์žˆ์œผ๋ฉด ๊ฐฑ์‹ ์‹œ์ผœ์ฃผ๊ณ  ๋‹ค์Œ ๋ฐฉ๋ฌธ์„ ์žฌ๊ท€๋กœ ์ด๋™ํ•œ๋‹ค.

 

์ด๋•Œ ๋˜ ์ฃผ์˜ํ•ด์•ผ ํ• ์ ์€ visit๋ฅผ ํ†ตํ•ด ๋ฐฉ๋ฌธํ•œ ์žฅ์†Œ๋Š” ๊ฐ€์ง€ ์•Š๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ธ๋ฐ bfs๊ฐ€ ๋๋‚˜๋ฉด ๋Œ์•„์˜ค๋Š” ๊ณผ์ •์—์„œ visit๋ฅผ false๋กœ ๋งŒ๋“ค์–ด 

 ๋‹ค์Œ for๋ฌธ์„ ๋Œ๊ณ  ๊ทธ์— ๋Œ€ํ•œ ๋” ์ตœ์ ์˜ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด dp์— ์ตœ์†Œ์˜ ๊ฐ’์œผ๋กœ ๋‹ค์‹œ ๊ณ„์‚ฐ๋˜์–ด ์ €์žฅ๋˜๊ฒŒ ํ•˜๋Š” ์ ์„ ๊ฐ„๊ณผํ•ด์„  ์•ˆ๋œ๋‹ค.

 

 

c++ ์ฝ”๋“œ :

//
//  [KAKAO] 2020_trackBuild.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/05/07.
//

#include <stdio.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <queue>

using namespace std;
int nextPay = 0;

//
//vector<vector<int>> board = {{0,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,0},{0,0,0,0,0,1,0,0},{0,0,0,0,1,0,0,0},{0,0,0,1,0,0,0,1},{0,0,1,0,0,0,1,0},{0,1,0,0,0,1,0,0},{1,0,0,0,0,0,0,0}};
vector<vector<int>> board = {{0,0,1},{1,0,1},{1,0,0}};
int size = (int) board.size();
bool visit[26][26];
int dp[26][26];
int answer = 0;

int dx[] = { 0, 0, 1, -1 }; //up down left right
int dy[] = { 1, -1, 0, 0 };

//dp ๋ฐฐ์—ด ํ™•์ธ์šฉ
void print_dp(){
    for(int i =0; i < board.size(); i++){
        for(int j = 0; j < board.size(); j++){
            cout << dp[i][j] << " ";
        }
        cout << '\n';
    }
    cout << '\n';
}

int calculate_pay(int before, int now){
    if(before == -1){
        return 100;
    }else if((before == 2|| before == 3) &&(now == 0|| now == 1)){
        return 600;
    }else if((before == 0|| before == 1) &&(now == 2|| now == 3)){
        return 600;
    }
    return 100;
}

void make_bfs(int x, int y, int dir, int pay){
    for(int i = 0; i < 4 ; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];
        if(nx < 0 || ny < 0|| nx >= size || ny >= size) continue;
        if(board[nx][ny] == 1 || visit[nx][ny] == true) continue;
        
        pay = calculate_pay(dir, i);
        if(dp[nx][ny] < dp[x][y] + pay){
            continue;
        }
        dp[nx][ny] = dp[x][y] + pay;
        visit[nx][ny] = true;
        make_bfs(nx, ny, i, pay);
        //print_dp();
        visit[nx][ny] = false;
        // false๊ฐ€ ๋˜๊ณ  ๋Œ์•„์˜ค๋Š” ๊ณผ์ •์—์„œ ๋‹ค์Œ for๋ฌธ์„ ๋Œ๊ณ  ๊ทธ์— ๋Œ€ํ•œ ๋” ์ตœ์ ์˜ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด dp์— ์ตœ์†Œ์˜ ๊ฐ’์œผ๋กœ ๋‹ค์‹œ ๊ณ„์‚ฐ๋˜์–ด ์ €์žฅ๋œ๋‹ค.
        
    }
}


int main(){
    
    // ์ดˆ๊ธฐํ™”
    memset(visit, false, sizeof(visit));
    for(int i =0; i < board.size(); i++){
        for(int j = 0; j < board.size(); j++){
            dp[i][j] = 10000000;
        }
    }
    
    dp[0][0] = 0;
    make_bfs(0, 0, -1, 0);
    
    cout << dp[size -1][size -1] << '\n';
    
   
    return 0;
}

 

๊ฒฐ๊ณผ :

๋ญ˜ ๋นผ๋จน์€ ๊ฒƒ์ผ๊นŒ๋‚˜ .. ์•„์ง์€ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค ใ…  

์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค .. ๐Ÿฅบ๐Ÿฅบ

๋Œ“๊ธ€