#include <stdio.h> #include <algorithm> int main(void) { int capacities[3]; int amounts[3]; int new_amounts[3]; for (size_t i = 0; i < 3; ++i) { scanf("%i", &capacities[i]); } for (size_t i = 0; i < 3; ++i) { scanf("%i", &amounts[i]); } for (int desirable = 0; desirable <= capacities[2]; ++desirable) { int result = 999; // Zero steps case if (0 < result) { if (amounts[0] == desirable || amounts[1] == desirable || amounts[2] == desirable) result = 0; } if (1 < result) { for (size_t i = 0; i < 3 && 1 < result; ++i) { for (size_t j = 0; j < 3; ++j) { if (i == j) continue; int new_j = std::min(capacities[j], amounts[i]+amounts[j]); int taken = new_j - amounts[j]; if (desirable == new_j || desirable == amounts[i]-taken) { result = 1; break; } } } } if (2 < result) { for (size_t i = 0; i < 3 && 2 < result; ++i) { for (size_t j = 0; j < 3 && 2 < result; ++j) { if (i == j) continue; new_amounts[0] = amounts[0]; new_amounts[1] = amounts[1]; new_amounts[2] = amounts[2]; int new_ij = std::min(capacities[j], amounts[i]+amounts[j]); int taken_ij = new_ij - amounts[j]; new_amounts[j] = new_ij; new_amounts[i] -= taken_ij; for (size_t ii = 0; ii < 3 && 2 < result; ++ii) { for (size_t jj = 0; jj < 3; ++jj) { // if (ii == i || ii == j || ii == jj || jj == i || jj == j) // continue; if (ii == jj) continue; int new_iijj = std::min(capacities[jj], new_amounts[ii] + new_amounts[jj]); int taken_iijj = new_iijj - new_amounts[jj]; new_amounts[jj] = new_iijj; new_amounts[ii] -= taken_iijj; if (desirable == new_amounts[0] || desirable == new_amounts[1] || desirable == new_amounts[2]) { result = 2; break; } } } } } } if (result == 999) result = -1; printf("%i ", result); } printf("\n"); return 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 | #include <stdio.h> #include <algorithm> int main(void) { int capacities[3]; int amounts[3]; int new_amounts[3]; for (size_t i = 0; i < 3; ++i) { scanf("%i", &capacities[i]); } for (size_t i = 0; i < 3; ++i) { scanf("%i", &amounts[i]); } for (int desirable = 0; desirable <= capacities[2]; ++desirable) { int result = 999; // Zero steps case if (0 < result) { if (amounts[0] == desirable || amounts[1] == desirable || amounts[2] == desirable) result = 0; } if (1 < result) { for (size_t i = 0; i < 3 && 1 < result; ++i) { for (size_t j = 0; j < 3; ++j) { if (i == j) continue; int new_j = std::min(capacities[j], amounts[i]+amounts[j]); int taken = new_j - amounts[j]; if (desirable == new_j || desirable == amounts[i]-taken) { result = 1; break; } } } } if (2 < result) { for (size_t i = 0; i < 3 && 2 < result; ++i) { for (size_t j = 0; j < 3 && 2 < result; ++j) { if (i == j) continue; new_amounts[0] = amounts[0]; new_amounts[1] = amounts[1]; new_amounts[2] = amounts[2]; int new_ij = std::min(capacities[j], amounts[i]+amounts[j]); int taken_ij = new_ij - amounts[j]; new_amounts[j] = new_ij; new_amounts[i] -= taken_ij; for (size_t ii = 0; ii < 3 && 2 < result; ++ii) { for (size_t jj = 0; jj < 3; ++jj) { // if (ii == i || ii == j || ii == jj || jj == i || jj == j) // continue; if (ii == jj) continue; int new_iijj = std::min(capacities[jj], new_amounts[ii] + new_amounts[jj]); int taken_iijj = new_iijj - new_amounts[jj]; new_amounts[jj] = new_iijj; new_amounts[ii] -= taken_iijj; if (desirable == new_amounts[0] || desirable == new_amounts[1] || desirable == new_amounts[2]) { result = 2; break; } } } } } } if (result == 999) result = -1; printf("%i ", result); } printf("\n"); return 0; } |