๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][Programmers][BFS/DFS] ์•„์ดํ…œ ์ค๊ธฐ

ibelieveinme 2023. 6. 6. 09:57
728x90

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr


*๋ฌธ์ œ ์„ค๋ช…

์ง์‚ฌ๊ฐํ˜•๋“ค์ด x,y ์ขŒํ‘œ์— ๋†“์—ฌ ์žˆ์„ ๋•Œ, ์บ๋ฆญํ„ฐ๊ฐ€ ๊ฐ€์žฅ์ž๋ฆฌ๋งŒ ์ด๋™ํ•˜๋ฉด์„œ ์•„์ดํ…œ์ด ์žˆ๋Š” ์œ„์น˜๊นŒ์ง€ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์•Œ์•„๋‚ด๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋‘ ์‚ฌ๊ฐํ˜•์ด ๊ผญ์ง€์ ์—์„œ ๋งŒ๋‚˜๋‚˜๊ฑฐ๋‚˜ ๋ณ€์ด ๊ฒน์น˜๊ฑฐ๋‚˜ ์•„์–˜ ๋ถ„๋ฆฌ๋˜๊ฑฐ๋‚˜ ์™„์ „ํžˆ ํฌํ•จ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

 

*๋ฌธ์ œ์˜ˆ์‹œ

๋ฌธ์ œ ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

๋ฌธ์ œ ์ž…๋ ฅ์€ rectangle์˜ ์‹œ์ž‘/๋ x,y ์ขŒํ‘œ๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด๊ณผ ์บ๋ฆญํ„ฐ์˜ x,y์ขŒํ‘œ, ์•„์ดํ…œ์˜ x,y ์ขŒํ‘œ ๊ฐ’์ด ์ฃผ์–ด์ง„๋‹ค.

๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด ๋œ๋‹ค. 

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ 1,2,3 ๋ฒˆ ๋‹ต

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ์˜ 1,2,3 ๋ฒˆ ๋‹ต์€ ์œ„์™€ ๊ฐ™๋‹ค. ๋นจ๊ฐ„์ƒ‰์ด ์บ๋ฆญํ„ฐ ์œ„์น˜, ํŒŒ๋ž€์ƒ‰์ด ์•„์ดํ…œ ์œ„์น˜์ด๋‹ค. ๋นจ๊ฐ„์ƒ‰ ์บ๋ฆญํ„ฐ์ง€์ ์—์„œ ํŒŒ๋ž€์ƒ‰ ์•„์ดํ…œ ์ง€์ ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋Š” ์™ผ์ชฝ์œผ๋กœ ๊ฐ€๋˜, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฐ€๋˜ ๊ฐ€์žฅ ์งง์€ ํ•œ๊ฐ€์ง€ ๊ฒฝ์šฐ์ด๋‹ค.

 

*ํ’€์ด๋ฐฉ๋ฒ•

1) ์šฐ์„  board ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์„œ ์ฃผ์–ด์ง„ ์ง์‚ฌ๊ฐํ˜•๋“ค์„ board์— ๋†“๋Š” ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค. board์— ๋†“์—ฌ์žˆ๋‹ค๊ณ  ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด 1์„ ๋„ฃ์–ด์ค€๋‹ค.

2) ๋ชจ๋“  ์ง์‚ฌ๊ฐํ˜•์„ ๋†“์•˜์œผ๋ฉด ์ง์‚ฌ๊ฐํ˜• ๋‚ด๋ถ€๋ฅผ 0์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

3) ์บ๋ฆญํ„ฐ ์œ„์น˜์—์„œ ์‹œ์ž‘ํ•ด์„œ ์•„์ดํ…œ ์œ„์น˜์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ BFS ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•œ๋‹ค.

 

1) 2) 3) ๋ฐฉ๋ฒ•์„ ๋จธ๋ฆฌ์†์œผ๋กœ๋Š” ๋˜๋‚˜ ์ฝ”๋“œ๋กœ ์˜ฎ๊ธฐ๊ธฐ ์‰ฝ์ง€ ์•Š์•˜๋‹ค. ์™€์ค‘์— ๊ฒ€์ƒ‰ํ•ด์„œ ์ฐพ์•„๋ณด๋‹ˆ ๋ชจ๋“  ์ขŒํ‘œ๊ฐ’๋“ค์„ 2๋ฐฐ์ฒ˜๋ฆฌํ•˜์—ฌ ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์„ ํ•ด๊ฒฐํ•ด์•ผ ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

 

์†์œผ๋กœ ๊ทธ๋ ค๋ณด๋‹ˆ ์ดํ•ด๊ฐ€ ๊ฐ”๋‹ค. ๋™๊ทธ๋ผ๋ฏธ ์นœ ๋ถ€๋ถ„์ด ์œ„์•„๋ž˜ ๊ฐ™์€ ๋ถ€๋ถ„์ธ๋ฐ ์œ„ ์ขŒํ‘œ๊ณ„์™€ ๋‹ฌ๋ฆฌ ์™„์ „ํžˆ ๊ฒน์ณ์„œ ์ •ํ™•ํ•œ ํƒ์ƒ‰์ด ๋ถˆ๊ฐ€ ํ–ˆ๋‹ค. ๋˜, x,y ์ขŒํ‘œ๊ณ„๋ฅผ board์— ํ–‰์—ด๋กœ ํ‘œํ˜„ํ•˜๋‹ˆ x,y ์ขŒํ‘œ๊ฐ€ ๋ฐ˜๋Œ€๋ผ์„œ  x,y ๊ฐ’์„ ๋ฐ˜๋Œ€๋กœ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

 

*์•Œ์•„๋‘˜๊ฒƒ

1) queue์˜ emplace ํ•จ์ˆ˜

- C++ 11์—์„œ ์ฒ˜์Œ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

- queue๋‚˜ map์— ๊ฐ’์„ ๋„ฃ์„ ๋•Œ make_pair๋‚˜ make_tuple์„ ์ด์šฉํ•˜์ง€ ์•Š๊ณ  emplace ํ•จ์ˆ˜๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

- push์™€ ๋‹ฌ๋ฆฌ ๋ฐ”๋กœ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋ฉด์„œ ๋„ฃ๊ธฐ์— copy & move ์—ฐ์‚ฐ์„ ๋”ฐ๋กœ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์•„ ์—ฐ์‚ฐ๋น„์šฉ์ด ์ ๋‹ค๊ณ  ํ•œ๋‹ค.

 

*์ฝ”๋“œ

#include <string>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;

int BFS(vector<vector<int>> board, int characterX, int characterY, int itemX, int itemY){    
    int direction[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};
    queue<pair<int,int>> q;
    
    q.emplace(characterY, characterX);
    while(!q.empty()){
        int y = q.front().first;
        int x = q.front().second;
        q.pop();
        
        if(y == itemY && x == itemX) {
            break;
        }
        for(int i = 0; i < 4; i++){
            int nextY = y + direction[i][1];
            int nextX = x + direction[i][0];
            
            //์ขŒํ‘œ ๋ฒ”์œ„์ฒดํฌ
            if(nextX < 0 || nextX >= board.size() || nextY < 0 || nextY >= board[0].size()) continue;            
            //๋ฐฉ๋ฌธ์•ˆํ–ˆ์œผ๋ฉด
            if(board[nextY][nextX] == 1){
                q.emplace(nextY, nextX);
                //1์ถ”๊ฐ€ํ•ด์„œ ๋ฐฉ๋ฌธ์ฒดํฌ๊ฒธ ์ด๋™๊ฑฐ๋ฆฌ ์ €์žฅ
                board[nextY][nextX] = board[y][x] + 1;
            }
        }
    }
    return board[itemY][itemX] / 2;
}

int solution(vector<vector<int>> rectangle, int characterX, int characterY, int itemX, int itemY) {
    // ์ตœ์†Œ ๊ฑฐ๋ฆฌ
    int answer = 0;
    // ์บ๋ฆญํ„ฐ์™€ ์•„์ดํ…œ์ขŒํ‘œ ๊ฐ’ 2๋ฐฐ๋กœ ๋Š˜๋ฆฌ๊ธฐ
    characterX *= 2, characterY *= 2, itemX *= 2, itemY *= 2;
    // 101*101 ํฌ๊ธฐ์˜ ๋ณด๋“œ ์ƒ์„ฑ
    vector<vector<int>> board(101, vector<int>(101));
    // ์ง์‚ฌ๊ฐํ˜•์˜ ์‹œ์ž‘๊ณผ ๋ ์ขŒํ‘œ
    int x1, y1, x2, y2;
    
    // ์ง์‚ฌ๊ฐํ˜•๋“ค์„ board ๋ฐฐ์—ด์— ๋†“๊ธฐ
    for(int i = 0; i < rectangle.size(); i++){
        for(int j = 0; j < rectangle[i].size(); j++){
            rectangle[i][j] = rectangle[i][j] * 2;
        }
        x1 = rectangle[i][0], y1 = rectangle[i][1];
        x2 = rectangle[i][2], y2 = rectangle[i][3];
        
        // ์ขŒํ‘œ๊ฐ’์ด๋ผ ํ–‰์—ด๋กœ ๋”ฐ์ง€๋ฉด x๊ฐ€ ์—ด์ด๊ณ  y๊ฐ€ ํ–‰์ด๋ผ์„œ ๋ฐ˜๋Œ€๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.
        for(int y = y1; y <= y2; y++){
            for(int x = x1; x <= x2; x++){
                board[y][x] = 1;
            }
        }
    }
    
    // ์ง์‚ฌ๊ฐํ˜• ๋‚ด๋ถ€๋ฅผ 0์œผ๋กœ ์ฑ„์šฐ๊ธฐ
    for(int i = 0; i < rectangle.size(); i++){
        x1 = rectangle[i][0], y1 = rectangle[i][1];
        x2 = rectangle[i][2], y2 = rectangle[i][3];
        
        for(int y = y1 + 1; y < y2; y++){
            for(int x = x1 + 1; x < x2; x++){
                board[y][x] = 0;
            }
        }
    }
    
    // BFS๋กœ ์บ๋ฆญํ„ฐ ์ขŒํ‘œ๋ถ€ํ„ฐ ์•„์ดํ…œ ์ขŒํ‘œ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ ํƒ์ƒ‰
    answer = BFS(board, characterX, characterY, itemX, itemY);
    
    return answer;
}
728x90