Tower of Hanoi (III)

Source: Internet
Author: User
Tower of Hanoi (III)

Description

In India, there is an old legend: In the holy temple in the center of the world, benalus (in Northern India), three gem needles are inserted on a copper sheet. In the creation of the world, Fan Tian, the Hindu god, wore 64 gold tablets from the ground up to the ground on one of the needles. No matter day or night, there is always a monk moving the gold Tablets according to the following rules: one piece at a time, no matter which needle, the small pieces must be on the large film. The monks predicted that the world would be wiped out in a bang when all the gold pieces were moved from the needle worn by fan Tian to another, and the Vatican, temples, and sentient beings will all be lost together.

 

Now we have three needles numbered 1, 2, 3.

All gold clips are initially placed on the first needle. Now, the task for you is to determine whether an illegal command will appear in a series of instructions.

Illegal commands can be found in the following two cases:

1. There is no gold on a needle, but the instruction still requires the gold to be moved from there to other needles.

2. Move a large gold tablet to a small gold tablet.

 
Input
Enter an integer N in the first row to indicate the number of test data groups (n <10). The first row of test data contains two integers, P, q (1 <p <64,1 <q <100), indicating the number of floors of the tower and the number of subsequent Q rows. Each row has two integers A, B, (1 <= A, B <= 3) indicates an instruction. Command 1 2 indicates moving the gold tablet at the top of needle 1 to the top of needle 2. Data guarantee A and B will not be the same.
Output
If an invalid command exists, output the illegal command.
Sample Input
32 11 23 31 21 33 22 12 1
Sample output
legalillegalillegal

 #include <iostream>#include <stack>using namespace std;int main(){    int n;    int ceng,oper;    int t1,t2;    bool flag;    cin>>n;    while(n--)    {        stack<int>s[4];if(!s[1].empty())    s[1].pop();if(!s[2].empty())    s[2].pop();if(!s[3].empty())    s[3].pop();flag=true;cin>>ceng>>oper;for(int i=ceng;i>=1;i--)    s[1].push(i);  for(int i=0;i<oper;i++)  {      cin>>t1>>t2;      if(s[t1].empty())      {          flag=false;          break;      }      else if(!s[t2].empty() && s[t1].top()>s[t2].top())      {          flag=false;          break;      }      s[t2].push(s[t1].top());      s[t1].pop();  }  if(flag==true)      cout<<"legal"<<endl;  else      cout<<"illegal"<<endl;    }    return 0;}        

 

Tower of Hanoi (III)

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.