๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/Softeer

[C++/Softeer] Lv3. ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ๋ฌธํ•˜๊ธฐ (HSAT 7ํšŒ ์ •๊ธฐ ์ฝ”๋”ฉ ์ธ์ฆํ‰๊ฐ€ ๊ธฐ์ถœ)

728x90

https://softeer.ai/practice/6246/history?questionType=ALGORITHM

 

Softeer - ํ˜„๋Œ€์ž๋™์ฐจ๊ทธ๋ฃน SW์ธ์žฌํ™•๋ณดํ”Œ๋žซํผ

 

softeer.ai

 

ํ˜„๋Œ€ ์†Œํ”„ํ‹ฐ์–ด Lv3. ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ๋ฌธํ•˜๊ธฐ C++ ํ’€์ด

 

๋ฌธ์ œ ๋‚œ์ด๋„๋Š” ์ ์ ˆํ•ด๋ณด์ด๋‚˜, ์กฐ๊ฑด ์„ค๋ช…์ด ์•ฝ๊ฐ„ ์•„์‰ฌ์› ๋˜ ๋ฌธ์ œ.

์ตœ๋‹จ๊ฑฐ๋ฆฌ๋Š” ์•„๋‹ˆ๊ณ  ๋ฐฉ๋ฌธํ–ˆ๋˜ ์นธ์„ ๋‹ค์‹œ ์ง€๋‚˜์ง€๋งŒ ์•Š์œผ๋ฉด ๋˜๋Š”๋“ฏ

์ „์ฒด dfs ๊ฒฝ์šฐ ์ค‘, ์ˆœ์ฐจ์ ์œผ๋กœ ์ €์žฅ๋œ ์ง€์ ์„ ๊ฑฐ์น˜๋Š” ๊ฒฝ์šฐ๋งŒ์„ countํ•ด์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค.


#include<iostream>
#include <vector>
#include <queue>

using namespace std;

int grid[4][4]={0,};
int visited[4][4]={0,};
int n; int m;
vector<pair<int,int>> store;
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
int count=0;

bool canVisit(int x, int y){
    if(x<0 || x>=n || y<0 || y>=n) return false;
    if(grid[x][y] == 1) return false;
    if(visited[x][y]) return false;
    return true;
}

void dfs(int x, int y, int cnt){ // ์‹œ์ž‘ ์œ„์น˜, ๋ฐฉ๋ฌธ์ง€์  ์ˆ˜
    if(store[cnt]==make_pair(x,y)) {
        cnt++;
        if(cnt>=m){
            count ++;
            return;
        }
    }
    visited[x][y] = 1; // ์ด๊ฑฐ ๊นŒ๋จน์—ˆ๋‹ค..์ฃผ์˜!!!
    for(int i=0; i<4; ++i){
        int nx = x + dx[i];
        int ny = y + dy[i];
        if(canVisit(nx, ny)){
            dfs(nx,ny,cnt);
        }
    }
    visited[x][y] = 0;
    return;
}

void findRoute(){
    pair<int,int> p = store[0];
    dfs(p.first, p.second, 1);
}

int main(int argc, char** argv)
{
    int tmp; int tmp2;
    int answer;
    cin >> n >> m;
    for(int i=0; i<n; ++i){
        for(int j=0; j<n; ++j) {
            cin >> grid[i][j];
        }
    }
    for(int i=0; i<m; ++i){
        cin >> tmp >> tmp2;
        store.push_back({tmp-1 , tmp2-1});
    }

    findRoute();
    cout << count;
   return 0;
}
728x90