Simple dfs hdu 4536 XCOM Enemy Unknown

Source: Internet
Author: User

Question meaning:

In n countries, each country has a fear value, and each country belongs to one continent. Aliens Attack k times. Each time they attack three countries in different continents, they will ask you the maximum number of times you can defend against attacks as a consortium.

Each attack can support a country. After the attack, the fear Value of the country is-2, but the fear value of the other two countries is + 2, in addition, the fear value of all other countries in the continent where the other two countries are located is + 1.

When the fear value of a country is greater than 5, the task fails and cannot continue to resist the attack. Each country's fear value is minimized to 1.

Solution:

Dfs brute force search.

Three options are available each time. Select the country to be supported.

Code:

 

# Include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <set> # include <stack> # include <list> # include <queue> # define eps 1e-6 # define INF 0x1f1f1f1f # define PI acos (-1.0) # define ll _ int64 # define lson l, m, (rt <1) # define rson m + 1, r, (rt <1) | 1 // # pragma comment (linker, "/STACK: 1024000000,1024000000") using namespace std;/* freopen ("data. in "," r ", stdin); freopen (" data. out "," w ", stdout); */int n, m, k; int st [20], fear [20]; vector <int> myv [6]; struct Node {int a [3];} fi [110]; bool flag; int ans; void dfs (int cur, int * afr) {if (flag) return; for (int I = 0; I <n; I ++) {if (afr [I]> 5) // more than 5, it indicates that the last time has failed, therefore, it is cur-2 {ans = max (cur-2, ans); return ;}} if (cur> k) // can resist all attacks {flag = true; ans = k; return;} int tmp [20]; for (int I = 0; I <3; I ++) // select the protected country {// memcpy (tmp, afr, sizeof (afr); for (int j = 0; j <n; j ++) tmp [j] = afr [j]; tmp [fi [cur]. a [I]-= 2; // reduce the national fear value by 2 if (tmp [fi [cur]. a [I] <1) tmp [fi [cur]. a [I] = 1; for (int j = 1; j <= 2; j ++) // process the other two countries {int k = (I + j) % 3; int sta = st [fi [cur]. a [k]; for (int p = 0; p <myv [sta]. size (); p ++) {tmp [myv [sta] [p] + = 1;} tmp [fi [cur]. a [k] ++;} // printf ("cur: % d I: % d \ n", cur, I);/* for (int j = 0; j <n; j ++) printf ("% d", tmp [j]); putchar ('\ n'); */dfs (cur + 1, tmp) ;}} int main () {// printf ("% lf \ n", pow (5.0, 16.0); int t; scanf ("% d ", & t); for (int ca = 1; ca <= t; ca ++) {scanf ("% d", & n, & m, & k); for (int I = 0; I <m; I ++) myv [I]. clear (); for (int I = 0; I <n; I ++) {scanf ("% d", & st [I]); // The continent myv [st [I]. push_back (I); // all countries in the continent} for (int I = 0; I <n; I ++) scanf ("% d ", & fear [I]); // The fear value for (int I = 1; I <= k; I ++) for (int j = 0; j <3; j ++) scanf ("% d", & fi [I]. a [j]); // alien attack country ans = 0; flag = false; dfs (1, fear); printf ("Case # % d: % d \ n ", ca, ans);} return 0 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.