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

  1. 3
  2. 2 3
  3. 1 2
  4. 1 3
  5. 2 3
  6. 2 3
  7. 1 2
  8. 1 3
  9. 3 2
  10. 2 3
  11. 1 3
  12. 1 2
  13. 3 2

Sample Output

  1. 3
  2. -3
  3. 0
  4.  
  5. 汉诺塔问题,数组的模拟,没有任何算法,我使用了栈,要注意的是,所有的数据都需要全部输入,不要输出答案就停止循环。
  6.  
  7. 题意:汉诺塔,判断题中所给的m步能否把n个盘从第一根移到第三根;如果能输出所需步数,不能则输出0;如果遇到非法操作(所取的柱子为空 或者 所移的盘子是上面大下面小)就输出当前步数的负数! 如果遇到非法操作,后面的操作就无效了!
  8.  
  9. 附上代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <stack>
  5. using namespace std;
  6. int main()
  7. {
  8. stack<int> v[];
  9. int i,j,a,b,T,n,m;
  10. scanf("%d",&T);
  11. while(T--)
  12. {
  13. for(i=; i<=; i++) //清空栈
  14. while(!v[i].empty())
  15. v[i].pop();
  16. scanf("%d%d",&n,&m);
  17. for(i=n; i>=; i--)
  18. v[].push(i); //第一个柱子装满,依照栈的性质,从大到小装
  19. int t=;
  20. for(i=; 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=;
  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=;
  47. continue;
  48. }
  49. }
  50. if(v[].size()==n) //全部移完
  51. {
  52. t=;
  53. printf("%d\n",i);
  54. }
  55. }
  56. if(t)
  57. printf("0\n");
  58. }
  59. return ;
  60. }

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. LUOGU P1512 伊甸园日历游戏

    题目描述 Adam和Eve玩一个游戏,他们先从1900.1.1到2001.11.4这个日期之间随意抽取一个日期出来.然后他们轮流对这个日期进行操作: 1 : 把日期的天数加1,例如1900.1.1变到 ...

  2. Ajax--serialize应用表单数据序列化

    一.jQuery+Ajax表单数据序列化 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&qu ...

  3. poj 2406 Power Strings(KMP入门,next函数理解)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 37685   Accepted: 15590 D ...

  4. 磁力搜索嗅探器装成BT

    磁力搜索嗅探器装成BT ague-dht ague-dht 是一个磁力链接嗅探器,它伪装成BT下载客服端,加入DHT网络,嗅探磁力链接.每秒发送1000条请求时,平均3秒收到1次带有infohash的 ...

  5. Android——兼容性

    两种类型的兼容性:设备兼容性和应用兼容性. 设备的Android兼容性就是app能够正确运行的Android执行环境(Android execution environment).Android执行环 ...

  6. 2-4 Numpy+Matplotlib可视化(二)

    自定义绘图 # -*-coding:utf-8-*- # !/usr/bin/env python # Author:@vilicute import numpy as np import matpl ...

  7. vs code 配置c/c++环境

    1. 编译 通过 code-runner插件 运行编译 安装code-runner后在settings.json中找到code-runner.executorMap,可以看到其中的cpp 文件运行方式 ...

  8. 在liferay 7中如何删除service builder已经生成的数据库table

    在Liferay 7中,加了数据库保护机制,你改了service.xml的结构后,重新运行service builder,并不会帮你生成新的数据库表.然后你发现你在数据库中自己手动删除了表后,重新部署 ...

  9. request.getcontextPath() 详解 和 <link标签>

    classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找. 绝对路径: D:\磊弟资料\最代码\智父子考试 ...

  10. iOS 停止不必要的UI动效设计

    http://www.cocoachina.com/design/20151124/14400.html 前言:这篇短文将会探讨UI设计中动画的过度使用,并将其与早期的视觉设计进行对比,提出一些对于有 ...