uva 254 - Towers of Hanoi(递归)

232 篇文章 0 订阅
179 篇文章 0 订阅

题目链接:uva 254 - Towers of Hanoi


题目大意:给出n和k,n的范围为0~100, k的范围为0~2^n - 1, 然后模拟汉诺塔游戏移动n个碟子,按照最优的游戏策略,移动了k次后,三个柱子上各有多少个碟子。


注意:如果盘子的总数为奇数,那么移动的目标最终目标位B号柱,否则为C号柱(题目要按照某种序列移动碟子)。


解题思路:递归的思想,每次处理好说当前移动碟子所在的柱子,目标柱子以及中间辅助的柱子,然后枚举当前的步数够移动几个碟子。k很大,用double和long long处理不了,就写了大数。


#include <stdio.h>
#include <string.h>
#include <math.h>
#define max(a,b) (a)>(b)?(a):(b)
const int N = 1005;

struct bign {
	int s[N];
	bign () {
		memset(s, 0, sizeof(s));
	}
	bign (int number) {*this = number;}
	bign (const char* number) {*this = number;}

	void put();
	void del();

	bign operator = (char *num) {
		s[0] = strlen(num);
		for (int i = 1; i <= s[0]; i++)
			s[i] = num[s[0] - i] - '0';
		return *this;
	}

	bign operator = (int num) {
		char str[N];
		sprintf(str, "%d", num);
		return *this = str;
	}

	bool operator < (const bign& b) const {
		if (s[0] != b.s[0])
			return s[0] < b.s[0];
		for (int i = s[0]; i; i--)
			if (s[i] != b.s[i])
				return s[i] < b.s[i];
		return false;
	}
	bool operator > (const bign& b) const { return b < *this; }
	bool operator <= (const bign& b) const { return !(b < *this); }
	bool operator >= (const bign& b) const { return !(*this < b); }
	bool operator != (const bign& b) const { return b < *this || *this < b;}
	bool operator == (const bign& b) const { return !(b != *this); }

	bign operator + (const bign& c) {
		int sum = 0;
		bign ans;
		ans.s[0] = max(s[0], c.s[0]);

		for (int i = 1; i <= ans.s[0]; i++) {
			if (i <= s[0]) sum += s[i];
			if (i <= c.s[0]) sum += c.s[i];
			ans.s[i] = sum % 10;
			sum /= 10;
		}

		while (sum) {
			ans.s[++ans.s[0]] = sum % 10;
			sum /= 10;
		}
		return ans;
	}

	bign operator - (bign& c) {
		bign ans = *this;
		for (int i = 1; i <= c.s[0]; i++) {
			if (ans.s[i] < c.s[i]) {
				ans.s[i] += 10;
				ans.s[i + 1] -= 1;
			}
			ans.s[i] -= c.s[i];
		}

		ans.del();
		return ans;
	}
};

bign cnt[105], tmp = 1, over = 0;

void init() {
	cnt[0] = 0;
	for (int i = 1; i <= 100; i++)
		cnt[i] = cnt[i - 1] + cnt[i - 1] + tmp;
}

void hanoi(int n, bign k, int& a, int& b, int& c) {
	if (k == over) {
		a += n;
		return;
	}

	for (int i = 1; i <= n; i++) {
		if (cnt[i] == k) {
			a += n - i;
			if (n % 2 == i % 2)
				c += i;
			else
				b += i;
			return;
		} else if (cnt[i] > k) {
			a += n - i;
			if (n % 2 == i % 2) {
				c += 1;
				hanoi(i - 1, k - cnt[i - 1] - tmp, b, a, c);
			} else {
				b += 1;
				hanoi(i - 1, k - cnt[i - 1] - tmp, c, a, b);
			}
			return;	
		}
	}
}

int main () {
	init();
	int n, a, b, c;
	bign k;
	char str[N];
	while (scanf("%d%s", &n, str), n || strcmp(str, "0")) {
		a = b = c = 0;
		k = str;

		if (n % 2)
			hanoi(n, k, a, c, b);
		else
			hanoi(n, k, a, b, c);
		printf("%d %d %d\n", a, b, c);
	}
	return 0;
}

void bign::put() {
	if (s[0] == 0)
		printf("0");
	else
		for (int i = s[0]; i; i--)
			printf("%d", s[i]);
}

void bign::del() {
	while (s[s[0]] == 0) {
		s[0]--;
		if (s[0] == 0) break;
	}
	if (s[0] == 0) {
		s[0] = 1;
		s[1] = 0;
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值