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. 微信小程序--Tab栏切换的快速实现

    上效果! wxss样式代码: .tabs-item.selected { color:rgba(,,,.); border-bottom:2px solid rgba(,,,.); } .tabs-i ...

  2. 产生冠军 HDU - 2094 (拓扑排序)

    分析: 当有且只有一个节点入度为0时,该节点即为冠军,否则不能产生冠军.所以以下代码中只要入度大于0的无论是几都将其设置为1. #include <stdio.h> #include &l ...

  3. 【django后端分离】Django Rest Framework之认证系统之redis数据库的token认证(token过期时间)

    1:登录视图 redis_cli.py文件: import redis Pool= redis.ConnectionPool(host='localhost',port=6379,decode_res ...

  4. Java.控制层.响应工具类.

    Java.控制层.响应工具类. package cn.com.spdbccc.cds.index.web.base; public class ApiResponse { private int co ...

  5. VirtualBox安装,VirtualBox安装CentOS

    1.进入VirtualBox官网下载页,找到对应的版本 https://www.virtualbox.org/wiki/Downloads 按步骤安装好 2.进入CentOS官网下载页,找到对应的版本 ...

  6. Tomcat的原理

    Tomcat是一种WEB容器:它主要实现了javaEE中Servlet.JSP规范,同时提供HTTP服务. 它主要由一个Serve服务器,Server服务器中存在多个Service服务,Service ...

  7. day13 memcache,redis上篇

    memcache memcache简介 Memcached是一个自由开源的,高性能,分布式内存对象缓存系统. Memcached是以LiveJournal旗下Danga Interactive公司的B ...

  8. httpclient向浏览器发送get和post请求

    get请求代码实现 public static void main(String[] args) { CloseableHttpClient httpClient = null;  //请求对象 Cl ...

  9. poj3308 最小割

    因为行可以了,那列就不行,所以根据行列建立最小割模型. 然后这题精妙之处在于把乘法取对数后转化为加法,瞬间就简单了. 保证精度,C++AC ,16MS G++WA. #include<stdio ...

  10. 2019-9-2-win10-uwp-随着数字变化颜色控件

    title author date CreateTime categories win10 uwp 随着数字变化颜色控件 lindexi 2019-09-02 12:57:38 +0800 2018- ...