Nyoj 93 Hanoi (iii) "Stack"

Source: Internet
Author: User
Hanoi (iii)Time limit: 3000 ms | Memory limit: 65535 KB Difficulty: 3 description

In India, there is an ancient legend: in the World Center of Benares (in northern India) in the temple, a piece of yellow copper coin inserted three stone needles. In the creation of the world, Brahma, The Hindu Lord, was dressed in one of the needles from the bottom to the top of the 64 pieces of gold, which is called Hanoi. No matter day or night, there is always a monk in the following law to move these gold films: only one move at a time, no matter where the needle, the small piece must be in the large. The monks predicted that the world would be wiped out in a thunderbolt when all the pieces of gold were moved from the needle that was put on Brahma, and that the Vatican, the temples and all sentient beings would perish.


Now we're going to number three pins as 1,2,3.

All the gold pieces are on the 1th pin at the beginning, and the task now is to determine whether illegal instructions will appear during a series of instructions.

And the illegal instruction has the following two kinds of situations:

1, there is no gold film on a needle, but the instructions still require moving gold from the place to other needles.

2. Move a large piece of gold to a small gold piece. Enter the first line enter an integer N indicates the number of groups of test data (N<10)
The first row of each set of test data has two integers p,q (1<p<64,1<q<100), representing the number of Hanoi layers and subsequent instructions, respectively.
Then the Q line, each row is entered with two integers a,b, (1<=a,b<=3) represents an instruction.
Directive 1 2 means to move the top of the 1th pin to the top of the 2nd pin.
Data guarantee A,b will not be the same. Output if there is an illegal instruction, please output illegal
Output legal If no illegal instruction exists
Sample input

3
2 1
1 2
3
3
1 2 1 3 3 2 2 1 2-1
Sample output
Legal
Illegal
illegal
SOURCE [Zhang Yunzun] Original uploaded by Zhang Yunzun
#include <cstdio>
#include <stack>
using namespace std;
int main () {
	int n, p, Q, A, B, flag, I;
	scanf ("%d", &n);
	while (n--) {
		stack<int> sta[3];  Initialization of 
		scanf ("%d%d", &p,&q);
		for (i = p-1 i >=0; i--) {
			sta[0].push (i);
		} A needle from big to small into the stack p gold, size is the label 
		flag = 1;  Error is 0, correct is 1. 
		for (i = 0; i < Q; i++) {
			scanf ("%d%d", &a,&b);
			A = A-1; b = b-1;  Since the pin number starts at 0, subtract 1 
			if (Sta[a].empty ()) {  //There is no gold on a needle, but the instructions still require moving the gold sheet from there to the other pins. 
				flag = 0;
				break;
			if (Sta[b].empty ()) {  //if B is null, no comparison can be directly into the stack 
				Sta[b].push (Sta[a].top ());
				Sta[a].pop ();
			} else{   //If B is not empty, compare the upper and lower two gold slices 
				if (sta[b].top () < Sta[a].top ()) {  //move a large piece of gold to a small gold piece 
					flag = 0;
					break;
				else{   //correct situation, a stack of top B stack 
					Sta[b].push (Sta[a].top ());
					Sta[a].pop ();
		}} if (flag)  printf ("legal\n");
		else printf ("illegal\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.