int m,n,t; int v[30],vi[30][30]; //v记录目标, vi记录可用选择 int total,p[30],ni[30]; bool line[30];
bool check() { for (int i = 0; i < m; i++) if(p[i] < v[i]) returnfalse; //只要有一个不满足就不行 returntrue; }
void search(int k,int depth) { if(check() && depth < total) //递归的结束控制 { total = depth; t = 0; for (int i = 0; i < n; i++) if(line[i]) ni[t++] = i+1; //记录行号 return; } for (int i = k+1;i < n; i++) { for (int j = 0; j < m; j++) p[j]+=vi[i][j]; //再增加一排进行搜索 line[i] = true; search(i,depth+1); //对应的depth要加1. for (int j = 0; j < m; j++) p[j]-=vi[i][j]; //将p[j]复原 line[i] = false; } }
int main() { cin >> m; for (int i = 0; i < m; i++) cin >> v[i]; cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> vi[i][j]; total = 100; memset(p,0,sizeof(p)); memset(line,false,sizeof(line)); for (int i = 0; i < n; i++) //i表示第i+1排 { for (int j = 0; j < m; j++) //j表示第j+1列 p[j]+=vi[i][j]; //p[j]表示有i+1排时,第j列的和。 line[i] = true; //将第i排标记为正在使用中 search(i,1); for (int j = 0; j < m; j++) p[j]-=vi[i][j]; //将p[j]复原 line[i] = false; } cout << total <<""; t = 0; for (int i = 0; i < total-1; i++) cout << ni[i] <<""; cout << ni[total-1] << endl; return0; }