P1657选书-(dfs)

https://www.luogu.org/problemnew/show/P1657

解题:对于某个人喜欢的两本书,选或者是不选!

坑:数据有一组是0的,按dfs会出错,0本书选个屁,有啥意义?不给数据范围,出题不规范,选手两行泪。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

int n;
int a[25][2];
int book[25];
int ans=0;

void dfs(int x)///第x人
{
    if(x==n+1)///到了第n+1个人,表示已经有n人取过书了,结束
    {
        ans++;
        return;
    }

    if( book[ a[x][0] ] == 0  )///第一本
    {
        book [ a[x][0] ] = 1;
        dfs(x+1);
        book [ a[x][0] ] = 0;
    }
    if( book[  a[x][1] ] == 0 )///第二本
    {
        book [ a[x][1] ] = 1;
        dfs(x+1);
        book [ a[x][1] ] = 0;
    }
    return ;///如果没有符合要求的书进入递归,证明到目前为止的选书策略是错的
}


int main()
{
    memset(book,0,sizeof(book));
    scanf("%d",&n);
    ans=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d %d",&a[i][0],&a[i][1]);

    }
    if(n==1)
        ans=1;
    else if(n==0)
        ans=0;
    else
        dfs(1);///从第1个人开始选
    printf("%d
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/shoulinniao/p/11173749.html