UVa11795 Mega man ' s Mission (state compression DP)

Source: Internet
Author: User

Mega man ' s Mission

Mega man was off to save TheWorld again. His objective was to kill the Robots created by dr.wily whose motive was to conquer the world. In each mission, he willtry to destroy a particular Robot. Initially, Mega man is equippedwith a weapon, called the"Mega Buster" which canbe used to destroy Therobots.Unfortunately, it mayhappen that he weapon was not capable of taking down every robot.however, to his fortune, he was capab Le of using the weapons fromrobots which he hascompletelydestroyedAnd these weapons maybe able to takedown Robots which he otherwise cannot with his own weapon. Notethat, each of these enemy Robots carryexactlyOne weapon themselvesfor fighting Mega man.He isable to take off the Robots in any order as long as he had atleast one weapon capable of destroying the Robot at a P Articularmission. In this problem, given the information is about the Robotsand their weapons, and you'll have the determine the number of Waysmeg A man can complete he objective of destroying all therobots.

Input

Input starts with Aninteger t (t), thenumber of test cases.

Each test case starts with AnintegerN(1N(+ ). HereNDenotes thenumber of Robots to being destroyed (each Robot was numbered from 1toN). This is FollowedbyN+1 Lines,eachcontainingNCharacters. Each character'll eitherbe‘1’Or‘0’. These lines Representa(N+ 1) *NMatrix. The rows is numbered from 0toNWhile Thecolumns is numbered from 1 toN. Row 0 represents the information about the"Megabuster". TheJthCharacterof Row0Willbe‘1’Ifthe"Mega Buster"Can destroytheJthRobot.for theremainingNRows,theJthCharacterofIthRowwill be‘1’If the weaponofIthRobotcan destroytheJthRobot.note that, a Robot ' s weapon could is used to destroy the robotitself, but this would have no impact as the Robot must Be destroyedanyway for it weapon to be acquired.

Output

For each case of input, there'll be is one lineof output. It would first contain the case number followed by thenumber of ways Mega Mans can complete his objective. Look at thesample output for exact format.


Sample Input

3

1

1

1

2

11

01

10

3

110

011

100

000


Sampleoutput

Case 1:1

Case 2:2

Case 3:3


Test instructions: The Locke initially had only one weapon "Mega Buster" (a weapon that could destroy certain robots), and you needed to destroy N other robots in a certain order. For every robot you destroy, you will get his weapons (and possibly no weapons), and these weapons can destroy specific robots. Your task is to calculate the total number of orders to eliminate all robots. Note: A robotic weapon may be able to destroy itself, but this has no effect on the final answer, because the machine must first be destroyed to get his weapons.


Idea: The range of n is [1,16] and can be done using state compression DP to save the state in binary. The time complexity is O (n*2^n). First use the weapon array to save the weapon to kill the robot, in binary notation, the first I bit is 1 to kill the first robot, 0 is not possible. Where Weapon[0] represents the original possession of the weapon (i.e. "Mega Buster"). Using the set St to represent the killed robot, the binary 1 is killed, 0 is not killed, in that state possession of the weapon (all states the initial weapon is weapon[0]), with the attack array to save all States. Use dp[st] to indicate the total number of sequential scenarios when the status is St. So how do you write the state shift?

First of all, for each state, we preprocess this state can kill which robot, so as to transfer, and then for a state St, to kill the first robot, then never kill I state inside transfer, namely s^ (1<<i).

Dp[s] = SUM (dp[s^ (1<<i)), the condition is s& (1<<i) and attack[s^ (1<<i)] can kill I.

Enumerate all 1 inside the ST and add up.

When all robots do not kill the total number of scenarios is 1, i.e. dp[0] = 1.


<span style= "FONT-SIZE:18PX;" > #include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include < string> #include <algorithm> #include <queue> #include <stack>using namespace std; #define LL Long Longconst Double PI = ACOs ( -1.0); const double e = 2.718281828459;const double eps = 1e-8;const int maxn = (1<<16) +10 ; int WEAPON[MAXN]; Save each robot can kill the robot int ATTACK[MAXN];  Save each state can kill the robot ll DP[MAXN]; 2 of 16 times will explode int.    The number of sequential schemes used to count each state char S[20];int main () {//freopen ("In.txt", "R", stdin);    Freopen ("OUT.txt", "w", stdout);    int case, N;    int num = 1;    cin>>case;        while (case--) {cin>>n;            for (int i = 0; I <= N; i++) {scanf ("%s", s);            Weapon[i] = 0;            for (int j = 0; J < N; j + +) {if (s[j] = = ' 1 ') weapon[i] |= 1<<j; }//cout<<i<< "" <<weapon[i]<< Endl;        } int total = (1<<n)-1;            for (int st = 0, St <= total; st++) {attack[st] = weapon[0];                for (int i = 1; I <= n; i++) {int j = i-1;                if (st& (1<<j)) {//If the state can kill I, then the state can also kill I can kill Attack[st] |= Weapon[i];        }}//printf ("%d%d\n", St, Attack[st]);        } memset (DP, 0, sizeof (DP));        Dp[0] = 1; for (int st = 1, St <= Total, st++) {for (int i = 0; i < n; i++) {if (st&                    amp; (1<<i))//If this state of St can kill I {//Then ST is transferred by the state that cannot kill I, i.e. st^ (1<<i)                        if (attack[st^ (1<<i)]& (1<<i)) {//and requires st^ (1<<i) This state to be able to kill I                    DP[ST] + = dp[st^ (1<<i)]; }}}//printf ("%d%d\n", St, Dp[st]);    } printf ("Case%d:%lld\n", num++, Dp[total]); } return 0;} </span>



UVa11795 Mega man ' s Mission (state compression DP)

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.