#include <iostream>
#include <vector>
#include <windows.h>
#include "matrix.h"
using namespace std;
typedef vector<int> Function;
typedef vector<vector<int>> FunctionFull;
FunctionFull Assignation(int ** data, int N, int M, Function Choice, Function BusyPos, FunctionFull Opt, int j) {
for (int i = 0; i < M; i++) {
if(BusyPos[i] == 0) {
BusyPos[i] = 1;
Choice[j] = i;
Choice[N] += data[Choice[j]][j];
if (j < N-1) Opt = Assignation(data, N, M, Choice, BusyPos, Opt, j+1);
if(Choice[N] >= Opt[0][N]) {
if(Choice[N] > Opt[0][N]) Opt.clear();
Opt.push_back(Choice);
Choice[N] -= data[Choice[j]][j];
}
else {
BusyPos[i] = 0;
Choice[N] -= data[Choice[j]][j];
}
}
}
return Opt;
}
FunctionFull Assignation(int ** data, int N, int M) {
return Assignation(data, N, M, Function(N+1, 0), Function(N+1, 0), FunctionFull(1,Function(N+1, 0)), 0);
}
FunctionFull Assignation1(int ** data, int N, int M, Function Choice, Function BusyPos, FunctionFull Opt, int j) {
for (int i = 0; i < N; i++) {
if(BusyPos[i] == 0) {
BusyPos[i] = 1;
Choice[j] = i;
Choice[N] += data[Choice[j]][j];
if (j < N-1) Opt = Assignation(data, N, M, Choice, BusyPos, Opt, j+1);
if(Choice[N] >= Opt[0][N]) {
if(Choice[N] > Opt[0][N]) Opt.clear();
Opt.push_back(Choice);
Choice[N] -= data[Choice[j]][j];
}
else {
BusyPos[i] = 0;
Choice[N] -= data[Choice[j]][j];
}
}
}
return Opt;
}
FunctionFull Assignation1(int ** data, int N, int M) {
return Assignation1(data, N, M, Function(N+1, 0), Function(N+1, 0), FunctionFull(1,Function(N+1, 0)), 0);
}
void Result(FunctionFull Optimal){
for (auto v : Optimal) {
for(int i : v) cout << i << ' ';
cout << endl;
}
}
int main() {
SetConsoleCP (1251);
SetConsoleOutputCP (1251);
int N, M;
cout << "Введите размер матрицы MxN (Сотрудников х Должностей): ";
cin >> N >> M;
if(M <= 1 && N <= 1){
cerr << "Сотрудников / Дожностей должно быть больше одного!";
return -2;
}
int size = N * M;
int ** data = new int * [size];
for (int j = 0; j < size; j++) {
data [j] = new int [size];
}
int Option;
cout << "Сгенерировать матрицу (0) или вводить самостоятельно (...)?" << endl;
cin >> Option;
system("cls");
if(Option){
cout << "Введите данные о " << size << " сотрудниках: " << endl;
Input(data, N, M, Option);
system("cls");
cout << "Введенная матрица (" << M << "x" << N << "):" << endl;
OutPut(data, N, M);
}else{
Input(data, N, M, Option);
cout << "Сгенерированная матрица(" << M << "x" << N << "):" << endl;
OutPut(data, N, M);
}
if (N <= M){
FunctionFull Optimal = Assignation(data, N, M);
cout << endl;
cout << "Результат выбора сотрудников (Номер i сотрудника на должности j): " << endl;
Result(Optimal);
} else{
int ** copy = new int * [size];
for (int j = 0; j < size; j++) {
copy [j] = new int [size];
}
Transpose(copy, data, N, M);
FunctionFull Optimal = Assignation1(copy, N, M);
cout << "Результат выбора сотрудников (Номер j сотрудника на должности i): " << endl;
Result(Optimal);
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8d2luZG93cy5oPgojaW5jbHVkZSAibWF0cml4LmgiCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdHlwZWRlZiB2ZWN0b3I8aW50PiBGdW5jdGlvbjsKdHlwZWRlZiB2ZWN0b3I8dmVjdG9yPGludD4+IEZ1bmN0aW9uRnVsbDsKCkZ1bmN0aW9uRnVsbCBBc3NpZ25hdGlvbihpbnQgKiogZGF0YSwgaW50IE4sIGludCBNLCBGdW5jdGlvbiBDaG9pY2UsIEZ1bmN0aW9uIEJ1c3lQb3MsIEZ1bmN0aW9uRnVsbCBPcHQsIGludCBqKSB7CgoJZm9yIChpbnQgaSA9IDA7IGkgPCBNOyBpKyspIHsKCQlpZihCdXN5UG9zW2ldID09IDApIHsKCQkJQnVzeVBvc1tpXSA9IDE7CgkJCUNob2ljZVtqXSA9IGk7CgkJCUNob2ljZVtOXSArPSBkYXRhW0Nob2ljZV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