题--dfs

62 篇文章 3 订阅

这里写图片描述
这里写图片描述

#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <vector>
using namespace std;

#define min(a, b) (a) < (b) ? (a) : (b)
#define max(a, b) (a) > (b) ? (a) : (b)

int ans = 0;

void dfs(int cnt, const vector<int> &v, const vector<vector<string> > &str, int n, int m, int l, int s, map<string, bool> map1, int dep, int nandu, int max1, int min1)
{
    if(cnt == m)
    {
		if(max1 - min1 >= s && nandu <= l)
			ans++;
        return;
    }

	if(dep >= n)
		return;

    for(int i=dep; i<n; i++)
    {
        bool ok = true;
		int j = 0;
        for(j=0; j<str[i].size(); j++)
        {
            if(map1[str[i][j]])
            {
                ok = false;
                break;
            }
            map1[str[i][j]] = true;
        }
        if(ok && nandu+v[i] <= l)
        {
            int premin1 = min1;
            int premax1 = max1;
            
            max1 = max(max1, v[i]);
            min1 = min(min1, v[i]);
            
            dfs(cnt+1, v, str, n, m, l, s, map1, i+1, nandu+v[i], max1, min1);
            
            min1 = premin1;
            max1 = premax1;
        }
        for(j=0; j<str[i].size(); j++)
        {
            map1[str[i][j]] = false;
        }
    }
}

int main() 
{
	//ifstream cin;
	//cin.open("3.txt");
    int t;
    cin >> t;
    while(t--)
    {
        ans = 0;
        int n, m, l, s;
        cin >> n >> m >> l >> s;
        vector<int> v(n);
        vector<vector<string> > str(n);
        string tmp;
        int mm;
        for(int i=0; i<n; i++)
        {
            cin >> v[i];
            cin >> mm;
            for(int j=0; j<mm; j++)
            {
                cin >> tmp;
                str[i].push_back(tmp);
            }
        }
        vector<int> tans;
        map<string, bool> map1;
        dfs(0, v, str, n, m, l, s, map1, 0, 0, v[0], v[0]);
        cout << ans << endl;
    }
    return 0;
}
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <vector>
using namespace std;

#define min(a, b) (a) < (b) ? (a) : (b)
#define max(a, b) (a) > (b) ? (a) : (b)

int ans = 0;

void dfs(int cnt, const vector<int> &v, const vector<vector<string> > &str, int n, int m, int l, int s, map<string, bool> map1, int dep, int nandu, int max1, int min1)
{
    if(cnt == m)
    {
        if(max1 - min1 >= s && nandu <= l)
            ans++;
        return;
    }
	
    if(dep >= n)
        return;
	
	dfs(cnt, v, str, n, m, l, s, map1, dep+1, nandu, max1, min1);

	bool ok = true;
	int j;
	for(j=0; j<str[dep].size(); j++)
	{
		if(map1[str[dep][j]])
		{
			ok = false;
			break;
		}
		map1[str[dep][j]] = true;
	}
	
	if(ok && nandu+v[dep] <= l)
	{
		int premin1 = min1;
		int premax1 = max1;
		
		max1 = max(max1, v[dep]);
		min1 = min(min1, v[dep]);
		
		dfs(cnt+1, v, str, n, m, l, s, map1, dep+1, nandu+v[dep], max1, min1);
		
		min1 = premin1;
		max1 = premax1;
	}
	
	for(j=0; j<str[dep].size(); j++)
	{
		map1[str[dep][j]] = false;
	}
}

int main() 
{
    //ifstream cin;
    //cin.open("3.txt");
    int t;
    cin >> t;
    while(t--)
    {
        ans = 0;
        int n, m, l, s;
        cin >> n >> m >> l >> s;
        vector<int> v(n);
        vector<vector<string> > str(n);
        string tmp;
        int mm;
        for(int i=0; i<n; i++)
        {
            cin >> v[i];
            cin >> mm;
            for(int j=0; j<mm; j++)
            {
                cin >> tmp;
                str[i].push_back(tmp);
            }
        }
        vector<int> tans;
        map<string, bool> map1;
        dfs(0, v, str, n, m, l, s, map1, 0, 0, v[0], v[0]);
        cout << ans << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值