-
Notifications
You must be signed in to change notification settings - Fork 0
/
11553.cpp
42 lines (39 loc) · 1.01 KB
/
11553.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
#include <iostream>
#include <vector>
using namespace std;
int funcion(vector< vector<int> > &mat, vector<int> pasx, vector<int> pasy,
int niv, int val, int fin,int t) {
// cout<<niv<<" "<<val<<" "<<fin<<" "<<t<<endl;
int men = 9999999;
int i = niv;
for (int j = 0; j < mat[i].size() && !pasx[i]; ++j) {
if (!pasx[i] && !pasy[j]) {
pasx[i] = true;
pasy[j] = true;
if (niv == fin)
return val + mat[i][j];
else
men =
min(funcion(mat, pasx, pasy, niv + 1,val + mat[i][j], fin,t), men);
pasx[i] = false;
pasy[j] = false;
}
}
return men;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
vector< vector<int> > mat(n, vector<int>(n));
vector<int> pasx(n, false);
vector<int> pasy(n, false);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
scanf("%d", &mat[i][j]);
printf("%d\n", funcion(mat, pasx, pasy, 0, 0, n-1,t));
}
return 0;
}