-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path재귀_minimum_sum(S).cpp
54 lines (49 loc) · 1.06 KB
/
재귀_minimum_sum(S).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
3
1 5 3
2 4 7
5 3 5
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
vector <vector<int>> v;
vector <int> check_col;
int n,min0=INT_MAX;
void solution(int row, int score)
{
int sum=0;
if(row == n){
min0=min(min0,score); // 최소값 구하기,뽑은 숫자의 합이 최소가 되는 수
cout << "현재 합계 : " << score << ", 최소값 : " << min0 << "\n\n";
return;
}
for(int i=0;i<n;i++){
if(!check_col[i]) // 같은 열에 방문했는가 check!
{
cout << row << " " << i << " " << score << endl;
check_col[i]=1;
solution(row+1,score+v[row][i]);
check_col[i]=0;
}
}
return;
}
int main()
{
cout << "[입력]" << endl;p;
cin >> n;
v.resize(n,vector<int>(n,0));
check_col.resize(n,0);
for(int i=0;i<n;i++){ n mm
for(int j=0;j<n;j++){
cin >> v[i][j];
}
}
cout << "[방문정보와 합계]" << endl;
solution(0,0);
cout << "[정답]" << endl;
cout << min0;
}