UVA - 11795 Mega Man's Mission

本文探讨了一款关于MegaMan的游戏策略问题,MegaMan的任务是摧毁由Dr.Wily制造企图征服世界的机器人。每消灭一个机器人,MegaMan就能获得其武器用于后续战斗。文章分析了不同机器人武器的有效性,并提出了一种算法来计算完成任务的所有可能方式。

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]);
    }
} 

 

转载于:https://www.cnblogs.com/renjianshige/p/7459390.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值