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. C# 将汉字转化成拼音

    本文来自http://www.cnblogs.com/yazdao/archive/2011/06/04/2072488.html 首先下载Visual Studio International Pa ...

  2. java的基本数据类型特征

    java的数据类型分为基本数据类型和引用数据类型. 基本数据类型分为数值型.字符型(char).布尔型(boolean) 数值型变量 1.整数型 类型 占用存储空间 表示范围 byte 1字节Byte ...

  3. EXCLE使用宏生成目录

    宏代码: Sub mu() Dim i As Integer Dim ShtCount As Integer Dim SelectionCell As Range ShtCount = Workshe ...

  4. Mybatis 中常用的java类型与jdbc类型

    JDBC Type Java Type CHAR String VARCHAR String LONGVARCHAR String NUMERIC java.math.BigDecimal DECIM ...

  5. oracle自定义job名字,job调度

    一.调试创建 begin -- create_schedule dbms_scheduler.create_schedule(schedule_name => 's_change_send_da ...

  6. 安装MySQL的心得

    1.去官网上下载适合自己电脑的安装包,最好在网上查查教程起码知道自己应该怎么下载,下载哪一个. 2.我遇到的问题不多:<1>.没在bin目录下安装,启动数据库时出现错误2:<2> ...

  7. poj 2377 Bad Cowtractors

    题目连接 http://poj.org/problem?id=2377 Bad Cowtractors Description Bessie has been hired to build a che ...

  8. wire与reg的区别?转载大神!

    本文转自:http://www.cnblogs.com/thymon/archive/2010/06/09/1754541.html //------------------------------- ...

  9. 54.xilinx_modelsim仿真错误1

    在仿真DDR3核时,用modelsim编译时会出现下面错误 Error:can't read "env(XILINX)":no such variable 原因:在.do文件中指定 ...

  10. [收藏]Spring Security中的ACL

    ACL即访问控制列表(Access Controller List),它是用来做细粒度权限控制所用的一种权限模型.对ACL最简单的描述就是两个业务员,每个人只能查看操作自己签的合同,而不能看到对方的合 ...