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;如果遇到非法操作(所取的柱子为空 或者 所移的盘子是上面大下面小)就输出当前步数的负数! 如果遇到非法操作,后面的操作就无效了! 附上代码:
#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的更多相关文章
- 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 ...
- ZOJ-1239 Hanoi Tower Troubles Again!
链接:ZOJ1239 Hanoi Tower Troubles Again! Description People stopped moving discs from peg to peg after ...
- Codeforces Gym 100114 A. Hanoi tower 找规律
A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...
- 汉诺塔 Hanoi Tower
电影<猩球崛起>刚开始的时候,年轻的Caesar在玩一种很有意思的游戏,就是汉诺塔...... 汉诺塔源自一个古老的印度传说:在世界的中心贝拿勒斯的圣庙里,一块黄铜板上插着三支宝石针.印度 ...
- HDU 1329 Hanoi Tower Troubles Again!(乱搞)
Hanoi Tower Troubles Again! Problem Description People stopped moving discs from peg to peg after th ...
- 3-6-汉诺塔(Hanoi Tower)问题-栈和队列-第3章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第3章 栈和队列 - 汉诺塔(Hanoi Tower)问题 ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版> ...
- 1028. Hanoi Tower Sequence
1028. Hanoi Tower Sequence Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Hanoi Tow ...
- Python学习札记(十四) Function4 递归函数 & Hanoi Tower
reference:递归函数 Note 1.在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. eg.计算阶乘: #!/usr/bin/env python3 def ...
- 10276 - Hanoi Tower Troubles Again!(思维,模拟)
People stopped moving discs from peg to peg after they know the number of steps needed to complete t ...
随机推荐
- 微信小程序--轮播图,标题,盒子,tab栏的合成例子
小程序是什么? 微信小程序,是一种不需要下载安装即可使用的应用,用户扫一扫或搜一下即可打开应用,在微信-发现-小程序可打开应用. 一.小程序的样式编写: 目录结构: app.json { " ...
- 【Pyqt5】自定义信号简单原理(易懂版),多窗口交互,传输数据,调用方法
PS:如果你想在2窗口调用1窗口的内部方法,或者在2窗口传递数据给1窗口数据,本片博客可以放心食用 主窗口: class MainWindow(QWidget,Ui_MainFrom): insert ...
- 完整版unity安卓发布流程(包括SDK有原生系统依赖关系的工程)
要3个东西!NDS,SDK,JDK, NDK官网下载:https://developer.android.google.cn/ndk/downloads/index.html(注意系统是不是64位) ...
- <第一周>降维
PCA 矩阵的主成分就是其协方差矩阵对应的特征向量,按照对应的特征值大小进行排序,最大的特征值为第一主成分,以此类推 主要过程 对所有样本进行中心化 计算样本的协方差矩阵 XX.T 对协方差矩阵做特征 ...
- linux系统 (实验二)实验楼的课程笔记
who am i 打印用户名和终端 终端的启动时间 whoami 打印用户名 who 命令其它常用参数 参数 说明 -a 打印能打印的全部 -d 打印死掉的进程 -m 同am i,mom li ...
- JavaScript--放大镜
上例图: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
- MUI - 封装localStorage与plus.storage
MUI - 封装localStorage与plus.storage 2.0版本 在使用plus.storage频繁地存取数据时,可以感觉到明显的卡顿,而且很耗内存, 在切换到localstorage时 ...
- Git同步Python代码
之前我们都是将代码保存到本地目录, 然后再上传到Git中,但如果针对在pycharm中的代码,就要换另一种方式了,下面简单介绍一下. 1.打开pycharm主界面,选择菜单栏VCS---checkou ...
- hdu5444 乱搞 长春网赛
可以暴力. #include<iostream> #include<cstring> #define maxn 1100 using namespace std; int a[ ...
- 两张图说明http协议,tcp协议,ip协议,dns服务之间的关系和区别
一.理解一个传输流再去扩展 用http举例来说,首先作为发送端的客户端在应用层(http协议)发出一个想看某个web页面的http请求. 接着,为了传输方便,在传输层(tcp协议)把从应用层处收到的数 ...