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(T50),
the number of test cases.

Each test case starts with an
integer N(1N16).
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
(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 }

UVA 11795,码迷,mamicode.com

时间: 2024-08-04 22:33:30

UVA 11795的相关文章

UVA 11795 七 Mega Man&#39;s Mission

七 Mega Man's Mission Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 11795 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 6 long long dp[65

UVA - 11795 Mega Man&#39;s Mission

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 t

状压DP UVA 11795 Mega Man&#39;s Mission

题目传送门 1 /* 2 题意:洛克人有武器可以消灭机器人,还可以从被摧毁的机器人手里得到武器,问消灭全部机器人的顺序总数 3 状态压缩DP:看到数据只有16,就应该想到状压(并没有).因为是照解题报告写的,代码里加点注释,省的以后忘记了 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-8 10:41:28 8 * File Nam

uva 11795 Mega Man&#39;s Mission 状压dp

// uva 11795 Mega Man's Mission 状压dp // 设r[i]表示第i个机器人所拥有的武器的数目 // r[0]表示初始时洛克人所拥有的武器数 // w[s]表示杀死集合s中的机器人后所得的武器数 // d[s]表示能杀死集合s中的机器人的顺序总数 // d[s] = sigma(d[s-{i}]) 其中i是集合s中的机器人 // 还有一点就是w[S-{i}]的武器可以杀死i. // 注意: // 1)初始的时候d[0]=1,其他均为0.这个很好理解,因为杀死 //

uva 11795(状态压缩dp)

题意:有一个机器人要杀死其他n(1 <= n <= 16)个机器人,他自己配备了一个武器,并且给出了这个武器能杀死的敌人,如101表示他能杀死第1和第3个敌人,这样他就能得到第1和第3号敌人的武器,从而杀死更多的敌人,问他杀死所有的敌人的方法数. 题解:因为敌人数最多是16个,很容易想到应该是状态压缩dp,可以用f[S]表示状态S下进制位为1的敌人全被杀死的方案数,那么结果就是f[(1 << n) - 1],如果拿掉了状态S中的杀死第j个人能得到的武器,剩下的人如果能杀死第j个人,

关于10月15日#5的四道图论题的心得与感悟

还是我,接着补..... 第一题:UVA 10943 送分题,几乎是动规入门的例题 第二题:UVA 11584 Partitioning by Palindromes .关于在字符串中寻找回文串,一开始的直观想法是先预处理再利用类似于线段覆盖的手法处理.但是两者之间存在差别.还是只有老老实实利用动规求解. 第三题:UVALive 6270一开始想暴力模拟后找规律,结果暴力没写出来.在手推的时候,经人指点,发现了规律F[i] = F[i - 1] + F[i - 2].过后才发现是斐波拉契数列.

训练指南DP阶段训练1

最近又忙又颓.............时间抓不紧....下学期开始就要准备考研了.......就2个月左右可以做自己喜欢的事了....争取把紫书和白书没做的,做过的..来一次完整的总结 训练指南上面的5个例题+后面15个习题是第一阶段 vjudge训练地址 http://vjudge.net/contest/139533#overview -------------------------------------------------------------------------------

动态规划(二)

补充(一)中的部分 以下所有计算中都是将一个字符串当做\(s[1]-s[n]\)的 \(LCS\)(最长公共子序列) 我们设\(d[i][j]\)表示第一个串的前\(i\)位与第二个串的前\(j\)位的最长公共子序列的长度. 当\(a[i]==b[j]\)的时候,\(d[i][j]=d[i-1][j-1]+1\). 当\(a[i]!=b[j]\)的时候,\(d[i][j]=max(d[i-1][j],d[i][j-1])\). \(LCS\)(最长公共子串) 我们设\(d[i][j]\)表示匹配

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d