dfs简单题

A - Triangle

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// #define ios ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int N = 1e6 + 10;
int a[4];
int main()
{
	IOS
	for(int i=0;i<4;i++) cin>>a[i];
	sort(a,a+4);
	if(a[0]+a[1]>a[2]||a[1]+a[2]>a[3]) cout<<"TRIANGLE"<<endl;
	else if(a[0]+a[1]==a[2]||a[1]+a[2]==a[3]) cout<<"SEGMENT"<<endl;
	else cout<<"IMPOSSIBLE"<<endl;
	return 0;
}

B - Far Relative’s Birthday Cake

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// #define ios ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int N = 1e2 + 10;
char a[N][N];
int main()
{
	IOS
	int n;
	ll sum = 0;
	cin >> n;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cin >> a[i][j];
	for (int i = 1; i <= n; i++)
	{
		int c = 0;
		for (int j = 1; j <= n; j++)
			if (a[i][j] == 'C') c++;
		sum += c * (c - 1) / 2;
	}
	for (int j = 1; j <= n; j++)
	{
		int c = 0;
		for (int i = 1; i <= n; i++)
			if (a[i][j] == 'C') c++;
		sum += c * (c - 1) / 2;
	}
	cout << sum << endl;
	return 0;
}

C - 枚举元组

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// #define ios ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int N = 1e2 + 10;
int n, a[10], k;
void dfs(int s) {
	if (s == n) {
		for (int i = 1; i <= n; i++)
			cout << a[i] << " ";
		cout << endl;
		return ;
	}
	for (int i = 1; i <= k; i++) {
		a[s + 1] = i;
		dfs(s + 1);
	}
}
int main() {
	cin >> n >> k;
	dfs(0);
	return 0;
}

D - 枚举子集

 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// #define ios ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int N = 1e2 + 10;
int n, a[15], k;
void dfs(int s) {
	if (s == n) {
		for (int i = 1; i <= n; i++)
			a[i] ? cout << 'Y' : cout << 'N';
		cout << endl;
		return ;
	}
	for (int i = 0; i <= 1; i++) {
		a[s + 1] = i;
		dfs(s + 1);
	}
}
int main() {
	IOS
	cin >> n;
	dfs(0);
	return 0;
}

E - 寻找团伙

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
ull p[30];
int n, k;

void input() {
    cin >> n >> k;

    for(int i=0; i<n; i++) {
        int c, x;
        cin >> c;

        while(c--) {
            cin >> x;
            p[i] |= (1ULL << (k - x));   // 人士 i 拥有 x 能力,修改对应 bit
        }
    }
}

int choice[30];
ull ans;

void dfs(int pos) {
    if(pos == n) {
        ull res = 0;
        for(int i=0; i<n; i++)
            if(choice[i])
                res ^= p[i];
        
        ans = max(res, ans);
        return;
    }

    choice[pos] = 0;
    dfs(pos + 1);
    choice[pos] = 1;
    dfs(pos + 1);
}

void work() {
    dfs(0);
    cout << ans << endl;
}

int main() {
    input();
    work();

    return 0;
}

F - 枚举排列

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// #define ios ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int N = 1e2 + 10;
int n, a[10], k;
bool b[10];
void dfs(int s) {
	if (s == n) {
		for (int i = 1; i <= n; i++)
			cout << a[i] << " ";
		cout << endl;
		return ;
	}
	for (int i = 1; i <= k; i++) {
		if(!b[i]){
			a[s + 1] = i;
			b[i]=true;
			dfs(s + 1);
			b[i]=false;
		}
	}
}
int main() {
	IOS
	cin >> k >> n;
	dfs(0);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值