zoukankan      html  css  js  c++  java
  • 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
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<string.h>
     4 #include<stack>
     5 using namespace std;
     6 stack<int> a[3];
     7 int b[60];
     8 int q=0;
     9 int solve(int from,int to)
    10 {
    11     if(a[from].empty())
    12     {//-1有错
    13         return -1; 
    14     }
    15     if(!a[to].empty())//a[to].top()有可能为空,要先进行判断
    16         if(a[from].top()>a[to].top())
    17             return -1; 
    18     a[to].push(a[from].top());
    19     a[from].pop();
    20     return 0;
    21 }
    22 void empty()
    23 {
    24     int i;
    25     for(i=0;i<3;i++)
    26     {
    27         while(!a[i].empty())
    28             a[i].pop();
    29     }
    30 }
    31 int main()
    32 {
    33     int T,n,m,i,j,from,to,get;
    34     while(cin>>T)
    35     {
    36         while(T--)
    37         {
    38             cin>>n>>m;
    39             for(i=n;i>0;i--)
    40                 a[0].push(i);
    41             for(j=1;j<=m;j++)
    42             {
    43                 cin>>from>>to;
    44                 get=solve(from-1,to-1);
    45                 if(get==-1)
    46                 {
    47                     b[q++]=0-j;
    48                     while(j<m)//要等输入完成才能退出本次循环
    49                     {
    50                         j++;
    51                         cin>>from>>to;
    52                     }
    53                     cout<<b[q-1]<<endl;
    54                     break;
    55                 }
    56                 if(a[0].empty()&&a[1].empty()&&a[2].top()==1)
    57                 //if(a[2].size()==n)
    58                 {    
    59                     b[q++]=j;
    60                     while(j<m)
    61                     {
    62                         j++;
    63                         cin>>from>>to;
    64                     }
    65                     cout<<b[q-1]<<endl;
    66                     break;
    67                 }else if(j==m)
    68                 {    
    69                     b[q++]=0;
    70                     cout<<b[q-1]<<endl;
    71                 }
    72             }    
    73             empty();//退出时应该把栈的内容清空
    74         }
    75     }
    76     return 0;
    77 }


  • 相关阅读:
    排序-计数-优化版
    排序-计数-基础版
    排序-归并
    Unity战斗模块之角色继承设计---1.1
    Unity中保存和读取数据的类---PlayerPrefs
    《计算机图形学》 第一章 基础知识--02向量(二维)
    《计算机图形学》 第一章 基础知识--01下载和安装DirectX,配置VS编辑器
    第四章 002-条件语句
    第四章 001-复合语句
    第三章 004-运算符
  • 原文地址:https://www.cnblogs.com/xdbingo/p/4800140.html
Copyright © 2011-2022 走看看