AtCoder Beginner Contest 173 English C - H and V 二进制枚举

题目链接:AtCoder Beginner Contest 173 English C - H and V

因为我不会而且还是看了队友的代码才明白有二进制有这种所以发个文章记录一下错题

Problem Statement

We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1≤i≤H,1≤j≤W) is given to you as a character ci,j: the square is white if ci, is., and black if ci,j is #.

Consider doing the following operation:
Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.

You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.

Constraints

1≤H,W≤6
1≤K≤HW
ci,j is . or #.

题意:

在h*w的矩阵中含有‘.’和‘#’分别代表白色和黑色格子,现在你可以选择某一行或者某一列让格子变成红色,求有多少种可能使得染成红色后,还恰好剩下k个黑色格子。

题解:

for(int i=0;i<(1< for(int j=0;j<(1< 二进制每次+1就可以暴力遍历每种情况出现的可能性

#include 
using namespace std;
#define ll long long
#define endl "\n"
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n,m,k;
    cin>>n>>m>>k;
    int ans=0;
    bool ar[n][m];
    for(int i=0;i<n;i++)for(int j=0;j<m;j++){
        char x;cin>>x;
        ar[i][j]= x == '#';
    }
    for(int i=0;i<(1<<n);i++){
        for(int j=0;j<(1<<m);j++){
            int cnt=0;
            for(int x=0;x<n;x++){
                for(int y=0;y<m;y++){
                    if(ar[x][y]&&(i&(1<<x))&&(j&(1<<y)))cnt++;
                }
            }
            if(cnt==k)ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
} 

你可能感兴趣的:(AtCoder)