Skip to content

Instantly share code, notes, and snippets.

@nineteeneightyfour1219
Created October 30, 2020 06:43
Show Gist options
  • Save nineteeneightyfour1219/d4e7056ea9721c1b65506693b974b909 to your computer and use it in GitHub Desktop.
Save nineteeneightyfour1219/d4e7056ea9721c1b65506693b974b909 to your computer and use it in GitHub Desktop.
순열 해독기.cpp
#include <iostream>
using namespace std;
int n,k;
int check = 1;
int checking = 0;
int arraying[1000][15] = { 0, };
int sum1[1000] = { 0, };
int total[1000] = { 0, };
int arr[15] = { 0, };
bool used[15] = { 0, };
int ab(int x) {
if (x < 0) return -x;
return x;
}
void search(int x) {
if (x == k) {
for (int i = 0; i < n; i++) {
total[i] = 0;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
total[i] += ab(arr[j] - arraying[i][j]);
}
}
for (int i = 0; i < n; i++) {
if (total[i] != sum1[i]) {
return;
}
}
for (int i = 0; i < k; i++) {
cout << arr[i] << " ";
}
cout << "\n";
checking = 1;
return;
}
for (int i = 1; i <= k; i++) {
if (used[i]) continue;
used[i] = 1;
arr[x] = i;
search(x + 1);
used[i] = 0;
}
}
int main() {
cin.tie(NULL); ios::sync_with_stdio(false);
cin >> n;//"테스트 케이스 입력"
X:
cin >> k;//"배열 길이 입력"
if (k > 15) { cout << "입력이 잘못되었습니다." << "\n"; goto X; }
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
cin >> arraying[i][j];//"각 경우당 수 입력"
}
cin >> sum1[i];//"각 경우당 차이값 입력"
}
search(0);
if (checking == 0) cout << "impossible";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment