#include<iostream> #include <cstdlib> using namespace std; int print(const int *a, const int size, unsigned int * kolejnosc, unsigned int ** tablica, int zycie1); void permutacja(int *Value, int N, int k, unsigned int * kolejnosc, unsigned int ** tablica, int zycie1); static unsigned n, z; static int j = 0; static int zycie; static bool prwada = false; static unsigned int ** tablica; static bool wyswietlanie = true; int main() { cin.sync_with_stdio(false); static unsigned n, z; cin >> n >> z; unsigned int ** tablica = new unsigned int*[n + 1]; unsigned int * kolejnosc = new unsigned int[n + 1]; int*permutacjeW = new int[n]; for (int i = 0; i < n; i++) { permutacjeW[i] = 0; } for (int i = 0; i <= n; i++) { tablica[i] = new unsigned int[2]; } for (int i = 1; i <= n; i++) { cin >> tablica[i][0] >> tablica[i][1]; } zycie = z; permutacja(permutacjeW, n, 0, kolejnosc, tablica, z); if (!prwada) { cout << "NIE" << endl; } system("PAUSE"); return 0; } int print(const int *a, const int size, unsigned int * kolejnosc, unsigned int ** tablica, int zycie1) { if (a != 0) { int kol = 0; zycie = zycie1; for (int i = 0; i < size; i++) { kolejnosc[kol] = a[i]; kol++; zycie -= tablica[a[i]][0]; if (zycie <= 0) { goto tu; } else { zycie += tablica[a[i]][1]; } if (i == size - 1 && zycie > 0 && wyswietlanie) { cout << "TAK" << endl; prwada = true; for (int k = 0; k < size; k++) { cout << kolejnosc[k] << " "; } cout << endl; wyswietlanie = false; return 5; } } } tu:; return 1; } void permutacja(int *Value, int N, int k, unsigned int * kolejnosc, unsigned int ** tablica, int zycie) { int koniec; static int level = -1; level = level + 1; Value[k] = level; if (level == N) { koniec = print(Value, N, kolejnosc, tablica, zycie); if (koniec != 1) { exit(EXIT_SUCCESS); } } else for (int i = 0; i < N; i++) if (Value[i] == 0) permutacja(Value, N, i, kolejnosc, tablica, zycie); level = level - 1; Value[k] = 0; }
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | #include<iostream> #include <cstdlib> using namespace std; int print(const int *a, const int size, unsigned int * kolejnosc, unsigned int ** tablica, int zycie1); void permutacja(int *Value, int N, int k, unsigned int * kolejnosc, unsigned int ** tablica, int zycie1); static unsigned n, z; static int j = 0; static int zycie; static bool prwada = false; static unsigned int ** tablica; static bool wyswietlanie = true; int main() { cin.sync_with_stdio(false); static unsigned n, z; cin >> n >> z; unsigned int ** tablica = new unsigned int*[n + 1]; unsigned int * kolejnosc = new unsigned int[n + 1]; int*permutacjeW = new int[n]; for (int i = 0; i < n; i++) { permutacjeW[i] = 0; } for (int i = 0; i <= n; i++) { tablica[i] = new unsigned int[2]; } for (int i = 1; i <= n; i++) { cin >> tablica[i][0] >> tablica[i][1]; } zycie = z; permutacja(permutacjeW, n, 0, kolejnosc, tablica, z); if (!prwada) { cout << "NIE" << endl; } system("PAUSE"); return 0; } int print(const int *a, const int size, unsigned int * kolejnosc, unsigned int ** tablica, int zycie1) { if (a != 0) { int kol = 0; zycie = zycie1; for (int i = 0; i < size; i++) { kolejnosc[kol] = a[i]; kol++; zycie -= tablica[a[i]][0]; if (zycie <= 0) { goto tu; } else { zycie += tablica[a[i]][1]; } if (i == size - 1 && zycie > 0 && wyswietlanie) { cout << "TAK" << endl; prwada = true; for (int k = 0; k < size; k++) { cout << kolejnosc[k] << " "; } cout << endl; wyswietlanie = false; return 5; } } } tu:; return 1; } void permutacja(int *Value, int N, int k, unsigned int * kolejnosc, unsigned int ** tablica, int zycie) { int koniec; static int level = -1; level = level + 1; Value[k] = level; if (level == N) { koniec = print(Value, N, kolejnosc, tablica, zycie); if (koniec != 1) { exit(EXIT_SUCCESS); } } else for (int i = 0; i < N; i++) if (Value[i] == 0) permutacja(Value, N, i, kolejnosc, tablica, zycie); level = level - 1; Value[k] = 0; } |