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. DoubanFm之设计模式(一)

    前两版DoubanFm写的太戳,第一版可以忽略,当是熟悉WP手机的一些API.. 第二版用了比较多的依赖注入,熟悉了Messenger,过后越写越大,感觉不对,赶快打住..现在开始好好思考各模块了. ...

  2. html/css 盒子布局 Margin 、Padding 、border 以及 清除浮动的知识 (学习HTML过程中的小记录)

    html/css  盒子布局 Margin .Padding .border 以及 清除浮动的知识 (学习HTML过程中的小记录) 作者:王可利(Star·星星) width     是"宽 ...

  3. Huffman树的编码译码

    上个学期做的课程设计,关于Huffman树的编码译码. 要求: 输入Huffman树各个叶结点的字符和权值,建立Huffman树并执行编码操作 输入一行仅由01组成的电文字符串,根据建立的Huffma ...

  4. linux 下的使用 ln 创建 软链接 和 硬链接

    linux 下的一个指令 ln 作用: 创建软链接或者硬链接 Linux 系统下每创建一个文件,系统都会为此文件生成一个 index node 简称(inode) ,而每一个文件都包含用户数据(use ...

  5. Run ionic web app in nodejs

    首先需要express插件:sudo npm install express 将ionic project的www拷贝至wwwroot,新建server.js: var express = requi ...

  6. dede 忘记密码在数据库中修改方法

    如何找回或修改dedecms后台管理员登录密码呢? 一个客户把密码忘了,找了很长一会没几个靠谱的回答,dede是使用md5加密,但是,它是显示32位md5加密码从第6位开始的20位 方法是直接修改其m ...

  7. jquery 源码学习(一)

    从上边的注释看,jQuery的源码结构相当清晰.条理,不像代码那般晦涩和让人纠结   1. 总体架构 1.1 自调用匿名函数 self-invoking anonymous function 打开jQ ...

  8. 三种嵌入式web服务器(Boa / lighttpd / shttpd)的 linux移植笔记

    一:移植Boa(web服务器)到嵌入式Linux系统 一.Boa程序的移植 1.下载Boa源码    下载地址: http://www.boa.org/    目前最新发行版本: 0.94.13   ...

  9. Learning note for Binding and validation

    Summary of my learning note for WPF Binding Binding to DataSet. when we want to add new record, we s ...

  10. [shell基础]——tr命令

    (1) tr 字符替换 测试文本内容 # cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4. ...