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 j-th character of Row 0 will be ‘1’ if the “Mega Buster” can destroy the j-th Robot. For the remaining N rows, the j-th character of i-th row will be ‘1’ if the weapon of i-th Robot can destroy the j-th 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

题解:

水题,本来不想做的,发现有大佬不用记忆化搜索写而用集合的写法写这个东西,就学着写了一下,设dp[S],表示打到S这个集合所有机器人的方案数,转移就是枚举一个机器人,如果可以打,就打就是了。

代码:

#include<iostream>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
#include<stdio.h>
#define MAXN 17
#define ll long long
using namespace std;
ll dp[<<MAXN],can[<<MAXN],wu[MAXN],n;
char a[]; void cl(){
memset(dp,,sizeof(dp));
memset(can,,sizeof(can));
memset(wu,,sizeof(wu));
} int main(){
int t;scanf("%d",&t);
for(int cas=;cas<=t;cas++){
cl();
scanf("%d",&n);
getchar();
for(int i=;i<n;i++){
char x=getchar();
x-='';
can[]|=x*(<<i);
}
for(int i=;i<n;i++){
getchar();
for(int j=;j<n;j++){
char x=getchar();
x-='';
wu[i]|=x*(<<j);
}
}
for(int i=;i<(<<n);i++){
can[i]=can[];
for(int j=;j<n;j++) if(i&(<<j)) can[i]|=wu[j];
}
dp[]=;
for(int s=;s<(<<n);s++){
for(int i=;i<n;i++) if(s&(<<i)&&(can[s^(<<i)]&(<<i))) dp[s]+=dp[s^(<<i)];
}
printf("Case %d: %lld\n",cas,dp[(<<n)-]);
}
}

UVA - 11795 Mega Man's Mission的更多相关文章

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

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

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

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

  3. UVA 11795 七 Mega Man's Mission

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

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

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

  5. UVA Mega Man's Mission(状压dp)

    把消灭了那些机器人作为状态S,预处理出状态S下可以消灭的机器人,转移统计方案.方案数最多16!,要用64bit保存方案. #include<bits/stdc++.h> using nam ...

  6. UVA 11795

    B Mega Man’s Missions Input Standard Input Output Standard Output Mega Man is off to save the world ...

  7. UVA11795 Mega Man's Mission

    状压dp #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> ...

  8. 【状压DP】【UVA11795】 Mega Man's Mission

    传送门 Description 你要杀n个怪,每杀掉一个怪那个怪会掉落一种武器,这种武器可以杀死特定的怪.游戏初始你有一把武器,能杀死一些怪物.每次只能杀一只,求有多少种杀怪方法. Input 多组数 ...

  9. UVA - 11795 状压DP

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

随机推荐

  1. Java 调式、热部署、JVM 背后的支持者 Java Agent

    我们平时写 Java Agent 的机会确实不多,也可以说几乎用不着.但其实我们一直在用它,而且接触的机会非常多.下面这些技术都使用了 Java Agent 技术,看一下你就知道为什么了. -各个 J ...

  2. 【LeetCode】215-数组中的第K个最大元素

    题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 ...

  3. Ubuntu系统添加用户权限

    一.首先创建一个新用户: sudo adduser hadoop 其次设置密码: sudo passwd hadoop 如果无法使用root密码,请输入如下命令: sudo passwd root 二 ...

  4. 可见性有序性,Happens-before来搞定

    写在前面 上一篇文章并发 Bug 之源有三,请睁大眼睛看清它们 谈到了可见性/原子性/有序性三个问题,这些问题通常违背我们的直觉和思考模式,也就导致了很多并发 Bug 为了解决 CPU,内存,IO 的 ...

  5. ASP.NET Core 2.2 : 二十一. 内容协商与自定义IActionResult和格式化类

    上一章的结尾留下了一个问题:同样是ObjectResult,在执行的时候又是如何被转换成string和JSON两种格式的呢? 本章来解答这个问题,这里涉及到一个名词:“内容协商”.除了这个,本章将通过 ...

  6. java 手机号码+邮箱的验证

    import java.util.regex.Pattern; //导入的包 1:String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9]) ...

  7. mysql简易导入excel

    方法-:利用excel本身的命令实现: 1 将excel文件中的数据转换成sql文件 (1)如图所示,我们在excel中执行如下语句 =CONCATENATE(“insert into table_n ...

  8. SQL手工注入进阶篇

    0.前言 上一篇我们介绍了SQL手工注入的流程以及步骤,但在实际的安全问题以及CTF题目中,查询语句多种多样,而且是肯定会对用户的输入进行一个安全过滤的,而这些过滤并不一定是百分百安全的,如何利用一些 ...

  9. 有关Spring事务,看这一篇就足够了

    本文将按照声明式事务的五个特性进行介绍: 事务传播机制 事务隔离机制 只读 事务超时 回滚规则 Spring事务传播机制 事务的特性 原子性(Atomicity):事务是一个原子操作,由一系列动作组成 ...

  10. Java匹马行天下之C国程序员的秃头原因

    Java帝国的崛起 前言: 分享技术之前先请允许我分享一下黄永玉老先生说过的话:“明确的爱,直接的厌恶,真诚的喜欢.站在太阳下的坦荡,大声无愧地称赞自己.” <编程常识知多少> <走 ...