2023天梯赛真题L2-3 锦标赛

建议在看过其他人思路后再看篇

本体考题考察二叉树,不需要繁琐的比较过程,直接暴力递归即可拿满25分

#include <bits/stdc++.h>
using namespace std;
struct Node{
	int win, lose;
}node[1000000];
int k;

bool solve(int num){
	if(node[num].win < node[num].lose){
		return false;
	}
	
	if(num > pow(2,k)-2){
		return true;
	}

	
	int ch1 = num*2 + 1, ch2 = ch1 + 1;

	//第一种放法
	node[ch1].win = node[num].win, node[ch2].win = node[num].lose; 
	if(solve(ch1) && solve(ch2))
		return true;
		
	//第二种放法,当第一种方法没能return true后才会执行
	swap(node[ch1].win, node[ch2].win);
	if(solve(ch1) && solve(ch2))
		return true;
	
	return false;
}


int main()
{

	cin >> k;
	for(int i=k-1;i>=0;i--){
		int t = pow(2,i);
		int num = t-1;
		while(t--){
			cin >> node[num].lose;
			num++;
		}
	}
	cin >> node[0].win;
	
	if(solve(0)){
		int num = pow(2,k-1)-1;
		int t = num+1;
		while(t>1){
			cout << node[num].lose << " " << node[num].win << " ";
			num++;
			t--;
		}
		cout << node[num].lose << " " << node[num].win << endl;
	}
	else{
		cout << "No Solution" << endl;
	}
	return 0;
}

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值