HDOJ 1997 Nottingham VII (Recursive simulation or law solution)

Source: Internet
Author: User
 Hanoi VII

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 1358 Accepted Submission (s): 891


Problem Description N Plates The minimum number of moves is 2^n-1, that is, the 2^n series will be generated during the move. Due to the increase in the number of errors caused by the series, this error is misplaced the column, and will not put the market on the small plate, that is, the size of the pillars from the bottom to the top of the relationship remains as follows:
N=m+p+q
A1>a2>...>am
B1>b2>...>bp
C1>c2>...>cq
AI is the disk number series on the A-column, BI is the disk number series on the B-pillar, CI is the disk number series on the C column, the original goal is to move n plates on the A column to the C drive. Give 1 series to determine whether it is a series produced in the correct movement.
Example 1:n=3
3
2
1
is right.
Example 2:n=3
3
1
2
is not correct.
Note: For example 2, if the target is to move n plates on the A column to the B plate. is correct.
Input contains multiple sets of data, first entering T, which indicates that there is a T group of data. 4 rows per set of data, row 1th n is the number of plates n<=64.
The following 3 lines are as follows
M A1 A2 ... am
P B1 B2. bp
Q C1 c2 ... cq
N=m+p+q,0<=m<=n,0<=p<=n,0<=q<=n,
Output for each set of data, determine whether it is a series that is generated in the correct movement. Correct output true, otherwise false

Sample Input

6 3 1 3 1 2 1 1 3 1 3 1 1 1 2 6 3 6 5 4 1 1 2 3 2 6 3 6 5 4 2 3 2 1 1 3 1 3 1 2 1 1 20 2 20 17 2 19 18 16 16 1 10 9 8 7 6 5 4 3 2 1
Sample Output
True false false False true
Problem: The topic is very good, not familiar with the law of Hanoi Mobile students, please enter, Portal: Grandhotel Pupp Blog
There are two kinds of solutions.
One, recursive simulation method: Using recursion to simulate the process of Hanoi movement, to determine whether the given State can be all moved to the third pillar.
① consider the maximum plate n plate, moving direction is a-->c, it can only be on a or C, if it is on B, then false;

② if the n plate is on a, then the n-1 plate on it will be in the movement from the a-->b, at this time the maximum disk number is n-1, the direction of movement is a->b;

③ if the n plate is on C, the n-1 plate on it will be in the movement from the b-->c, at this time the maximum disk number is n-1, the direction of movement is b->c;

④ repeat ①②③.
The code is as follows:
#include <cstdio>
#include <cstring>
using namespace std;

void input (int n,int *a)
{
	int i;
	memset (a,0,65*sizeof (int));//This cannot be written as sizeof (a) 
	for (i=0;i<n;++i)
		scanf ("%d", &a[i]);
}

BOOL Move (int n,int *a,int *b,int *c)//Simulate Hanoi move process 
{
	if (n==0) 
		return True
	; if (n==a[0])//If the nth plate is on a, the n-1 plate (except for the plates outside N) is moved to the B column with the C-pillar and 
		return Move (n-1,++a,c,b);
	else if (n==c[0])//If the nth plate is above C, the n-1 plate is moved from the B-pillar (except for the plate outside N) to the C-pillar with the 
		return Move (n-1,b,a,++c);
	return false;
}

int main ()
{
	int t,i,m,q,p,n;
	int a[65],b[65],c[65];
	scanf ("%d", &t);
	while (t--)
	{
		scanf ("%d", &n);
		scanf ("%d", &m);
		Input (m,a);
		scanf ("%d", &q);
		Input (q,b);
		scanf ("%d", &p);
		Input (p,c);
		if (move (n,a,b,c))
			printf ("true\n");
		else
			printf ("false\n");
	}
	return 0;
} 


Law solution:
We can find some regularities through the process of moving the Hanoi model above.
When the ① disk is odd, the bottom is an even number only in the 2nd bar, the base is an odd plate only in 1, 3rd column
② even the opposite of the above
③ the plates on each pillar are odd and even interchangeable.
The code is as follows:

#include <cstdio>
#include <cstring>
#define MAXN
using namespace std;
int a[3][maxn],n;

BOOL Solve ()
{
	int i,j;
	for (I=0;i<3;++i)
	{
		if (a[i][0]==0)
			continue;
		if ((n+i+a[i][1))%2)//The number of the disk is odd, the second pillar of the bottom of the disk is even, the base of the odd command appears in 1, 3, the column, the number of even when the opposite 
			return false;
		for (J=2;J<=A[I][0];++J)//Whether parity is reversed before and after 
		{
			if ((A[i][j]+a[i][j-1])%2==0)
				return false;
		}
	}
	return true;
}

int main ()
{
	int i,t;
	scanf ("%d", &t);
	while (t--)
	{
		scanf ("%d", &n);
		for (I=0;i<3;++i)
		{
			scanf ("%d", &a[i][0]);
			for (int j=1;j<=a[i][0];++j)
				scanf ("%d", &a[i][j]);
		}
		if (Solve ())
			printf ("true\n");
		else
			printf ("false\n");
	}
	return 0;




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.