题意:你最初只有一个武器,你需要按照一定的顺序消灭n个机器人(n<=16)。每消灭一个机器人将会得到他的武器。
每个武器只能杀死特定的机器人。问可以消灭所有机器人的顺序方案总数。
析:dp[s] 表示已经杀死 s 这个状态的机器人有多少种方案,然后挨着枚举每个机器人,在枚举机器人要保证能够杀死该机器人。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <unordered_map> #include <unordered_set> #define debug() puts("++++"); #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = (1<<16) + 5; const int mod = 2000; const int dr[] = {-1, 1, 0, 0}; const int dc[] = {0, 0, 1, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } LL dp[maxn]; int p[maxn], a[20]; char s[20]; int main(){ int T; cin >> T; for(int kase = 1; kase <= T; ++kase){ scanf("%d", &n); scanf("%s", s); int st = 0; for(int i = 0; i < n; ++i) if(s[i] == '1') st |= (1<<i); for(int i = 0; i < n; ++i){ scanf("%s", s); a[i] = 0; for(int j = 0; j < n; ++j) if(s[j] == '1') a[i] |= (1<<j); } int all = (1<<n) - 1; for(int i = 0; i <= all; ++i){ p[i] = st; for(int j = 0; j < n; ++j) if(i & (1<<j)) p[i] |= a[j]; } memset(dp, 0, sizeof dp); dp[0] = 1; for(int i = 1; i <= all; ++i) for(int j = 0; j < n; ++j) if((p[i^(1<<j)]&i)&(1<<j)) dp[i] += dp[i^(1<<j)]; printf("Case %d: %lld ", kase, dp[all]); } return 0; }