+ Binary enumeration thinking off white cow May 23 A title race membrane record

Record membrane

Title Description

Recently playing a beef called "membrane record" of the game, the mechanism of this game is this:
in a game, all of the enemies are arranged in a grid of n rows and m columns, Taurus command of his magical girl to attack the enemy.
Attack There are two types: Line blast, columns blast
line blast can destroy the enemy a whole row, column blast can destroy the enemy a whole column of
beef in total capable of releasing a trip to blast, b minor columns blast
given the initial situation of a bureau of the game I ask whether beef will wipe out the enemy?

Enter a description:

The first line contains a positive integer T, denotes the number of the test data set, followed by a set of test data T
each test first row of data has four positive integers n, m, a, b
Then there are n rows, each row m is a string of characters of the i-th row j-th column is illustrated here * If there is an enemy, if Description there is no

Output Description:

For each set of test data output line, if they can destroy all the enemies on the output yes, otherwise no output


Let me talk about, solving the problem of data may be some problems; to consider a = 0;

The very thought of a question;

First thought may hold binary pieces, the i-th pre-fetch a binary number (i.e., a combination of lines), number of columns to eliminate these lines are eliminated, referred to as CNT [i]; the number of columns and m is the rest is -cnt [i];

How is the key to solving the problem preprocessing, first column from the enumeration of 1-m, and the position of the enemy lines where each column is processed, passed into a binary number; each binary number in this column line enemy arrangement; then CNT [i] represents the number of enemies are arranged in columns i;

There are treatments, such as: 11011 arrangement, we must take the equivalent of 1,2,4,5-line before they can destroy the enemy, but when we take 1.2.4.5 also put 11010,11000, etc. are eliminated, so it to find the number of permutations of a binary number 1 weight each. You should be able to understand this myself;

Code:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=100100;
const int M=2000100;
const LL mod=100000000;
int n,m,a,b,cnt[M];
char s[25][N];
int sum_1(int p){
    int ans=0;
    while(p){
        if(p%2) ans++;
        p/=2;
    }
    return ans;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        cin>>n>>m>>a>>b;
        for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
        for (int i = 0; i < (1<<n); i++) cnt[i] = 0;
        for(int j=1;j<=m;j++){
            int t=0;
            for(int i=1;i<=n;i++){
                if(s[i][j]=='*') t|=(1<<i-1);//t代表第i列有哪几行有敌人
            }
            cnt[t]++;//相同列的个数
        }
        for(int i=1;i<=n;i++){
            for(int j=0;j<=(1<<n)-1;j++){
                if((j&(1<<i-1))==0){//这个表示数j的第i个二进制位为0
                    cnt[j|(1<<i-1)]+=cnt[j];//表示第i个二进制位为1的数加上cnt[j]
                }
            }
        }
        bool ok=false;
        for(int i=0;i<(1<<n);i++){
            if(sum_1(i)<=a&&m-cnt[i]<=b) ok=true;
        }
        if(ok) cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }	
    return 0;
}
Published 264 original articles · won praise 46 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_44291254/article/details/105036705