UVA 254 - Towers of Hanoi(递归)

 Towers of Hanoi 

In 1883, Edouard Lucas invented, or perhaps reinvented, one of the most popular puzzles of all times - the Tower of Hanoi, as he called it - which is still used today in many computer science textbooks to demonstrate how to write a recursive algorithm or program. First of all, we will make a list of the rules of the puzzle:

  • There are three pegs: AB and C.
  • There are n disks. The number n is constant while working the puzzle.
  • All disks are different in size.
  • The disks are initially stacked on peg A so that they increase in size from the top to the bottom.
  • The goal of the puzzle is to transfer the entire tower from the A peg to one of the others pegs.
  • One disk at a time can be moved from the top of a stack either to an empty peg or to a peg with a larger disk than itself on the top of its stack.

A good way to get a feeling for the puzzle is to write a program which will show a copy of the puzzle on the screen and let you simulate moving the disks around. The next step could be to write a program for solving the puzzle in a efficient way. You don't have to do neither, but only know the actual situation after a given number of moves by using a determinate algorithm.

The Algorithm

It is well known and rather easy to prove that the minimum number of moves needed to complete the puzzle with n disks is  . A simple algorithm which allows us to reach this optimum is as follows: for odd moves, take the smallest disk (number 1) from the peg where it lies to the next one in the circular sequence  ; for even moves, make the only possible move not involving disk 1.

Input

The input file will consist of a series of lines. Each line will contain two integers nmn, lying within the range [0,100], will denote the number of disks and m, belonging to [0,  ], will be the number of the last move. The file will end at a line formed by two zeros.

Output

The output will consist again of a series of lines, one for each line of the input. Each of them will be formed by three integers indicating the number of disks in the pegs AB and C respectively, when using the algorithm described above.

Sample Input

3 5
64 2
8 45
0 0

Sample Output

1 1 1
62 1 1
4 2 2

题意:n层的汉诺塔,问第k步时每个柱子有几个碟子。

思路:递归。每次从最大的碟子考虑,假设第n个碟子, 如果步数k大于2 ^ (n - 1) 那么是可以把这个碟子移到目标柱子的,然后剩下的碟子移到另一根柱子,步数剩下k - 2^(n - 1);

如果不够,那么这个最大的碟子肯定没移动,留在了原处,那么剩下的碟子下次要移到的位置为在非目标柱子和目标柱子间交替。

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)

const int MAXSIZE = 10000;

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

    void put();
    bign mul(int d);
    void del();
    void init() { memset(s, 0, sizeof(s)); }

    bign operator =  (char *num);
    bign operator =  (int num);

    bool operator <  (const bign& b) const;
    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);
    bign operator * (const bign& c);
    bign operator - (const bign& c);
    int  operator / (const bign& c);
    bign operator / (int k);
    bign operator % (const bign &c);
    int  operator % (int k);
    void operator ++ ();
    bool operator -- ();
};

int n;
char M[105];
int ans[4];
bign jie[105];
bign m, mul, zero;

void init() {
    memset(ans, 0, sizeof(ans));
    m = M; mul = jie[n];
}

void solve(int n, int num, int v) {
    if (n == 0)
	return;
    mul = mul / 2;
    if (m >= mul) {
	m = m - mul;
	ans[v] ++;
	solve(n - 1, 6 - num - v, v);
    }
    else {
	ans[num] ++;
	solve(n - 1, num, 6 - num - v);
    }
}

int main() {
    jie[0] = 1;
    for (int i = 1; i <= 100; i ++) {
	jie[i] = (jie[i - 1] + jie[i - 1]);
    }
    while (~scanf("%d%s", &n, M)) {
	if (n == 0 && strcmp(M, "0") == 0) break;
	init();
	solve(n, 1, (n % 2 ? 2 : 3));
	printf("%d %d %d\n", ans[1], ans[2], ans[3]);
    }
    return 0;
}

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

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

bool bign::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;
}

bign 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 bign::operator * (const bign& c) {
    bign ans;
    ans.s[0] = 0; 

    for (int i = 1; i <= c.s[0]; i++){  
	int g = 0;  

	for (int j = 1; j <= s[0]; j++){  
	    int x = s[j] * c.s[i] + g + ans.s[i + j - 1];  
	    ans.s[i + j - 1] = x % 10;  
	    g = x / 10;  
	}  
	int t = i + s[0] - 1;

	while (g){  
	    ++t;
	    g += ans.s[t];
	    ans.s[t] = g % 10;
	    g = g / 10;  
	}  

	ans.s[0] = max(ans.s[0], t);
    }  
    ans.del();
    return ans;
}

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

    for (i = 1; i <= ans.s[0]; i++) {
	if (ans.s[i] < 0) {
	    ans.s[i] += 10;
	    ans.s[i + 1]--;
	}
    }

    ans.del();
    return ans;
}

int bign::operator / (const bign& c) {
    int ans = 0;
    bign d = *this;
    while (d >= c) {
	d = d - c;
	ans++;
    }
    return ans;
}

bign bign::operator / (int k) {
    bign ans; 
    ans.s[0] = s[0];
    int num = 0;  
    for (int i = s[0]; i; i--) {  
	num = num * 10 + s[i];  
	ans.s[i] = num / k;  
	num = num % k;  
    }  
    ans.del();
    return ans;
}

int bign:: operator % (int k){  
    int sum = 0;  
    for (int i = s[0]; i; i--){  
	sum = sum * 10 + s[i];  
	sum = sum % k;  
    }  
    return sum;  
} 

bign bign::operator % (const bign &c) {
    bign now = *this;
    while (now >= c) {
	now = now - c;
	now.del();
    }
    return now;
}

void bign::operator ++ () {
    s[1]++;
    for (int i = 1; s[i] == 10; i++) {
	s[i] = 0;
	s[i + 1]++;
	s[0] = max(s[0], i + 1);
    }
}

bool bign::operator -- () {
    del();
    if (s[0] == 1 && s[1] == 0) return false;

    int i;
    for (i = 1; s[i] == 0; i++)
	s[i] = 9;
    s[i]--;
    del();
    return true;
}

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

bign bign::mul(int d) {
    s[0] += d;
    int i;
    for (i = s[0]; i > d; i--)
	s[i] = s[i - d];
    for (i = d; i; i--)
	s[i] = 0;
    return *this;
}

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


你可能感兴趣的:(UVA 254 - Towers of Hanoi(递归))