Wannafly模拟赛3 贝伦卡斯泰露(搜索+剪枝)

本文介绍了一种基于搜索加剪枝策略的算法实现方法,通过递归地更新两个序列并适时剪枝来解决问题。文章提供了完整的代码示例,展示了如何在给定约束条件下寻找可行解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:点击打开链接

思路:搜索+剪枝

// 贝伦卡斯泰露 运行/限制:3ms/1000ms
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n;
int num[45], s1[45], s2[45];//s1表示序列1,s2为序列2
bool dfs(int cnt1, int cnt2, int dep) {
	if (dep > n) return true;//剪枝
	if (cnt1 > n / 2 || cnt2 > n / 2) return false;//剪枝
	if (num[dep] == s1[cnt2 + 1]) {//可以更新s2
		s2[cnt2 + 1] = num[dep];
		bool flag = dfs(cnt1, cnt2 + 1, dep + 1);
		if (flag) return true;//剪枝
	}
	//更新s2不满足,继续更新s1
	s1[cnt1 + 1] = num[dep];
	return dfs(cnt1 + 1, cnt2, dep + 1);
}
int main(){
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			scanf("%d", &num[i]);
		}
		s1[1] = num[1];
		if (dfs(1, 0, 2)) {
			printf("Frederica Bernkastel\n");
		}
		else {
			printf("Furude Rika\n");
		}
	}
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值