UVA 11795
B |
Mega Man’s Missions |
|
Input |
Standard Input |
|
Output |
Standard Output |
Mega Man is off to save the world again. His objective is to kill the Robots created by Dr. Wily whose motive is to conquer the world. In each mission, he will try to destroy a particular Robot. Initially, Mega Man is equipped with a weapon, called the “Mega Buster” which can be used to destroy the Robots. Unfortunately, it may happen that his weapon is not capable of taking down every Robot. However, to his fortune, he is capable of using the weapons from Robots which he has completely destroyed and these weapons maybe able to take down Robots which he otherwise cannot with his own weapon. Note that, each of these enemy Robots carry exactly one weapon themselves for fighting Mega Man. He is able to take down the Robots in any order as long as he has at least one weapon capable of destroying the Robot at a particular mission. In this problem, given the information about the Robots and their weapons, you will have to determine the number of ways Mega Man can complete his objective of destroying all the Robots.
Input
Input starts with an integer T(T≤50), the number of test cases.
Each test case starts with an integer N(1≤N≤16). Here N denotes the number of Robots to be destroyed (each Robot is numbered from 1 to N). This line is followed by N+1 lines, each containing N characters. Each character will either be ‘1’ or ‘0’. These lines represent a (N+1)*N matrix. The rows are numbered from 0 to N while the columns are numbered from 1 to N. Row 0 represents the information about the “Mega Buster”. The jth character of Row 0 will be ‘1’ if the “Mega Buster” can destroy the jthRobot. For the remaining N rows, the jth character of ith row will be ‘1’ if the weapon of ith Robot can destroy the jth Robot. Note that, a Robot’s weapon could be used to destroy the Robot itself, but this will have no impact as the Robot must be destroyed anyway for its weapon to be acquired.
Output
For each case of input, there will be one line of output. It will first contain the case number followed by the number of ways Mega Man can complete his objective. Look at the sample output for exact format.
Sample Input |
Sample Output |
3 1 1 1 2 11 01 10 3 110 011 100 000
|
Case 1: 1 Case 2: 2 Case 3: 3
|
设dp[s] 为 已经摧毁掉的机器人的集合s的方法数
swep[s] 为摧毁机器人的集合s所拥有的武器集合
则dp[s] += dp[s ^ ( 1 << j]] ( 1 << j & j) && ( (1 << j) & (swep[ s ^ ( 1 << j)]) ( 0 =< j < N)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <bitset> 6 7 using namespace std; 8 9 typedef long long ll; 10 const int MAX_N = 20; 11 int N; 12 int wea[MAX_N],swea[1 << 17]; 13 ll dp[1 << 17]; 14 15 16 void init() { 17 for(int i = 0; i < (1 << N); ++i) { 18 swea[i] = wea[0]; 19 for(int j = 0; j < N; ++j) { 20 if(1 << j & i) { 21 swea[i] |= wea[j + 1]; 22 } 23 } 24 } 25 26 } 27 void solve() { 28 dp[0] = 1; 29 for(int i = 1; i <= N; ++i) { 30 int comb = (1 << i) - 1; 31 while(comb < 1 << N) { 32 33 for(int j = 0; j < N; ++j) { 34 if((1 << j & comb) && ((1 << j) 35 & (swea[comb ^ ( 1 << j)]))) { 36 dp[comb] += dp[comb ^ (1 << j)]; 37 } 38 } 39 int x = comb & -comb, y = comb + x; 40 comb = ((comb & ~y) / x >> 1) | y; 41 } 42 } 43 44 printf("%lld\n",dp[(1 << N) - 1]); 45 } 46 47 int main() 48 { 49 // freopen("sw.in","r",stdin); 50 int t; 51 scanf("%d",&t); 52 int ca = 1; 53 while(t--) { 54 memset(dp,0,sizeof(dp)); 55 memset(wea,0,sizeof(wea)); 56 scanf("%d",&N); 57 for(int i = 0; i <= N; ++i) { 58 char s[20]; 59 scanf("%s",s); 60 for(int j = 0; s[j] != '\0'; ++j) { 61 if(s[j] != '0') wea[i] |= (1 << j); 62 } 63 } 64 65 init(); 66 printf("Case %d: ",ca++); 67 solve(); 68 } 69 //cout << "Hello world!" << endl; 70 return 0; 71 }