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 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的更多相关文章
- 状压DP UVA 11795 Mega Man's Mission
题目传送门 /* 题意:洛克人有武器可以消灭机器人,还可以从被摧毁的机器人手里得到武器,问消灭全部机器人的顺序总数 状态压缩DP:看到数据只有16,就应该想到状压(并没有).因为是照解题报告写的,代码 ...
- UVa 11795 Mega Man's Mission (状压DP)
题意:你最初只有一个武器,你需要按照一定的顺序消灭n个机器人(n<=16).每消灭一个机器人将会得到他的武器. 每个武器只能杀死特定的机器人.问可以消灭所有机器人的顺序方案总数. 析:dp[s] ...
- UVA 11795 七 Mega Man's Mission
七 Mega Man's Mission Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Subm ...
- UVa 11795 状压DP Mega Man's Mission
kill[S]表示消灭机器人的集合为S,剩下的所能杀死的机器人集合. 设d(S)表示杀死机器人集合为S的方法数,答案为d((1<<n) - 1). d(S)可以由d(S')转移过来,其中S ...
- UVA Mega Man's Mission(状压dp)
把消灭了那些机器人作为状态S,预处理出状态S下可以消灭的机器人,转移统计方案.方案数最多16!,要用64bit保存方案. #include<bits/stdc++.h> using nam ...
- UVA 11795
B Mega Man’s Missions Input Standard Input Output Standard Output Mega Man is off to save the world ...
- UVA11795 Mega Man's Mission
状压dp #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> ...
- 【状压DP】【UVA11795】 Mega Man's Mission
传送门 Description 你要杀n个怪,每杀掉一个怪那个怪会掉落一种武器,这种武器可以杀死特定的怪.游戏初始你有一把武器,能杀死一些怪物.每次只能杀一只,求有多少种杀怪方法. Input 多组数 ...
- UVA - 11795 状压DP
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...
随机推荐
- 【Offer】[32] 【从上到下打印二叉树】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 不换行:从上到下打印出二叉树的每个节点,同层的节点按照从左到右的顺序打印.例如,输入下图的二叉树,则依次打印出8,6,10,5,7,9, ...
- git 中文乱码-一次被坑经历
git log和gitcommit中文出现乱码,花了大半天的时间试了网上的各种方法,还是搞不定. 只好放大招. 卸载软件后重装,还是不行.然后git config --list 发现一些奇怪的配置信息 ...
- Can't connect to MySQL server on 'localhost' (10038)
平台:win7 已经安装好数据库,但是 在连接MySQL数据库时发生问题: 经过一系列的资料查找后,得到结论MySQL没有启动 解决方法: 将MySQL加入到Windows的服务中.切换到MySQL安 ...
- ios 各个手机型号尺寸及分辨率适配
常用宏 // 判断是否是ipad #define isPad ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdio ...
- composer 依赖的require安装与remove删除命令
安装:require composer require phpmailer/phpmailer 删除:remove composer remove phpmailer/phpmailer
- spring aop介绍和示例
参考:<Spring in Action> 一.AOP介绍 AOP是Aspect Oriented Programming的缩写,意思是面向切面编程. 应用中有一些功能使用非常普遍,比如事 ...
- [VB.NET Tips]Select Case语句拾遗
正常的Select 语句如下: Dim status As Integer = 5 Select Case status Case 0 Console.WriteLine("状态是:0&qu ...
- 将maven项目到入到idea中
一,前言 在文章将maven项目导入到eclipse中中我将新建的项目到入到了eclipse中了,因为最近也在尝试idea,那么就顺便也到入idea中. maven项目的话,我就使用在文章使用命令行创 ...
- 采用redis生成唯一且随机的订单号
项目描述 最近做的一个项目有这么一个需求:需要生成一个唯一的11位的就餐码(类似于订单号的概念),就餐码的规则是:一共是11位的数字,前面6位是日期比如2019年07月20就是190720,后面五位是 ...
- Java查询判断素数实验报告
实验源代码: package sushu; import java.util.Scanner; public class First { int size=2; int data[]=new int[ ...