์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

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

PROGRAMMING/Algorithm

by koharin 2024. 2. 25. 15:34

๋ณธ๋ฌธ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“  ํ’€์ด ๊ณผ์ •

ํ˜„์žฌ ๋ฐฉ๋ฌธํ•œ ์œ„์น˜๊ฐ€ node[i].first, node[i].second๋ผ๋ฉด index+1 ํ•˜๊ณ  ํ•ด๋‹น ๋…ธ๋“œ ๋ฐฉ๋ฌธ, ์•„๋‹ˆ๋ฉด index ์œ ์ง€ํ•˜๊ณ ํ•ด๋‹น ๋…ธ๋“œ ๋ฐฉ๋ฌธ, ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€ ์—ฌ๋ถ€๋Š” ์ด๊ฒƒ์œผ๋กœ ์ฒดํฌํ•  ์ˆ˜ ์žˆ์Œ

(2,0) → (2,1) → (1,1) → (0,1) → (0,2) → (1,2)

(2,0) → (2,1) → (1,1) → (0,1) → (0,0) → (0,1) → (0,2) → (1,2)

(2,0) → (1,0) → (1,1) → (0,1) → (0,2) → (1,2)

(2,0) → (1,0) → (0,0) → (0,1) → (0,2) → (1,2)

(2,0) → (1,0) → (0,0) → (0,1) → (1,1) → (1,2)

๊ณ„์† index ๋˜๋Š” index+1๋กœ ์žฌ๊ท€ ํ˜ธ์ถœ์„ ํ•˜๋ฏ€๋กœ, ๊ณ„์† ์žฌ๊ท€ํ˜ธ์ถœํ•˜๋‹ค๊ฐ€ ๋งˆ์ง€๋ง‰ ์ˆœ์„œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๋ฉด result++ํ•˜๊ณ  ๋ฆฌํ„ด.

ํ•จ์ˆ˜๊ฐ€ ๋ฆฌํ„ด์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๋งˆ์ง€๋ง‰ ์‹คํ–‰ ์œ„์น˜์—์„œ ๋‹ค์Œ์„ ์‹คํ–‰ํ•˜๊ฒŒ ๋  ๊ฒƒ.

๋”ฐ๋ผ์„œ ๋ฐฉ๋ฌธ์„ ๋‹ค์‹œ ์ดˆ๊ธฐํ™”ํ•ด์„œ ๋‹ค์Œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋„๋ก ํ•จ.

			if(cx == node[index].first && cy == node[index].second){
                search(cx, cy, index+1);
            } else{
                search(cx, cy, index);
            }

์ด๋ ‡๊ฒŒ if๋ฌธ์œผ๋กœ ํ•˜๋ฉด ๊ฒฐ๊ณผ๊ฐ€ ์ž˜ ๋‚˜์™”๋Š”๋ฐ, ์‚ผํ•ญ์—ฐ์‚ฐ์ž ์ผ์„ ๋• +1 ๊ฐ’์ด ๊ตฌํ•ด์กŒ์Œ. ๊ทธ๋ž˜์„œ ๊ทธ๋ƒฅ if๋ฌธ ์‚ฌ์šฉํ•จ.

 

ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋กœ ํ™•์ธ

 

๐Ÿ“ ์ œ์ถœ ์ฝ”๋“œ

#include<iostream>
#include<vector>

using namespace std;
int nx[] = {0,0,1,-1};
int ny[] = {1,-1,0,0};
int n,m,result=0;
vector<pair<int,int>> node; // ๋ฐฉ๋ฌธํ•ด์•ผ ํ•˜๋Š” ์œ„์น˜(x,y) m๊ฐœ
int graph[4][4]={0,};
int check[4][4]={0,};

int search(int x, int y, int index){
    // ๋ชจ๋“  ๋ฐฉ๋ฌธํ•ด์•ผ ํ•˜๋Š” m๊ฐœ ์ง€์  ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ๋ฌธํ–ˆ์œผ๋ฉด ๋ฆฌํ„ด
    if(index == m){
        result++;
        //cout << endl;
        return 0;
    } 

    for(int i=0; i<4; i++){
        int cx = x + nx[i];
        int cy = y + ny[i];
        // ์œ ํšจํ•˜์ง€ ์•Š๋Š” ์œ„์น˜๋ฉด ํŒจ์Šค
        if(cx < 0 || cy < 0 || cx >= n || cy >= n) continue; 
        // ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๊ณ , ๋ฒฝ(1)์ด ์•„๋‹ˆ๋ฉด (cx,cy) ๋ฐฉ๋ฌธ
        if(check[cx][cy] == 0 && graph[cx][cy] == 0){
            check[cx][cy] = 1;
            //cout << "visit (" << cx << "," << cy << ") \n";
            if(cx == node[index].first && cy == node[index].second){
                search(cx, cy, index+1);
            } else{
                search(cx, cy, index);
            }
            check[cx][cy] = 0;
        }
    }
}

int main(int argc, char** argv)
{
    cin >> n >> m;
    
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            cin >> graph[i][j];
        }
    }

    for(int i=0; i<m; i++){
        int x,y;

        cin >> x >> y;
    
        node.push_back(make_pair(x-1,y-1));
    }
    // ์ง€๋‚˜์•ผ ํ•˜๋Š” ๋…ธ๋“œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ, index๋Š” 0์ด ์•„๋‹Œ 1๋กœ ์คŒ
    check[node[0].first][node[0].second] = 1;
    //cout << "visit (" << node[0].first << "," << node[0].second << ") \n";
    search(node[0].first, node[0].second, 1);
    cout << result << endl;

    return 0;
}

 

๐Ÿ’ป ์ •๋‹ต ๊ฒฐ๊ณผ 

728x90
๋ฐ˜์‘ํ˜•

'PROGRAMMING > Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[BOJ] 14501: ํ‡ด์‚ฌ (C++)  (0) 2024.03.24
[BOJ] 7576: ํ† ๋งˆํ†  (C++)  (1) 2024.03.05
[BOJ] 2875 (C++)  (1) 2024.02.25
[Softeer] ์ž๋™์ฐจ ํ…Œ์ŠคํŠธ (C++)  (0) 2024.02.16
[Softeer] ์„ฑ์ ํ‰๊ท  (C++)  (0) 2024.02.15

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ