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;如果遇到非法操作(所取的柱子为空 或者 所移的盘子是上面大下面小)就输出当前步数的负数! 如果遇到非法操作,后面的操作就无效了! 附上代码:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
int main()
{
stack<int> v[];
int i,j,a,b,T,n,m;
scanf("%d",&T);
while(T--)
{
for(i=; i<=; i++) //清空栈
while(!v[i].empty())
v[i].pop();
scanf("%d%d",&n,&m);
for(i=n; i>=; i--)
v[].push(i); //第一个柱子装满,依照栈的性质,从大到小装
int t=;
for(i=; i<=m; i++)
{
scanf("%d%d",&a,&b);
if(!t) //如果确定了输出结果,将不再进行后面的循环
continue;
if(v[a].empty()) //所取的柱子为空,非法操作
{
printf("%d\n",-i);
t=;
continue;
}
if(v[b].empty()) //放置的柱子为空,盘子直接移过去
{
v[b].push(v[a].top());
v[a].pop();
}
else
{
if(v[a].top()<v[b].top()) //不为空,则需判断所移的盘子是上面大下面小
{
v[b].push(v[a].top());
v[a].pop();
}
else
{
printf("%d\n",-i);
t=;
continue;
}
}
if(v[].size()==n) //全部移完
{
t=;
printf("%d\n",i);
}
}
if(t)
printf("0\n");
}
return ;
}

zoj 2954 Hanoi Tower的更多相关文章

  1. HDU1329 Hanoi Tower Troubles Again!——S.B.S.

    Hanoi Tower Troubles Again! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  2. ZOJ-1239 Hanoi Tower Troubles Again!

    链接:ZOJ1239 Hanoi Tower Troubles Again! Description People stopped moving discs from peg to peg after ...

  3. Codeforces Gym 100114 A. Hanoi tower 找规律

    A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...

  4. 汉诺塔 Hanoi Tower

    电影<猩球崛起>刚开始的时候,年轻的Caesar在玩一种很有意思的游戏,就是汉诺塔...... 汉诺塔源自一个古老的印度传说:在世界的中心贝拿勒斯的圣庙里,一块黄铜板上插着三支宝石针.印度 ...

  5. HDU 1329 Hanoi Tower Troubles Again!(乱搞)

    Hanoi Tower Troubles Again! Problem Description People stopped moving discs from peg to peg after th ...

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

    课本源码部分 第3章  栈和队列 - 汉诺塔(Hanoi Tower)问题 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版> ...

  7. 1028. Hanoi Tower Sequence

    1028. Hanoi Tower Sequence Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Hanoi Tow ...

  8. Python学习札记(十四) Function4 递归函数 & Hanoi Tower

    reference:递归函数 Note 1.在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. eg.计算阶乘: #!/usr/bin/env python3 def ...

  9. 10276 - Hanoi Tower Troubles Again!(思维,模拟)

    People stopped moving discs from peg to peg after they know the number of steps needed to complete t ...

随机推荐

  1. set的基本使用

    构造一个集合 现在我们来构造一个集合. C++ 中直接构造一个 set的语句为: sets.这样我们定义了一个名为 s的.储存 T类型数据的 集合,其中 T是集合要储存的数据类型.初始的时候 s是空集 ...

  2. 解决listview点击item失效

    开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承BaseAdapter,在adapter中按照需求进行编写,问题就出现了 ...

  3. python-null

    很早之前,遇到过一个面试官问的python中有没有null,当时有点紧张,想成了None,就脱口而出是有的.今天遇到了none问题,所以就正好说一下. python中是没有NULL的. Python中 ...

  4. 为什么不用原生的Spring Cloud Config

    引言 近几年传统应用架构已经逐渐朝着微服务架构演进.那么随着业务的发展,微服务越来越庞大,此时服务配置的管理变得会复杂起来.为了方便服务配置文件统一管理,实时更新,配置中心应运而生.其实,所谓配置中心 ...

  5. 有意思的DP(CF360B Levko and Array)

    刚才面试了一个蛮有意思的DP题目,脑子断片,没写出来,不过早上状态还是蛮好的 一个长度为n的序列最多改变k次,使相邻两数之差绝对值的最大值最小 三维的dp我先尝试写一下 Codeforces 360B ...

  6. vue+axios 对restful 请求封装

    礼拜天来公司整理项目,项目是最近开始重构的,里面的各种http请求接口是restful结构的(为了提升项目的比格),整理一下笔记 [restful介绍][1]博主讲的很详细 技术栈: vue + vu ...

  7. oralce管理命令

    Emctl start agent TZ set to PRC Oracle Enterprise Manager 10g Database Control Release 10.2.0.1.0 Co ...

  8. (转) Hibernate持久化类与主键生成策略

    http://blog.csdn.net/yerenyuan_pku/article/details/65462930 Hibernate持久化类 什么是持久化类呢?在Hibernate中持久化类的英 ...

  9. linux awk命令详解,使用system来内嵌系统命令, awk合并两列

    linux awk命令详解 简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分 ...

  10. CC-Debugger 最小调试接法

    CC-Debugger 最小调试接法 以 CC2541 为例,最少需要四根 DD DC RST GND. 一般 VCC 目标调试板都有,所以这里你需要将 CC-Debugger 的 PIN 2 和 P ...