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 Busterwhich 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 jth Robot. 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 Robots 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
|
题意:
给出初始武器和消灭每个机器人所能获得的武器,消灭每个机器人需要对应的武器。问消灭所有机器人的方法一共有多少种。(N<=16)
分析:
可以用一个16位的二进制数表示出当前机器人的存在情况,即一个状态。
首先预处理出每个状态下所能获得的武器。
然后依据利用DP的方式求解到达每种状态的方法数。
dp[s] = sum(dp[s-(1<<i)]) 条件为s&(1<<i) 且 s^(1<<i)&i
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#define INF 0x7fffffff
using namespace std;
typedef long long LL;
const int N = 1<<17;
int weapon[N],rebot[N],n;
LL dp[N];
LL dfs(int state)
{
if(dp[state]!=-1) return dp[state];
dp[state]=0;
for(int i=0;i<n;i++)
{
if(state&(1<<i) && weapon[state-(1<<i)]&(1<<i))
dp[state]+=dfs(state-(1<<i));
}
return dp[state];
}
int main()
{
int T,C=1;
char s[20];
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(rebot,0,sizeof(rebot));
memset(weapon,0,sizeof(weapon));
for(int i=0;i<=n;i++)
{
scanf("%s",s);
for(int j=0;j<n;j++) if(s[j]-'0')
rebot[i]|=(1<<j);
}
int tot=1<<n;
for(int i=0;i<tot;i++)
{
weapon[i]=rebot[0];
for(int j=0;j<n;j++) if(i&(1<<j))
{
weapon[i]|=rebot[j+1];
}
}
memset(dp,-1,sizeof(dp));
dp[0]=1LL;
printf("Case %d: %lld\n",C++,dfs(tot-1));
}
return 0;
}