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 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)

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset> using namespace std; typedef long long ll;
const int MAX_N = ;
int N;
int wea[MAX_N],swea[ << ];
ll dp[ << ]; void init() {
for(int i = ; i < ( << N); ++i) {
swea[i] = wea[];
for(int j = ; j < N; ++j) {
if( << j & i) {
swea[i] |= wea[j + ];
}
}
} }
void solve() {
dp[] = ;
for(int i = ; i <= N; ++i) {
int comb = ( << i) - ;
while(comb < << N) { for(int j = ; j < N; ++j) {
if(( << j & comb) && (( << j)
& (swea[comb ^ ( << j)]))) {
dp[comb] += dp[comb ^ ( << j)];
}
}
int x = comb & -comb, y = comb + x;
comb = ((comb & ~y) / x >> ) | y;
}
} printf("%lld\n",dp[( << N) - ]);
} int main()
{
// freopen("sw.in","r",stdin);
int t;
scanf("%d",&t);
int ca = ;
while(t--) {
memset(dp,,sizeof(dp));
memset(wea,,sizeof(wea));
scanf("%d",&N);
for(int i = ; i <= N; ++i) {
char s[];
scanf("%s",s);
for(int j = ; s[j] != '\0'; ++j) {
if(s[j] != '') wea[i] |= ( << j);
}
} init();
printf("Case %d: ",ca++);
solve();
}
//cout << "Hello world!" << endl;
return ;
}

UVA 11795的更多相关文章

  1. UVA 11795 七 Mega Man's Mission

    七 Mega Man's Mission Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Subm ...

  2. 状压DP UVA 11795 Mega Man's Mission

    题目传送门 /* 题意:洛克人有武器可以消灭机器人,还可以从被摧毁的机器人手里得到武器,问消灭全部机器人的顺序总数 状态压缩DP:看到数据只有16,就应该想到状压(并没有).因为是照解题报告写的,代码 ...

  3. UVA - 11795 状压DP

    #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...

  4. UVa 11795 Mega Man's Mission (状压DP)

    题意:你最初只有一个武器,你需要按照一定的顺序消灭n个机器人(n<=16).每消灭一个机器人将会得到他的武器. 每个武器只能杀死特定的机器人.问可以消灭所有机器人的顺序方案总数. 析:dp[s] ...

  5. UVa 11795 状压DP Mega Man's Mission

    kill[S]表示消灭机器人的集合为S,剩下的所能杀死的机器人集合. 设d(S)表示杀死机器人集合为S的方法数,答案为d((1<<n) - 1). d(S)可以由d(S')转移过来,其中S ...

  6. UVA - 11795 Mega Man's Mission

    Mega Man is off to save the world again. His objective is to kill the Robots created by Dr. Wily who ...

  7. DP专题(不定期更新)

    1.UVa 11584 Partitioning by Palindromes(字符串区间dp) 题意:给出一个字符串,划分为若干字串,保证每个字串都是回文串,同时划分数目最小. 思路:dp[i]表示 ...

  8. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  9. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

随机推荐

  1. public、private、protected 与 默认 等访问修饰符

    1)public(公共的):被public修饰的属性和方法可以被所有类访问. 2)private(私有的):被private修饰的属性和方法只能在改类的内部使用. 3)protected(受保护的): ...

  2. ORA-01017 invalid username/password;logon denied" (密码丢失解决方案)

    1.先确认是否输错 用户名和密码 2.如果的确是丢失密码的话: 查看sqlnet.ora 如果是 SQLNET.AUTHENTICATION_SERVICES= (NONE) , 需更改为SQLNET ...

  3. HTTP网页错误代码大全带解释

    HTTP网页错误代码大全带解释 HTTP 400 - 请求无效HTTP 401.1 - 未授权:登录失败HTTP 401.2 - 未授权:服务器配置问题导致登录失败HTTP 401.3 - ACL 禁 ...

  4. centos 安装phantomjs

    sudo yum install gcc gcc-c++ make git openssl-devel freetype-devel fontconfig-devel git clone git:// ...

  5. 横屏下的ImagePickerController

    Try this way.... As per Apple Document, ImagePicker Controller never Rotate in Landscape mode. You h ...

  6. Linux 进程(一):环境及其控制

    进程环境 main启动 当内核执行C程序时,在调用main前先调用一个特殊的启动例程.可执行程序将此启动例程指定为程序的起始地址,接着启动例程从内核中取出命令行参数和环境变量值,然后执行main函数. ...

  7. mysql 创建一个用户,指定一个数据库

    mysql 创建一个用户 hail,密码 hail,指定一个数据库 haildb 给 hail mysql -u root -p password use mysql; insert into use ...

  8. VDN For PB Web实现消息推送

    利用VesnData.Net(VDN)的互联网数据驱动功能我们实现了PB连接互联网数据库的功能.在互联网开发的过程中我们往往有些消息或者数据希望即时能够通知到各个客户端,现在比较流行的一种技术就是消息 ...

  9. @synthesize 有什么好处?

    如果不用 synthesize,操作的是 @property中定义的变量,使用synthesize之后,间接的操作了一个新的成员变量,到底有什么好处?直接只用一个@property不是更简单吗?

  10. Linux学习之路--启动VNC服务

    我的Linux是Fedora 13,安装方法如下: 1.打开终端,执行 # yum install -y tigervnc tigervnc-server 2.编辑/etc/sysconfi/vncs ...