Tower tower (3) Time Limit: 3000 MS | memory limit: 65535 kb difficulty: 3
-
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 line to indicate the number of groups of test data (n <10)
The first line of each group of test data contains two integers, namely, p and q (1 <p <100 <q <), indicating the number of layers of the tower and the number of subsequent commands respectively.
In the next Q row, two integers A, B, (1 <= A, B <= 3) are input in each row to indicate a command.
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 illegal
If no illegal command exists, the output legal
-
Sample Input
-
32 11 23 31 21 33 22 12 1
-
Sample output
-
legalillegalillegal
1 #include<stdio.h> 2 #include<stack> 3 using namespace std; 4 stack<int> a,b,c; 5 stack<int>& getStack(int k) 6 { 7 switch (k) 8 { 9 case 1:10 return a;11 case 2:12 return b;13 case 3:14 return c;15 }16 }17 void clearStack()18 {19 while(!a.empty())a.pop();20 while(!b.empty())b.pop();21 while(!c.empty())c.pop();22 }23 24 bool hanNuoTa3(int n, int m)25 {26 int i,start,to;27 bool jduge = true;28 for (i=n; i > 0; i--)29 a.push(i);30 for (i=0; i < m; i++)31 {32 scanf("%d%d",&start,&to);33 if (getStack(start).size() == 0)34 jduge = false;35 else36 {37 if (getStack(to).size() == 0)38 {39 getStack(to).push(getStack(start).top());40 getStack(start).pop();41 }42 else43 {44 if (getStack(to).top() < getStack(start).top())45 jduge = false;46 else47 {48 getStack(to).push(getStack(start).top());49 getStack(start).pop();50 }51 }52 }53 }54 clearStack();55 if (jduge)56 return true;57 else58 return false;59 }60 int main()61 {62 int n,m,k;63 scanf("%d",&n);64 while(n--)65 {66 scanf("%d%d",&k,&m);67 if (hanNuoTa3(k,m))68 printf("legal\n");69 else70 printf("illegal\n");71 }72 return 0;73 }
Nyoj 93 tower of legends (III)