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. 线程操作API

    线程操作API 1.currentThread 2.getId() .getName().getPriority().getStart.isAlive().isDaemon().isInterrupt ...

  2. expr命令

    expr命令的兩大作用:1)四则运算:2)字符串的操作: 1.四则运算 [tough@localhost ~]$ + + [tough@localhost ~]$ + [tough@localhost ...

  3. poj 2153 Rank List

    原题链接:http://poj.org/problem?id=2153 简单题,map,平衡树均可.. map: #include<algorithm> #include<iostr ...

  4. ECMAScript5

    张鑫旭:ECMAScript5介绍 淘宝整理的es5-safe /********* It provides the following methods: Function.prototype.bin ...

  5. arm-elf-gcc交叉编译器的使用教程

    arm-elf-gcc交叉编译器的使用教程 一开始需要安装arm-elf-gcc,但是这是一个32位的程序,我是安装了64位的系统,据说安装ia32.libs依赖库能运行这个,但是看到博客上面前人安装 ...

  6. iOS 进阶 第二十一天(0531)

    0531 - Autolayout 不仅可以做屏幕适配还可以做系统适配 uidynamic 做物理动画.能做的效果如下图: Autolayout Autolayout 是一种“自动布局”技术,专门用来 ...

  7. 【ConnerStone】SVN代码管理 - 基本使用

    第一步,链接服务器,创建代码管理仓库 第二步,输入服务器的配置,链接服务器(例子是以svn:// 为例子) 第三部 ,链接成功后,SVN的基本界面组成 第四步 从仓库中check out你需要的项目 ...

  8. [Android Training视频系列] 8.3 Dealing with Audio Output Hardware

    用户在播放音乐的时候有多个选择,可以使用内置的扬声器,有线耳机或者是支持A2DP的蓝牙耳机.(补充:A2DP全名是Advanced Audio Distribution Profile 蓝牙音频传输模 ...

  9. 关于在 loadView 中改变状态栏的可视性

    这种问题不知道大家是否遇见过,在此用两句话(时间紧迫,还得加班)分享下今天犯的错误 我把状态栏的的可视性的改变写在了loadView 里面,然后就出现了调用了两次 loadView 和 viewDid ...

  10. AJAX三种返回值方式

    (一)TEXT方式 该方式返回的是拼接字符串,想要取到其中的值,需要先将返回值进行拆分 (二)JSON方式 该方式返回的是数组,想要取到其中的值,可用索引项进行提取 (三)XML方式 XML:可扩展标 ...