ZOJ 2954 Hanoi Tower(模拟啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1953

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

代码如下:

#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);
            else
                printf("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;
    }
    else
        y = s[b].top();
    if(x > y)
    {
        flog = 1;
        step = ans;
        return;
    }
    else
    {
        s[b].push(x);
        s[a].pop();
    }
}
时间: 2024-08-08 09:42:38

ZOJ 2954 Hanoi Tower(模拟啊 )的相关文章

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 pe

3-6-汉诺塔(Hanoi Tower)问题-栈和队列-第3章-《数据结构》课本源码-严蔚敏吴伟民版

课本源码部分 第3章  栈和队列 - 汉诺塔(Hanoi Tower)问题 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接??? <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集解析使用说明        课本源码合辑  链接??? <数据结构>课本源码合辑        习题集全解析  链接??? <数据结构题集>习题解析合辑        本源码引入的文件  链接? 无外链        相关测试数据下载  链接? 无数

Hanoi Tower问题的求解

文章前部分为转载,转自http://www.cnblogs.com/yanlingyin/ 当然.这是一个经典的递归问题~   想必来看这篇博文的同学对汉诺塔应该不会陌生了吧, 写这篇博还是有初衷的: 之前学数据结构的时候自己看书.也上网上查了很多资料,资料都比较散.而且描述的不是很清楚,对于当时刚刚 接触算法的我,要完全理解还是有一定难度.今天刚好有时间就整理了下思路.重写分析了一下之前的疑惑的地方. 没有透彻的地方便都豁然开朗了.所以迫不及待把我的想法记录下来,和大家分享. 如果你也是和之前

汉诺塔 Hanoi Tower

一个古老的印度传说:在世界的中心贝拿勒斯的圣庙里,一块黄铜板上插着三支宝石针.印度教的主神梵天在创造世界的时候,在其中一根针上从下到上穿好了由大到小的64片金片,这就是所谓的汉诺塔(Hanoi Tower).不论白天黑夜,总有一个僧侣在按照下面的法则移动这些金片:一次只移动一片,不管在哪根针上,小片必须在大片上面. 僧侣们预言,当所有的金片从梵天穿好的金片上移到另一根针上时,世界末日就会来临,而梵塔.寺庙和众生也会随之灭亡...... 故事不多说了,汉诺塔是递归思想的典型应用,上代码: 1 #i

1028. Hanoi Tower Sequence

Description Hanoi Tower is a famous game invented by the French mathematician Edourard Lucas in 1883. We are given a tower of n disks, initially stacked in decreasing size on one of three pegs. The objective is to transfer the entire tower to one of

ZOJ-1239 Hanoi Tower Troubles Again!

链接:ZOJ1239 Hanoi Tower Troubles Again! Description People stopped moving discs from peg to peg after they know the number of steps needed to complete the entire task. But on the other hand, they didn't not stopped thinking about similar puzzles with

Codeforces Gym 100114 A. Hanoi tower 找规律

A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description you the conditions of this task. There are 3 pivots: A, B, C. Initially, n disks of different diameter are placed on the pivot A: the smallest dis

HDU 1329 Hanoi Tower Troubles Again!

Hanoi Tower Troubles Again! Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 132964-bit integer IO format: %I64d      Java class name: Main People stopped moving discs from peg to peg after they know the numbe

sicily 1028. Hanoi Tower Sequence

1028. Hanoi Tower Sequence Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Hanoi Tower is a famous game invented by the French mathematician Edourard Lucas in 1883. We are given a tower of n disks, initially stacked in decreasing size