博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
zoj 2954 Hanoi Tower 模拟
阅读量:3905 次
发布时间:2019-05-23

本文共 2585 字,大约阅读时间需要 8 分钟。

题目:

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

 

 

32 31 21 32 32 31 21 33 22 31 31 23 2

 

 

Sample Output

 

 

3-30

思路:

根据题目要求,利用三个栈来维护汉诺塔,然后根据题目要求判断即可。

注意初始化。

代码如下:

#include 
#include
#include
#include
#include
using namespace std;const int maxn=12005;int t;int n,m;int op1[maxn],op2[maxn];stack
st[5];int main(){ scanf("%d",&t); while(t--) { for (int i=1;i<=3;i++) { while(!st[i].empty())st[i].pop(); } scanf("%d%d",&n,&m); for (int i=1;i<=m;i++) { scanf("%d%d",&op1[i],&op2[i]); } for (int i=n;i>=1;i--) st[1].push(i); int ok=0,ans=0; for (int i=1;i<=m;i++) { int a=op1[i],b=op2[i]; if(!st[b].empty()&&st[a].top()>st[b].top()) { ans=-i; break; } int t=st[a].top(); st[a].pop(); st[b].push(t); if(st[3].size()==n) { ans=i; break; } } printf("%d\n",ans); } return 0;}

 

转载地址:http://ikoen.baihongyu.com/

你可能感兴趣的文章
自定义cscope-index
查看>>
(ubuntu)在andorid andk工程中使用ccache加速编译速度
查看>>
android graphics system学习资料汇总
查看>>
GDB
查看>>
Oracle RAC Failover 详解
查看>>
[转载]Oracle RAC客户端连接不稳定的解决方法
查看>>
ORA RAC ORA-12545:因目标主机或对象不存在,连接失败!
查看>>
证明两节点能正常failover的sql
查看>>
oracle10g rac 报ora-12545错误的解决方案 转载
查看>>
Linux配置Xmanager
查看>>
IP地址正则表达式
查看>>
对SOAP消息头的处理
查看>>
webservice TCP Monitor
查看>>
各系统下查看cpu物理和逻辑个数
查看>>
Oracle中sysdate的时区偏差
查看>>
【每日一算】旋转有序数组
查看>>
【每日一算】两数之和
查看>>
深入理解Mysql索引底层数据结构与算法
查看>>
B+tree结构详解
查看>>
B+树算法在mysql中能存多少行数据?
查看>>