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[1<<MAXN],can[1<<MAXN],wu[MAXN],n;
char a[500];

void cl(){
    memset(dp,0,sizeof(dp));
    memset(can,0,sizeof(can));
    memset(wu,0,sizeof(wu));
}

int main(){
    int t;scanf("%d",&t);
    for(int cas=1;cas<=t;cas++){
        cl();
        scanf("%d",&n);
        getchar();
        for(int i=0;i<n;i++){
            char x=getchar();
            x-=‘0‘;
            can[0]|=x*(1<<i);
        }
        for(int i=0;i<n;i++){
            getchar();
            for(int j=0;j<n;j++){
                char x=getchar();
                x-=‘0‘;
                wu[i]|=x*(1<<j);
            }
        }
        for(int i=0;i<(1<<n);i++){
            can[i]=can[0];
            for(int j=0;j<n;j++) if(i&(1<<j)) can[i]|=wu[j];
        }
        dp[0]=1;
        for(int s=0;s<(1<<n);s++){
            for(int i=0;i<n;i++) if(s&(1<<i)&&(can[s^(1<<i)]&(1<<i))) dp[s]+=dp[s^(1<<i)];
        }
        printf("Case %d: %lld\n",cas,dp[(1<<n)-1]);
    }
} 

UVA - 11795 Mega Man's Mission

时间: 2024-10-06 13:50:54

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

状压DP UVA 11795 Mega Man&#39;s Mission

题目传送门 1 /* 2 题意:洛克人有武器可以消灭机器人,还可以从被摧毁的机器人手里得到武器,问消灭全部机器人的顺序总数 3 状态压缩DP:看到数据只有16,就应该想到状压(并没有).因为是照解题报告写的,代码里加点注释,省的以后忘记了 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-8 10:41:28 8 * File Nam

uva 11795 Mega Man&#39;s Mission 状压dp

// uva 11795 Mega Man's Mission 状压dp // 设r[i]表示第i个机器人所拥有的武器的数目 // r[0]表示初始时洛克人所拥有的武器数 // w[s]表示杀死集合s中的机器人后所得的武器数 // d[s]表示能杀死集合s中的机器人的顺序总数 // d[s] = sigma(d[s-{i}]) 其中i是集合s中的机器人 // 还有一点就是w[S-{i}]的武器可以杀死i. // 注意: // 1)初始的时候d[0]=1,其他均为0.这个很好理解,因为杀死 //

UVA 11795 七 Mega Man&#39;s Mission

七 Mega Man's Mission Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 11795 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 6 long long dp[65

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 particu

UVa11795 Mega Man&#39;s Mission(状态压缩DP)

Mega Man's Mission Mega Man is off to save theworld again. His objective is to kill the Robots created by Dr.Wily whose motive is to conquer the world. In each mission, he willtry to destroy a particular Robot. Initially, Mega Man is equippedwith a w

UVA11795 Mega Man&#39;s Mission

状压dp #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<vector> #include<queue> #define MAXN 20 #define ll long long using namespace std; int n; vector<int> G[MAXN]; ll f[1<<MA

【UVA11795】 Mega Man&#39;s Mission

题面 你要杀n个怪,每杀掉一个怪那个怪会掉落一种武器,这种武器可以杀死特定的怪.游戏初始你有一把武器,能杀死一些怪物.每次只能杀一只,求有多少种杀怪方法.n≤16 分析 f[i]的i的二进制表示每个怪物是否被杀死,f[i]的值表示目前这种状态的方案数.最后答案为f[(1<<n)-1]同时用s[i]表示状态为i的怪已经被杀了后得到的武器能杀死哪些怪.转移:如果这些怪在i状态时未被杀且i状态的武器能够杀死这个怪,就把这些怪杀了 代码 #include<bits/stdc++.h> us

uva 11795(状态压缩dp)

题意:有一个机器人要杀死其他n(1 <= n <= 16)个机器人,他自己配备了一个武器,并且给出了这个武器能杀死的敌人,如101表示他能杀死第1和第3个敌人,这样他就能得到第1和第3号敌人的武器,从而杀死更多的敌人,问他杀死所有的敌人的方法数. 题解:因为敌人数最多是16个,很容易想到应该是状态压缩dp,可以用f[S]表示状态S下进制位为1的敌人全被杀死的方案数,那么结果就是f[(1 << n) - 1],如果拿掉了状态S中的杀死第j个人能得到的武器,剩下的人如果能杀死第j个人,

训练指南DP阶段训练1

最近又忙又颓.............时间抓不紧....下学期开始就要准备考研了.......就2个月左右可以做自己喜欢的事了....争取把紫书和白书没做的,做过的..来一次完整的总结 训练指南上面的5个例题+后面15个习题是第一阶段 vjudge训练地址 http://vjudge.net/contest/139533#overview -------------------------------------------------------------------------------