Tower of Hanoi foundation

Have to say, the Hang power oj Tower of Hanoi for various kinds of questions, and I really admire.

Prior to solve the Towers of Hanoi problem, first in terms of a story:
there is a legend early in India, Brahma at the time of creation of the world to do three diamond pillars, from the bottom up in a pile on a pole in order of size 64 gold discs. Brahma then command the disk Brahman repositioned in order of size to another pillar. And a predetermined, large disk can not be placed above the small disk, a disk can only be moved between the three pillars.

This is the most basic of the Tower of Hanoi problem ,, find the optimal solution is to let us move the number of steps (ie, the minimum number of steps to achieve our objective), we generally think of doing this problem is to use recursion to do (if did not understand, you can own to try, I suggest that you can play at the Tower of Hanoi game , really fun ha ha, will be able to play more of a spot patterns).

Do so using recursive code is as follows :( Consider A, B, C three columns, which are on the disk A column is now the object on the disc are moved to column A column C, and the specific meaning of problems requirements as mentioned above story)

#include<stdio.h>
int sum=0;
void f(int n,char A,char B,char C)
{
	if(n==1)
	{
		sum++;   //A柱子上最底层(即最大)的圆盘直接移到C柱子上 
	}
	else
	{
		f(n-1,A,C,B);  //首先将n-1个圆盘从A柱上绕过C柱移到B柱上 
		sum++;         //然后将最大圆盘从A柱上直接移到C柱上 
		f(n-1,B,A,C);  //最后再将n-1个圆盘从B柱上绕过A柱移到C柱上 
	}
}
int main()
{
	int n;
	scanf("%d",&n);
	f(n,'A','B','C');
	printf("%d",sum);
	return 0;
}

But in fact, this question can not recursively to do, as long as the law is found, the law as follows:

Disk number n: 1 2 3 4 5 6

Mobile steps optimal solution: 137,153,163

In this case we can easily find its follow 2 ^ n-1, then we can write code

#include<stdio.h>
#include<math.h>
int main()
{
	long long n,sum;
	scanf("%lld",&n);
	int i;
	for(i=1;i<=n;i++)
	{
		sum=pow(2,i)-1;
	}
	printf("%lld",sum);
	return 0;
 } 

The reason here is using long long because I found that when larger n, turned out to be negative, so the use of long integer instead of integer. Although this method is very stupid, but it's a simple way.

In addition to this problem, when I was training before encountered such a problem, in the above question is the basis of the Towers of Hanoi on added one condition: not allowed to move to the far right directly from the most left (right) side (left) side (each movement must be moved or removed from the middle of the middle column) , meaning that the column can not be moved from A to C to move the A-pillar or column C from the column, must pass B column to move, I will look at cases cited

The number of disks: 12345   

Mobile steps optimal solution: 282,680,242   

Can be found, starting from the second term, the number of steps it will follow its previous item multiplied by 3 plus 2, so that the code is like writing

#include<stdio.h>
#define N 100
int main()
{
	int n,a[N]={0};
	scanf("%d",&n);
	a[1]=2;
	int i;
	for(i=2;i<=n;i++)
	{
		a[i]=3*a[i-1]+2;
	}
	printf("%d",a[n]);
	return 0;
 } 

Tower of Hanoi look at the kinds of questions, I also found two particularly interesting questions are based on the most basic of the Tower of Hanoi problem, it has been adapted, it is to make us a while seeking to reach the target number plate needs k the minimum number of moving , and the other is let us find the number of the m-th mobile number plates of the two topics on the contrary, is very interesting, so we first look at the first question, I was to count one by one a little, after all, then we should not be more hands Well

Numeral platter ascending order of No. 1, No. 2, No. 3 ......

1.n = 3 (i.e. a total of three plates)

No. k disc: 123 

Minimum number of movements: 421

2.n = 4 Shi

No. k disc: 1234 

Minimum number of movements: 8421 

3.n = 5 Shi

No. k disc: 12,345

Minimum number of movements: 168421  

Easy to see, small disk is always twice the disk size larger number of movements, so the code is as follows:

#include<stdio.h>
#define N 100
int main()
{
	int n,k,a[N]={0};
	scanf("%d%d",&n,&k);
	a[n]=1;
	int i;
	for(i=n;i>k;i--)
	{
		a[i-1]=2*a[i];
	}
	printf("%d",a[k]);
	return 0;
 } 

Next is the second question, but this did not really want to top that, but we can think from a different angle, since it allows us to find the number plate number of the m-th move, then we have to find out each number plate it will move many times in our movement, we might as well give an example, took 3rd dish, it was the first 4,12,20,28 ...... are times when the move so, we are easy to see that these numbers are multiples of 4, and is an odd multiple, and they can be viewed as 4 ^ 2 (3-1), the plate number i is 2 ^ (i-1), so the code is as follows:

#include<stdio.h>
#include<math.h>
#define N 100
int main()
{
	int n,m,a[N]={0};
	scanf("%d%d",&n,&m);
	int i;
	for(i=1;i<=n;i++)
	{
		a[i]=pow(2,i-1);   
        if((m/a[i])%2==1&&m%a[i]==0)
        {
    	printf("%d",i);
        }
    }
    return 0;
}



This really is much simpler, however, this time I can only enter one set of data, in order to be entered once multiple sets , you can refer to the following code:

#include<stdio.h>
#include<math.h>
#define N 100
int main()
{
	long long n,m,a[N]={0};
	long long i,r;
	for(i=1;i<=n;i++)
	{
		a[i]=pow(2,i-1);  
	} 
	while(~scanf("%lld%lld",&n,&m),n+m)
	{
		for(i=1;i<=n;i++)
		{
		r=(m/a[i]);
        if(r%2==1&&m%a[i]==0)
        {
    	printf("%lld\n",i);
        }
        }
    }
    return 0;
}



This is a great God of the code (the code above), speaks in great detail, I put the ideas and code to tap to share to you, here is a very big thank god.

Add here a little knowledge:

If do not know ~ scanf, you can see (Great God in which detailed explanation)

https://blog.csdn.net/SiriusSun_/article/details/84575798
 

About Tower of Hanoi there are very, very many questions, of course, mostly by the most original adaptation over, these are just some of the more simple, are interested can go to find the Hang power oj into it and do something a bit more complex kinds of questions.

In addition most of them are some of the ideas and methods, but also the specific code based on the specific question may be, such as some types of data such as the use of different questions in the,


 

 

 

Released three original articles · won praise 5 · views 76

Guess you like

Origin blog.csdn.net/l218623/article/details/103918155