zoj 2954 Hanoi Tower(汉诺塔)
转载请注明出处:http://blog.csdn.net/u012860063
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=43243#problem/H
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 byT 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
Author: CAO, Peng
Source: Zhejiang University Local Contest 2008
题意:汉诺塔,判断题中所给的m步能否把n个盘从第一根移到第三根;如果能输出所需步数,不能则输出0;如果遇到非法操作(所取的柱子为空 或者 所移的盘子是上面大下面小)就输出当前步数的负数! 如果遇到非法操作,后面的操作就无效了!
代码:(栈操作)
:
#include<cstdio>
#include<stack>
#include<iostream>
using namespace std;
stack<int>s[4];
int ans,flog,p,n,m,t,step,k,i;
int x, y,a,b ,f;
void judge(int a , int b);
int main()
{while(~scanf("%d",&t)){while(t--){scanf("%d%d",&n,&m);flog = ans = p = k = step = 0;for( i = 0 ; i < 4 ; i++ ){while(!s[i].empty())s[i].pop();}for(i = n ; i >= 1 ; i--)s[1].push(i);for(i = 1 ; i <= m ; i++ ){scanf("%d%d",&a,&b);if(flog)continue;ans++;if(s[a].empty() == 1){flog = 1;step = ans;continue;}judge(a , b);if(s[3].size()==n){flog = 2;step = ans;}}if(flog == 1)printf("-%d\n",step);else if(s[3].size() == n)printf("%d\n",step);elseprintf("0\n");}}return 0;
}void judge(int a , int b)
{x = s[a].top();if(s[b].empty() == 1){s[b].push(x);s[a].pop();return;}elsey = s[b].top();if(x > y){flog = 1;step = ans;return;}else{s[b].push(x);s[a].pop();}
}
zoj 2954 Hanoi Tower(汉诺塔)相关推荐
- Hanoi Tower 汉诺塔的简单分析/C
当然.这是一个经典的递归问题~ 想必来看这篇博文的同学对汉诺塔应该不会陌生了吧, 写这篇博还是有初衷的: 之前学数据结构的时候自己看书.也上网上查了很多资料,资料都比较散.而且描述的不是很清楚, ...
- Tower of Hanoi(汉诺塔)详解
一个经典的汉诺塔问题,带着我自己的理解给做这个问题的友友们解决一下,包括我本人在做的时候也遇到的一些问题给大家阐述一下.话不多说,来看: 汉诺塔 问题描述: 汉诺塔(Hanoi Tower),又称河内 ...
- 【PE806】Nim on Towers of Hanoi(汉诺塔游戏,生成函数)
PE:Project Euler 题意: 汉诺塔游戏是如下的问题:有三根柱子,第一根柱子套有 n n n 个圆盘,圆盘从上往下半径递增.每次操作可以把套在某根柱子上的最上面的那个圆盘移到另一个柱子上. ...
- 个人心得——hanoi问题 汉诺塔问题详细分析
简单地介绍题目 点进来的各位其实也应该不陌生了,三根柱子,N个圆盘,要求把所有的圆盘从第一根柱子放到第三根上,并且编号下面的圆盘不能放在编号上的圆盘上.这个问题其实知乎上有很多答主都答得不错,这里我想 ...
- 不会吧,不会吧,全网最细汉诺塔讲解,不会有人不知道吧。面试官直呼内行,看完只想默默找水喝(C语言)
最干hanoi,看完直呼口干舌燥 Hanoi(汉诺塔问题) 一.什么是汉诺塔 二.分析 1.移动过程 2.应用思想+函数雏形 3.部分代码 三.总代码 四.递归调用 OVER Hanoi(汉诺塔问题) ...
- 汉诺塔python代码解释_python实现汉诺塔算法
题目: 汉诺塔给出最优解,如果对汉诺塔的定义有不了解,请翻看数据结构教材. 除了最基本的之外,还有一题,给定一个数组,arr=[2,3,1,2,3],其含义是这是一个有5个圆盘的汉诺塔,每一个数字代表 ...
- 汉诺塔算法python_python实现汉诺塔算法
题目: 汉诺塔给出最优解,如果对汉诺塔的定义有不了解,请翻看数据结构教材. 除了最基本的之外,还有一题,给定一个数组,arr=[2,3,1,2,3],其含义是这是一个有5个圆盘的汉诺塔,每一个数字代表 ...
- C++Tower of Hanoi汉诺塔的实现算法(附完整源码)
C++Tower of Hanoi汉诺塔的实现算法 C++Tower of Hanoi汉诺塔的实现算法完整源码(定义,实现,main函数测试) C++Tower of Hanoi汉诺塔的实现算法完整源 ...
- 奇怪的汉诺塔 Four Column Hanoi Tower
奇怪的汉诺塔 题面 传送门 思路 首先考虑三个柱子的汉诺塔: 假设当前有 n n n个盘子: 先把前n-1个盘子从A柱移到B柱,然后把A柱上剩的那一个盘子移动到C柱最后把B柱上的那n-1个盘子移动到C ...
最新文章
- 2022-2028年中国康养旅游行业市场竞争力分析及发展策略分析报告
- 送一款巧克力式绝美键盘!真香!
- 什么是以太坊?它到底怎么运作的?
- RAISE_APPLICATION_ERROR用法
- mysql 启动 failed to start_Linux下启动MySQL提示“mysql deamon failed to start”错误的解决办法...
- Linq 通过反射动态查询对象
- [SpringSecurity]基本原理_过滤器链
- 用 Python 检验数据正态分布的几种方法
- TorchNet的学习笔记
- androidpn 推送初探
- ue设置MySQL_MySQL的安装与配置——详细教程 - Winton-Q
- java nekohtml_用过nekohtml的进来
- linux pip的安装路径,pip使用详解(包括pip install安装路径)
- Redis未授权访问缺陷让服务器沦为肉鸡
- Spring IoC容器设计原理及高级特性
- Android修行手册 - TextureView和SurfaceView的属性方法以及示例
- Unity异常退出日志存储位置
- Echarts官网展示
- ansible 配置使用大全资料
- logback各标签详解