There are n countries, each of which belongs to one continent. now someone is going to attack some countries. Each attack will come from three different continents. We can choose to protect one country, the fear value of the protected country is-2, the fear value of the other two countries is + 2, and the fear value of the two countries in the same continent is + 1.
Analysis: Because the fear value exceeds 5 points and exceeds the limit, each attack will increase the fear value of the other two continents, therefore, there will be more than 5 fear values in the country within 10 days.
The Code is as follows:
# Include <cstdlib> # include <cstring> # include <cstdio> # include <iostream> # include <algorithm> using namespace STD; int n, m, K; char Val [20], CON [20]; char kill [105] [5]; bool fail () {for (INT I = 0; I <n; ++ I) {If (Val [I]> 5) return true;} return false;} inline void fix (char & X) {If (x <1) x = 1 ;} void modify (int x, int p) {// X days, supporting the kill [x] [I] country char a = kill [x] [p], B = kill [x] [(p + 1) % 3], c = kill [X] [(p + 2) % 3]; Val [a]-= 2, fix (Val [a]); Val [B] + = 2, val [c] + = 2; for (INT I = 0; I <n; ++ I) {If (CON [I] = CON [a]) continue; if (I = B | I = c) continue; If (CON [I] = CON [B]) Val [I] + = 1; if (CON [I] = CON [c]) Val [I] + = 1 ;}} bool DFS (INT X, int end) {// return 1 indicates there is a scheme to make all countries safe and sound // printf ("x = % d \ n", x); If (fail ()) {// if the number of countries in the middle of the day exceeds 5, the solution fails to return false;} If (x = end + 1) {// reaches the end + 1 day, indicating that there is a solution to safely pass the previous end day return true;} Char CPY [20]; memcpy (CPY, Val, sizeof (VAL )); for (INT I = 0; I <3; ++ I) {// Day X, supporting modify (X, I) in the kill [x] [I] countries ); if (DFS (x + 1, end) {return true;} memcpy (Val, CPY, sizeof (CPY);} return false;} int main () {int t; scanf ("% d", & T); For (int ca = 1; Ca <= T; ++ CA) {scanf ("% d", & N, & M, & K); For (INT I = 0; I <n; ++ I) {scanf ("% d", & CON [I]); // indicates that the country belongs Which continent} For (INT I = 0; I <n; ++ I) {scanf ("% d", & Val [I]);} for (INT I = 0; I <K; ++ I) {for (Int J = 0; j <3; ++ J) {scanf ("% d ", & kill [I] [J]) ;}int ans = K; char CPY [20]; // since there is a minimum limit not lower than 1, therefore, the for (INT I = 0; I <K; ++ I) {memcpy (CPY, Val, sizeof (VAL) cannot be restored on the original basis )); // The original value is not recorded here, resulting in a long error if (! DFS (0, I) {ans = I; break;} memcpy (Val, CPY, sizeof (CPY);} printf ("case # % d: % d \ n ", CA, ANS);} return 0 ;}
BFS version:
# Include <cstdlib> # include <cstring> # include <cstdio> # include <iostream> # include <algorithm> using namespace STD; int n, m, K, ans; char Val [20], CON [20]; char kill [105] [5]; bool fail (char Val []) {for (INT I = 0; I <N; ++ I) {If (Val [I]> 5) return true;} return false;} inline void fix (char & X) {If (x <1) X = 1;} void modify (char Val [], int X, int p) {// X days, support for country kill [x] [I] int A = kill [x] [p], B = K Ill [x] [(p + 1) % 3], c = kill [x] [(p + 2) % 3]; Val [a]-= 2, fix (Val [a]); Val [B] + = 2, Val [c] + = 2; for (INT I = 0; I <n; ++ I) {If (CON [I] = CON [a]) continue; if (I = B | I = c) continue; if (CON [I] = CON [B]) Val [I] + = 1; if (CON [I] = CON [c]) val [I] + = 1 ;}} struct que {char Val [20]; char day;} Q [1000000], Pos; int front, tail; void BFS () {front = tail = 0; Q [tail]. day = 0; memcpy (Q [tail ++]. val, Val, Sizeof (VAL); While (front! = Tail) {pos = Q [Front ++]; ans = POS. day; If (POS. day> = k) continue; For (INT I = 0; I <3; ++ I) {q [tail] = Pos; Modify (Q [tail]. val, POS. day, I); If (! Fail (Q [tail]. val) {q [tail ++]. day = POS. day + 1 ;}}} int main () {int t; scanf ("% d", & T); For (int ca = 1; Ca <= T; ++ CA) {scanf ("% d", & N, & M, & K); For (INT I = 0; I <N; ++ I) {scanf ("% d", & CON [I]); // specifies which continent the country belongs to} For (INT I = 0; I <N; ++ I) {scanf ("% d", & Val [I]) ;}for (INT I = 0; I <K; ++ I) {for (Int J = 0; j <3; ++ J) {scanf ("% d", & kill [I] [J]) ;}} BFS (); printf ("case # % d: % d \ n", CA, ANS);} return 0 ;}