蓝桥杯试题——汉诺塔问题

蓝桥杯试题——汉诺塔问题

蓝桥杯试题——汉诺塔问题_第1张图片

#include
#include
int Step,x,y;
long long int ANS[6];
using namespace std;

int Between(){
	if(Step>=x&&Step<=y) return 1;
		else return 0;
}

void Count(int From,int End){
	if(From=='A'){
		if(End=='B') ANS[0]++;
		else if(End=='C') ANS[1]++;
	}
	else if(From=='B'){
		if(End=='A') ANS[2]++;
		else if(End=='C') ANS[3]++;
	}
	else if(From=='C'){
		if(End=='A') ANS[4]++;
		else if(End=='B') ANS[5]++;
	}
}

void Han(int n,char From,char To,char End) {
	if(n==1){
		Step++;
		if(Between()){
			Count(From,End);
		}
		return;
	}
	else{
		Han(n-1,From,End,To);
		Step++;
		if(Between()){
			Count(From,End);
		}
		Han(n-1,To,From,End);
	}
}

int main() {
	int n;
	cin>>n>>x>>y;
	memset(ANS,0,sizeof(int)*6);
	Step=0;
	char a='A',b='B',c='C';
	Han(n,a,b,c);
	for(int i=0;i<6;i++)
		cout<<ANS[i]<<endl;
	return 0;
}

你可能感兴趣的:(蓝桥杯)