[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] level.3 - ๊ฒฝ์ฃผ๋กœ ๊ฑด์„ค(C++)

2023. 5. 19. 13:03ยท๐Ÿ–ฅ๏ธ Study Note/Coding Test

https://school.programmers.co.kr/learn/courses/30/lessons/67259

๋‚ด ํ•ด๋‹ต

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

using namespace std;

enum dir
{
    start = -1,
    right,
    down,
    left,
    up
};

struct road
{
    int x;
    int y;
    int coast;
    dir d;

    road()
    {
        x = 0;
        y = 0;
        coast = 0;
        d = dir::start;
    };

    road(int _x, int _y, int _coast, dir _d)
    {
        x = _x;
        y = _y;
        coast = _coast;
        d = _d;
    };
};

int solution(vector<vector<int>> board) {
	int answer = 1'000'000;
    int coast[25][25][4]; // ๋„๋กœ ๊ฑด์„ค ๋น„์šฉ ๋ฐฉํ–ฅ๋ณ„๋กœ ์ €์žฅ
    int dir_x[4] = { 1, 0 , -1, 0 }; // ์šฐ, ํ•˜, ์ขŒ, ์ƒ
    int dir_y[4] = {0, 1, 0, -1};
    
    // ๋น„์šฉ์„ ์ตœ๋Œ€ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.
    for (int i = 0; i < 25; ++i)
        for (int j = 0; j < 25; ++j)
    	    memset(coast[i][j], 1'000'000, sizeof(int) * 4);

    // ์‹œ์ž‘์  Q์— ๋„ฃ๊ธฐ
    queue<road> q;
    for(int i= 0; i < 4; ++i)
	    coast[0][0][i] = 0;
    q.push(road());

    // Q๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๊ฒฝ๋กœ ํƒ์ƒ‰
    while (!q.empty())
    {
	    road cur = q.front();
	    q.pop();
        
        // ๋„์ฐฉ์ง€์ ์— ๋„์ฐฉํ•˜๋ฉด ์ตœ์†Œ ๋น„์šฉ ์ €์žฅ
	    if (cur.y == board.size() - 1 && cur.x == board.size() - 1)
        {
	        answer = answer < cur.coast ? answer : cur.coast;
	        continue;;
        }
        
        // 4๋ฐฉํ–ฅ ํƒ์ƒ‰
	    for (int i = 0; i < 4; ++i)
        {
            int nx = cur.x + dir_x[i];
            int ny = cur.y + dir_y[i];
            
            // ๋‹ค์Œ ์ง€์ ์ด ๋„๋กœ ๋ฒ”์œ„ ๋ฐ–์ด๋ฉด continue
            if (nx < 0 || nx >= board.size() || ny < 0 || ny >= board.size())
                continue;
            
            // ๋‹ค์Œ ์ง€์ ์ด ๋ฒฝ์— ๋ง‰ํ˜€์žˆ๋‹ค๋ฉด continue
            if (board[ny][nx] == 1)
                continue;
            
            // ๋‹ค์Œ ์ง€์  ๊ฑด์„ค์— ๋“œ๋Š” ๋น„์šฉ ๊ณ„์‚ฐ
            int c =  cur.coast + 100;
            
            // ์ฝ”๋„ˆ๋ผ๋ฉด 500์› ์ถ”๊ฐ€
            if (cur.d != dir::start)
                if (cur.d % 2 != i % 2)
                    c += 500;
	        
            // ํ˜„์žฌ ๋ฐฉํ–ฅ์—์„œ ๋„๋กœ ๊ฑด์„ค ๋น„์šฉ์ด ์ตœ์†Œ ๊ฐ’์ด๋ผ๋ฉด Q์— ์ถ”๊ฐ€
            if (coast[ny][nx][i] >= c)
            {
	            coast[ny][nx][i] = c;
	            q.push(road(nx, ny, c, static_cast<dir>(i)));
            }
        }
    }

    return answer;
}

 

๋ฐฉํ–ฅ๋งˆ๋‹ค ๊ฑด์„ค ๋น„์šฉ์„ ๋”ฐ๋กœ ์ €์žฅํ•ด์„œ ๋น„๊ตํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์ด ๊นŒ๋‹ค๋กœ์šด ๋ฌธ์ œ์˜€๋‹ค.

์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ ๋ณ€๊ฒฝ๊ธˆ์ง€ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'๐Ÿ–ฅ๏ธ Study Note > Coding Test' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] level.2 - ๋ฌด์ธ๋„ ์—ฌํ–‰(C++)  (0) 2023.05.23
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] level.2 - ๋•…๋”ฐ๋จน๊ธฐ(C++)  (0) 2023.05.22
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] level.3 - ์„ ์ž… ์„ ์ถœ ์Šค์ผ€์ค„๋ง (C++)  (0) 2023.05.18
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] level.3 - ํŒŒ๊ดด๋˜์ง€ ์•Š์€ ๊ฑด๋ฌผ(C++)  (0) 2023.05.16
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] level.3 - ๊ฐ€์žฅ ๋จผ ๋…ธ๋“œ(C++)  (0) 2023.05.15
'๐Ÿ–ฅ๏ธ Study Note/Coding Test' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] level.2 - ๋ฌด์ธ๋„ ์—ฌํ–‰(C++)
  • [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] level.2 - ๋•…๋”ฐ๋จน๊ธฐ(C++)
  • [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] level.3 - ์„ ์ž… ์„ ์ถœ ์Šค์ผ€์ค„๋ง (C++)
  • [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] level.3 - ํŒŒ๊ดด๋˜์ง€ ์•Š์€ ๊ฑด๋ฌผ(C++)
Beankong_
Beankong_
์ฃผ๋‹ˆ์–ด ํด๋ผ์ด์–ธํŠธ ํ”„๋กœ๊ทธ๋ž˜๋จธ ๊ณต๋ถ€ ๊ธฐ๋ก
  • Beankong_
    Beankong's Devlog
    Beankong_
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ์ „์ฒด ๊ธ€ (141)
      • โ›… Daily (0)
      • ๐Ÿ–ฅ๏ธ Study Note (135)
        • Unreal Engine (5)
        • Coding Test (123)
        • Design Patteren (5)
        • VCS (Git..) (1)
        • Server (1)
      • ๐Ÿงญ Devlog (6)
        • ์˜ค๋‹ต๋…ธํŠธ (2)
        • UE5 GameLift Server Test Project (1)
        • TIL (3)
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
Beankong_
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] level.3 - ๊ฒฝ์ฃผ๋กœ ๊ฑด์„ค(C++)
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”