建议在看过其他人思路后再看篇
本体考题考察二叉树,不需要繁琐的比较过程,直接暴力递归即可拿满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;
}