Untitled
unknown
plain_text
a year ago
756 B
2
Indexable
Never
#include <iostream> using namespace std; int n,k,s; int diem[20][20]; bool check; void backtrack(int col, int sum, int last){ if(col == k && sum == s){ check = true; return; } if(col == k || sum == s){ return; } for(int i = 0; i < n; i++){ if(diem[i][col] >= last){ backtrack(col + 1, sum + diem[i][col], diem[i][col]); } } } int main (){ freopen("input.txt", "r", stdin); int T; cin >> T; for (int tc = 1; tc <= T; tc++){ cin >> s >> k >> n; for(int i = 0; i < n; i++){ for(int j = 0; j < k ; j++){ cin >> diem[i][j]; } } check = false; backtrack(0,0,0); if(check) cout << "Case " << tc << endl << 1 << endl; else cout << "Case " << tc << endl << -1 << endl; } return 0; }