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(T≤50), the number of test cases.
Each test case starts with an integer N(1≤N≤16). 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的更多相关文章
- UVA 11795 七 Mega Man's Mission
七 Mega Man's Mission Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Subm ...
- 状压DP UVA 11795 Mega Man's Mission
题目传送门 /* 题意:洛克人有武器可以消灭机器人,还可以从被摧毁的机器人手里得到武器,问消灭全部机器人的顺序总数 状态压缩DP:看到数据只有16,就应该想到状压(并没有).因为是照解题报告写的,代码 ...
- UVA - 11795 状压DP
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...
- UVa 11795 Mega Man's Mission (状压DP)
题意:你最初只有一个武器,你需要按照一定的顺序消灭n个机器人(n<=16).每消灭一个机器人将会得到他的武器. 每个武器只能杀死特定的机器人.问可以消灭所有机器人的顺序方案总数. 析:dp[s] ...
- UVa 11795 状压DP Mega Man's Mission
kill[S]表示消灭机器人的集合为S,剩下的所能杀死的机器人集合. 设d(S)表示杀死机器人集合为S的方法数,答案为d((1<<n) - 1). d(S)可以由d(S')转移过来,其中S ...
- 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 ...
- DP专题(不定期更新)
1.UVa 11584 Partitioning by Palindromes(字符串区间dp) 题意:给出一个字符串,划分为若干字串,保证每个字串都是回文串,同时划分数目最小. 思路:dp[i]表示 ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
随机推荐
- hdu 5131 Song Jiang's rank list
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5131 Song Jiang's rank list Description <Shui Hu Z ...
- JavaScript高级程序设计之元素大小
1.偏移量 // 元素相对于文档的偏移量 var getOffSet = function (ele) { var actualLeft = ele.offsetLeft, // 相对于offsetP ...
- 禁用DropDownList的Items
可以先看看效果,被禁用的DropDownList的Items已经无法选取与颜色也变为灰色. 刚开始,DropDownList没有接收从网址传来的参数时,它每一个Item都是可以选取的,一旦接收了值.所 ...
- How to Notify Command to evaluate in mvvmlight
How to Raize Command to evalituate in mvvm In mvvmlight, we bind our control to the relaycommand obj ...
- Linux Centos 6.6搭建SFTP服务器
Linux Centos 6.6搭建SFTP服务器 在Centos 6.6环境使用系统自带的internal-sftp搭建SFTP服务器. 打开命令终端窗口,按以下步骤操作. 0.查看openssh的 ...
- 我的VS2013中,用Ado.net给SQLParameter赋值的时候,当赋值null的时候,生成的sql语句是default
/// <summary> /// 增加一条数据 /// </summary> public bool Add(Model.WechatDocuments model) { S ...
- ASP.NET中Global.asax 文件是什么?
Global.asax 文件,有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法.你可以使用这个文件实现应用程序安全性以及其它一些任务.下面让我们详细看 ...
- Objective-C面向对象(四)
1.协议(protocol)和委托 1.1 规范.协议与接口 OC中协议的作用就相当于其他语言中接口的作用.协议定义的是多个类共同的公共行为规范,协议通常定义一组公用方法,但不提供实现. 1.2 定义 ...
- 子网划分与CIDR(斜杠加数字的表示与IP 的关系)(改进)
子网和CIDR 将常规的子网掩码转换为二进制,将发现子网掩格式为连续的二进制1跟连续0,其中子网掩码中为1的部份表示网络ID,子网掩中为0的表示主机ID.比如255.255.0.0转换为二进制为1 ...
- cnblog评价以及团队软件的部分改善
博客评价: 1.在word2003版本里的东西复制,不能直接直接粘贴到博客发表(发生过,大部分时候可以): 2.第一次使用的时候不知道复制过来的代码都是左对齐的,(代码排版和插入图片位置不明显): 3 ...