题意:求所有符合题目要求的真假钥匙的总数
题解:先看数据范围 N<=15 ,M<=100 ,数据不大,直接暴力枚举 2^N 种情况, 然后对每组测试进行核验,当每组测试都通过时, 这组数据符合要求。
代码
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 9;
//开全局方便后面使用
int c[20], a[105][20];
int key[20];//记录钥匙的真假情况
char r[105];
int ans = 0;
int n, m, k;
void dfs(int dep)
{
if (dep > n) return;
//枚举当第dep个钥匙为假的情况
key[dep] = 0;
if (dep == n)
{
int f = 1;
//对所有测试进行核验
for (int i = 1; i <= m; i++)
{
int cnt = 0;
for (int j = 1; j <= c[i]; j++)
{
cnt += key[a[i][j]];
}
if ((cnt < k && r[i] == 'o') || (cnt >= k && r[i] == 'x'))
{
f = 0;
break;
}
}
if (f) ans++;
//return;
}
dfs(dep + 1);
//枚举当第dep个钥匙为真的情况
key[dep] = 1;
if (dep == n)
{
//对所有测试进行核验
int f = 1;
for (int i = 1; i <= m; i++)
{
int cnt = 0;
for (int j = 1; j <= c[i]; j++)
{
cnt += key[a[i][j]];
}
if ((cnt < k && r[i] == 'o') || (cnt >= k && r[i] == 'x'))
{
f = 0;
break;
}
}
if (f) ans++;
return;
}
dfs(dep + 1);
}
void solve()
{
cin >> n >> m >> k;
for (int i = 1; i <= m; i++)
{
cin >> c[i];
for (int j = 1; j <= c[i]; j++)
{
cin >> a[i][j];
}
cin >> r[i];
}
dfs(1);
cout << ans << '\n';
return;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int _ = 1;
//cin >> _;
while (_--) solve();
return 0;
}
标签:dep,return,Keys,dfs,int,key,ans
From: https://www.cnblogs.com/jin1/p/18627796