首页 技术 正文
技术 2022年11月19日
0 收藏 535 点赞 2,389 浏览 2539 个字

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)-]);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898