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 }
时间: 2024-10-25 03:28:59

zoj 2954 Hanoi Tower的相关文章

ZOJ 2954 Hanoi Tower(模拟啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1953 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

3-6-汉诺塔(Hanoi Tower)问题-栈和队列-第3章-《数据结构》课本源码-严蔚敏吴伟民版

课本源码部分 第3章  栈和队列 - 汉诺塔(Hanoi Tower)问题 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接??? <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集解析使用说明        课本源码合辑  链接??? <数据结构>课本源码合辑        习题集全解析  链接??? <数据结构题集>习题解析合辑        本源码引入的文件  链接? 无外链        相关测试数据下载  链接? 无数

Hanoi Tower问题的求解

文章前部分为转载,转自http://www.cnblogs.com/yanlingyin/ 当然.这是一个经典的递归问题~   想必来看这篇博文的同学对汉诺塔应该不会陌生了吧, 写这篇博还是有初衷的: 之前学数据结构的时候自己看书.也上网上查了很多资料,资料都比较散.而且描述的不是很清楚,对于当时刚刚 接触算法的我,要完全理解还是有一定难度.今天刚好有时间就整理了下思路.重写分析了一下之前的疑惑的地方. 没有透彻的地方便都豁然开朗了.所以迫不及待把我的想法记录下来,和大家分享. 如果你也是和之前

汉诺塔 Hanoi Tower

一个古老的印度传说:在世界的中心贝拿勒斯的圣庙里,一块黄铜板上插着三支宝石针.印度教的主神梵天在创造世界的时候,在其中一根针上从下到上穿好了由大到小的64片金片,这就是所谓的汉诺塔(Hanoi Tower).不论白天黑夜,总有一个僧侣在按照下面的法则移动这些金片:一次只移动一片,不管在哪根针上,小片必须在大片上面. 僧侣们预言,当所有的金片从梵天穿好的金片上移到另一根针上时,世界末日就会来临,而梵塔.寺庙和众生也会随之灭亡...... 故事不多说了,汉诺塔是递归思想的典型应用,上代码: 1 #i

1028. Hanoi Tower Sequence

Description Hanoi Tower is a famous game invented by the French mathematician Edourard Lucas in 1883. We are given a tower of n disks, initially stacked in decreasing size on one of three pegs. The objective is to transfer the entire tower to one of

ZOJ-1239 Hanoi Tower Troubles Again!

链接:ZOJ1239 Hanoi Tower Troubles Again! Description People stopped moving discs from peg to peg after they know the number of steps needed to complete the entire task. But on the other hand, they didn't not stopped thinking about similar puzzles with

Codeforces Gym 100114 A. Hanoi tower 找规律

A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description you the conditions of this task. There are 3 pivots: A, B, C. Initially, n disks of different diameter are placed on the pivot A: the smallest dis

HDU 1329 Hanoi Tower Troubles Again!

Hanoi Tower Troubles Again! Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 132964-bit integer IO format: %I64d      Java class name: Main People stopped moving discs from peg to peg after they know the numbe

sicily 1028. Hanoi Tower Sequence

1028. Hanoi Tower Sequence Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Hanoi Tower is a famous game invented by the French mathematician Edourard Lucas in 1883. We are given a tower of n disks, initially stacked in decreasing size