CHINA oj- title Advanced Topics 56 - m-th step of the Tower of Hanoi

Here Insert Picture Description

The first step 56 m of the Tower of Hanoi

Author: Turbo Time limit: 3S chapter: Recursive

Problem Description :

Given three different rods A, B, C, and the size of a few dishes. These plates set in descending order according to the size of the rod A, the smallest at the top. Now the task is to move the plate from the lever C A stacked bar and maintain the original order. In carrying out the task, a plate can only be moved, at any time and does not allow a large plate on the small plate above, B can be used as an auxiliary storage rod lever. Seeking: a total of n disks, the m-th step in the process of moving the rod from which the rod which is to.

Enter a description:

Your program needs from the standard input device (usually keyboard) reads multiple sets of test data. Each line of input data, each input line of a plate integer number n, 1≤n≤10, and the step number m, between two data separated by a space. Beginning of the line end of the line and no extra spaces, no extra blank line between the two sets of data.
Output Description:

For each test case, you need to program a standard output device (generally on the terminal program) corresponding to one line are sequentially outputted answer, the case where the m-th output line of the step movement, as in the first step is to move from A m B, the output "A-B" (without the quotes). If the m-th step movement does not exist, the output "none"
(no quotes).

No blank line between two data sets, a first set of front and rear last group no blank lines.

Example Input: Output Example 2324: B-C none

Towers of Hanoi problem ...... I remember I had two, this time to really do or do not remember a loud noise. Knowing only recursive, but how recursion, like a long time did not recall.
Saw a big brother to explain, and suddenly understand, recursive idea is a step by step to simplify the problem.

If I want to nine plates moved from tower A tower C, if I have the above eight plates moved from Tower A Tower B, so this time I only need one plate moves from A tower to tower C finished! But the question is how to move from the plate 8 A column B column it? Like, if I have above seven plates moved from Tower A Tower C, so this time I only need one plate is moved Tower B from A tower on it ......

This is the idea of recursive ah
Code:

/*
	T56 汉诺塔问题的第m步 
*/

#include<stdio.h> 

int m;
int count;
void hannuota(int n, char A, char B, char C);

int main() {
	int n = 0;
	
	while (scanf("%d%d", &n, &m) != EOF) {
		count = 0;
		
		if (m == -1 || m == 0) {
			printf("none\n"); 
			continue ;
		}
		hannuota(n, 'A', 'B', 'C');
		if (count < m) {// 用不了m个步骤 
			printf("none\n"); 
		}
	}
	
	return 0;
}

// 将n个盘子从A移到C 
void hannuota(int n, char A, char B, char C) {
	if (n == 1) {// 递归出口 
		//printf("%c--%c\n", A, C);
		count++;
		if (count == m) {
			printf("%c--%c\n", A, C);
			return ;
		}
		return ;
	}
	
	hannuota(n - 1, A, C, B);
	//printf("%c--%c\n", A, C);
	count++;
	if (count == m) {
		printf("%c--%c\n", A, C);
		return ;
	}
	hannuota(n - 1, B, A, C);
}

This time should be thoroughly remember

Published 74 original articles · won praise 7 · views 8843

Guess you like

Origin blog.csdn.net/qq_41409120/article/details/104771615