zoj 2954 Hanoi Tower(汉诺塔)

来源:互联网 发布:linux测试环境搭建 编辑:程序博客网 时间:2024/06/06 10:06

转载请注明出处: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

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

Sample Output

3-30


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();}}



2 0