๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][Programmers][DFS/BFS] ๊ฒŒ์ž„ ๋งต ์ตœ๋‹จ๊ฑฐ๋ฆฌ

ibelieveinme 2023. 5. 4. 23:58
728x90

๋ฌธ์ œ

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

 

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

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

programmers.co.kr

 

ํ•ด๊ฒฐ๋ฒ•

BFS ๊ธฐ๋ณธ/๋Œ€ํ‘œ ๋ฌธ์ œ๋‹ค. ์ตœ๋‹จ๊ฑฐ๋ฆฌ ์ฐพ๊ธฐ ๋ฌธ์ œ์ด๋ฏ€๋กœ Queue๋ฅผ ์ด์šฉํ•œ BFS ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜๋ฉด ๋œ๋‹ค.

์ด ๋•Œ, ๊ตฌ์ง€ ๋ฐฉ๋ฌธ์ฒดํฌ ๋ฐฐ์—ด์„ ์•ˆ๋งŒ๋“ค๊ณ  ํ˜„์žฌ ๊ฐ’์— +1์„ ํ•˜๋ฉฐ ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์ฃผ๋ฉด ๋” ํŽธํ•˜๊ฒŒ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

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

1) BFS ์ฝ”๋“œ

#include <vector>
#include <queue>
#include <utility>//pair queue
using namespace std;

int BFS(vector<vector<int>> maps){
    int direction[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};//์ƒํ•˜์ขŒ์šฐ ๋ฐฉํ–ฅํ‚ค
    queue<pair<int, int>> q;//BFS ํƒ์ƒ‰์„ ์œ„ํ•œ queue
    
    //ํ˜„์žฌ ์ขŒํ‘œ queue์— ์ถ”๊ฐ€
    q.push(make_pair(0,0));
    
    //queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ํƒ์ƒ‰
    while(!q.empty()){
    
    	//ํ˜„์žฌ queue ๋นผ๊ธฐ ์ „์— ๊ฐ’ ๋ณ€์ˆ˜์— ์ €์žฅํ•ด๋‘๊ธฐ
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        
        //์ฃผ๋ณ€ ํƒ์ƒ‰. ๋Œ€๋ถ€๋ถ„ ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰.
        for(int k = 0; k < 4; k++){
            int nx = x + direction[k][0];
            int ny = y + direction[k][1];
            
            //์ด๋™ ๊ฐ€๋Šฅํ•œ ๊ณณ์ด๋ฉด ํ•ด๋‹น์ขŒํ‘œ๋ฅผ queue์— ๋„ฃ๊ธฐ
            if(maps[nx][ny] == 1){
                maps[nx][ny] = maps[x][y] + 1;
                q.push(make_pair(nx,ny));
            }
        } 
     }
}

2) pair queue, queue ์‚ฌ์šฉ๋ฒ•

#include <queue>
#include <utility>//pair queue๋ฅผ ์œ„ํ•ด ์„ ์–ธ

//queue ์„ ์–ธ. ์ธ์ž 2๊ฐœ ๋„ฃ์„๊ฑฐ๋ฉด pair queue๋กœ ์ƒ์„ฑ
queue<pair<int, int>> q;

//q์— pair๊ฐ’ ๋„ฃ๊ธฐ
q.push(make_pair(0,0));

//q์˜ ์ฒซ๋ฒˆ์งธ ์›์†Œ x, y๊ฐ’ ๊บผ๋‚ด๊ธฐ
int x = q.front().first;
int y = q.front().second;

//q ๋งจ ์•ž ๊ฐ’ ๊บผ๋‚ด๊ธฐ
q.pop();

 

์ฝ”๋“œ

#include <vector>
#include <queue>
#include <utility>//pair queue
using namespace std;

int BFS(vector<vector<int>> maps){
    int direction[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};//์ƒํ•˜์ขŒ์šฐ ๋ฐฉํ–ฅํ‚ค
    queue<pair<int, int>> q;//BFS ํƒ์ƒ‰์„ ์œ„ํ•œ queue
    
    //0,0 ๋ถ€ํ„ฐ ํƒ์ƒ‰ ์‹œ์ž‘
    q.push(make_pair(0,0));
    
    //queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ํƒ์ƒ‰
    while(!q.empty()){
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        //์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰
        for(int k = 0; k < 4; k++){
            int nx = x + direction[k][0];
            int ny = y + direction[k][1];
            //๋ฒ”์œ„์ฒดํฌ
            if(nx < 0 || nx >= maps.size() || ny < 0 || ny >= maps[0].size()) continue;
            //์ƒ๋Œ€ํŒ€ ์ง„์˜์— ๋„์ฐฉํ–ˆ๋‹ค๋ฉด ๋ฐ”๋กœ ์ •๋‹ต ๋ฆฌํ„ด. ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด.
            if(nx == maps.size()-1 && ny == maps[0].size()-1) return maps[x][y] + 1;
            //๊ฐ€๋Š” ๊ธธ์ด 1์ด๋ฉด ํƒ์ƒ‰ queue์— ์ถ”๊ฐ€
            if(maps[nx][ny] == 1){
                maps[nx][ny] = maps[x][y] + 1;
                q.push(make_pair(nx,ny));
            }
        }
        //ํ˜„์žฌ ๊ฐ’์— + 1 ํ•œ ๊ฐ’์„ ์ด์›ƒ ๋ธ”๋Ÿญ์— ๋„ฃ์–ด์•ผ ํ•˜๋ฏ€๋กœ map[0][0] = 2 ๋กœ ๋งž์ถฐ๋‘๊ธฐ.
        if(x == 0 && y == 0)  maps[x][y] = 2;
    }
    //queue๊ฐ€ ๋นŒ ๋•Œ ๊นŒ์ง€ ํƒ์ƒ‰ํ–ˆ๋Š”๋ฐ ์ƒ๋Œ€ํŒ€ ์ง„์˜์„ ๋ชป๋งŒ๋‚ฌ๋‹ค๋ฉด ๊ฐˆ ์ˆ˜ ์—†๋Š” ๊ฒƒ. return -1.
    return -1;
}

int solution(vector<vector<int>> maps){
    return BFS(maps);
}
728x90