zoj 2954 Hanoi Tower

Hanoi Tower

Time Limit: 2 Seconds Memory Limit: 65536 KB

You all must know the puzzle named "The Towers of Hanoi". The puzzle has three pegs (peg 1, peg 2 and peg 3) and N disks of different radii. Initially all disks are located on the first peg, ordered by their radii - the largest at the bottom, the smallest at the top. In each turn you may take the topmost disc from any peg and move it to another peg, the only rule says that you may not place the disc atop any smaller disk. The problem is to move all disks to the last peg (peg 3). I use two different integers a (1 <= a <= 3) and b (1 <= b <= 3) to indicate a move. It means to move the topmost disk of peg a to the top of peg b. A move is valid if and only if there is at least one disk on peg a and the topmost disk of peg a can be moved on the peg b without breaking the former rule.

Give you some moves of a game, can you give out the result of the game?

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 55) which is the number of test cases. And it will be followed by T consecutive test cases.

The first line of each test case is a single line containing 2 integers n (1 <= n <= 10) and m (1 <= m <= 12000) which is the number of disks and the number of the moves. Then m lines of moves follow.

Output

For each test case, output an integer in a single line according to the result of the moves.
Note:
(1) If there is an invalid move before all disks being on peg 3 and the invalid move is the p-th move of this case (start from 1) , output the integer -p please and the moves after this move(if any) are ignored.
(2) If after the p-th move all disks are on peg 3 without any invalid move, output the integer p please and the moves after this move (if any) are ignored.
(3) Otherwise output the integer 0 please.

Sample Input

 

3
2 3
1 2
1 3
2 3
2 3
1 2
1 3
3 2
2 3
1 3
1 2
3 2

 

Sample Output

 

3
-3
0


汉诺塔问题,数组的模拟,没有任何算法,我使用了栈,要注意的是,所有的数据都需要全部输入,不要输出答案就停止循环。

题意:汉诺塔,判断题中所给的m步能否把n个盘从第一根移到第三根;如果能输出所需步数,不能则输出0;如果遇到非法操作(所取的柱子为空 或者 所移的盘子是上面大下面小)就输出当前步数的负数! 如果遇到非法操作,后面的操作就无效了!

附上代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <stack>
 5 using namespace std;
 6 int main()
 7 {
 8     stack<int> v[5];
 9     int i,j,a,b,T,n,m;
10     scanf("%d",&T);
11     while(T--)
12     {
13         for(i=1; i<=3; i++)        //清空栈
14             while(!v[i].empty())
15                 v[i].pop();
16         scanf("%d%d",&n,&m);
17         for(i=n; i>=1; i--)
18             v[1].push(i);         //第一个柱子装满,依照栈的性质,从大到小装
19         int t=1;
20         for(i=1; i<=m; i++)
21         {
22             scanf("%d%d",&a,&b);
23             if(!t)             //如果确定了输出结果,将不再进行后面的循环
24                 continue;
25             if(v[a].empty())    //所取的柱子为空,非法操作
26             {
27                 printf("%d\n",-i);
28                 t=0;
29                 continue;
30             }
31             if(v[b].empty()) //放置的柱子为空,盘子直接移过去
32             {
33                 v[b].push(v[a].top());
34                 v[a].pop();
35             }
36             else
37             {
38                 if(v[a].top()<v[b].top()) //不为空,则需判断所移的盘子是上面大下面小
39                 {
40                     v[b].push(v[a].top());
41                     v[a].pop();
42                 }
43                 else
44                 {
45                     printf("%d\n",-i);
46                     t=0;
47                     continue;
48                 }
49             }
50             if(v[3].size()==n)  //全部移完
51             {
52                 t=0;
53                 printf("%d\n",i);
54             }
55         }
56         if(t)
57             printf("0\n");
58     }
59     return 0;
60 }

 

posted @ 2016-01-21 16:36  lucky_少哖  阅读(240)  评论(0编辑  收藏  举报