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

๋ณธ๋ฌธ ์ œ๋ชฉ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํƒ€๊ฒŸ ๋„˜๋ฒ„ (DFS/BFS, C++)

PROGRAMMING/Algorithm

by koharin 2021. 2. 2. 23:57

๋ณธ๋ฌธ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ป C++ ์ œ์ถœ์ฝ”๋“œ


#include <vector>
#include <string>

using namespace std;

int answer = 0;
void DFS(int sum, int index, int target, vector<int> *numbers){
    if(index == (*numbers).size()){ 
        if(sum == target) answer++;
        return;
    }
    DFS(sum + (*numbers)[index], index + 1, target, numbers);
    DFS(sum - (*numbers)[index], index + 1, target, numbers);
}

int solution(vector<int> numbers, int target) {

    DFS(0, 0, target, &numbers);
    return answer;
}

 

 

๐Ÿ“ ๋ฌธ์ œํ’€์ด ๊ณผ์ •


#include <stdio.h>
#include <vector>

using namespace std;
int answer = 0;
void DFS(int sum, int index, int target, vector<int> *numbers){
    if(index == (*numbers).size()){ 
        if(sum == target) answer++;
        return;
    }
    DFS(sum + (*numbers)[index], index + 1, target, numbers);
    DFS(sum - (*numbers)[index], index + 1, target, numbers);
}

int solution(vector<int> numbers, int target) {

    DFS(0, 0, target, &numbers);
    return answer;
}

int main(){
    vector<int> number;
    int n;

    scanf("%d", &n);

    for(int i=0; i<n; i++){
        int num;
        scanf("%d", &num);
        number.push_back(num);
    }
    int target;
    scanf("%d", &target);
    printf("# of target: %d\n", solution(number, target));
    
}
  • index๊ฐ€ number.size()๋ผ๋Š” ๊ฒƒ์€ 5๊ฐœ์˜ ๋ชจ๋“  ์›์†Œ๋ฅผ sum์— ๋”ํ•œ ๊ฒƒ์ด๊ณ , ์ด ๊ฒฐ๊ณผ๋ฅผ target๊ณผ ๋น„๊ตํ•˜์—ฌ ๊ฐ™์œผ๋ฉด answer์„ 1 ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.

 

 

๐Ÿ” DFS ๋…ธ๋“œ ํƒ์ƒ‰ ์ˆœ์„œ


DFS(0, 0)
{
      DFS(sum+num[0], 1)
      {
          DFS(sum+num[1], 2)
          {
              DFS(sum+num[2], 3)
              {
                  DFS(sum+num[3], 4)
                  {
                      DFS(sum+num[4], 5)
                      {
                          if(5 == num.size()) return;
                      }
                      DFS(sum-num[4], 5)
                      {
                          if(5 == num.size()) return;
                      }
                  }
                  DFS(sum-num[3]), 4)
                  {
                      DFS(sum+num[4]), 5)
                      {
                      }
                      DFS(sum-num[4]), 5)
                      {
                      }
                  }
  ...
...
  • ์žฌ๊ท€ํ˜ธ์ถœ์˜ ๊ฒฝ์šฐ, ์œ„์™€ ๊ฐ™์€ ๊ณผ์ •์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์ž์‹ ์„ ๋ถˆ๋ €๋˜ ํ•จ์ˆ˜๋กœ ๋ฆฌํ„ดํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค. DFS(0,0)์—์„œ ๋งˆ์ง€๋ง‰์œผ๋กœ DFS(sum-num[0], 1)์„ ๋๋‚ด๋ฉด DFS(0,0)๊ฐ€ ์ž์‹ ์„ ํ˜ธ์ถœํ•œ main ํ•จ์ˆ˜๋กœ ๋ฆฌํ„ดํ•˜์—ฌ ๋น„๋กœ์†Œ ์žฌ๊ท€๋ฅผ ๋๋‚ผ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

[+1, +1, +1, +1, +1]
[+1, +1, +1, +1, -1]

[+1, +1, +1, -1, +1]
[+1, +1, +1, -1, -1]

[+1, +1, -1, +1, +1]
[+1, +1, -1, +1, -1]
[+1, +1, -1, -1, +1]
[+1, +1, -1, -1, -1]

[+1, -1, +1, +1, +1]
[+1, -1, +1, +1, -1]
[+1, -1, +1, -1, +1]
[+1, -1, +1, -1, -1]
[+1, -1, -1, +1, +1]
[+1, -1, -1, +1, -1]
[+1, -1, -1, -1, +1]
[+1, -1, -1, -1, -1]

[-1, +1, +1, +1, +1]
[-1, +1, +1, +1, -1]
[-1, +1, +1, -1, +1]
[-1, +1, +1, -1, -1]
[-1, +1, -1, +1, +1]
[-1, +1, -1, +1, -1]
[-1, +1, -1, -1, +1]
[-1, +1, -1, -1, -1]
[-1, +1, -1, -1, -1]
[-1, -1, +1, +1, +1]
[-1, -1, +1, +1, -1]
[-1, -1, +1, -1, +1]
[-1, -1, +1, -1, -1]
[-1, -1, -1, +1, +1]
[-1, -1, -1, +1, -1]
[-1, -1, -1, -1, +1]
[-1, -1, -1, -1, -1]
  • ์›์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 5๊ฐœ์ธ ๊ฒฝ์šฐ, ์œ„์™€ ๊ฐ™์€ ๋…ธ๋“œ ํƒ์ƒ‰ ์ˆœ์„œ๋ฅผ ๊ฐ–๋Š”๋‹ค.

 

 

๐Ÿ‘ ๊ฒฐ๊ณผ


728x90
๋ฐ˜์‘ํ˜•

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