Hanoi Tower

Description

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

这道题意思是要求模拟汉诺塔进行操作,如果全部在第三根柱子上就输出达到的第i个操作,
非法操作就输出-i,其他为0.
#include <stdio.h>
#include <vector>
std :: vector < int > mp[4];
const int maxn = 12005;
int from[maxn], to[maxn];
void init ( int n )
{
    for ( int i = 0; i < 4; i ++ )
        mp[i].clear ( );    //清空
    for ( int i = n; i >= 1; i -- )
        mp[1].push_back ( i );  //将1-n进栈至1
}
int main ( )
{
    int T, n, m, ans;
    scanf ( "%d", &T );
    while ( T -- )
    {
        ans = 0;    //初始化为0,中间没有异常操作就输出0
        scanf ( "%d%d", &n, &m );
        init ( n );
        for ( int i = 1; i <= m; i ++ )
            scanf ( "%d%d", &from[i], &to[i] );
        for ( int i = 1; i <= m; i ++ )
        {
            int u = from[i], v = to[i], len = mp[u].size ( );
            if ( len >= 1 )
            {
                int a = mp[u][len-1], l = mp[v].size ( );
                if ( l < 1 || a < mp[v][l-1] )
                {
                    mp[u].pop_back ( ); //出栈
                    mp[v].push_back ( a );  //进栈
                }
                else    //错误操作就退出并赋为负数
                {
                    ans = -i;
                    break ;
                }
            }
            else    //无法出栈
            {
                ans = -i;
                break ;
            }
            if ( mp[3].size ( ) == n )  //第三个标签达到n个就完成
            {
                ans = i;
                break ;
            }
        }
        printf ( "%d\n", ans );
    }
    return 0;
}

 

你可能感兴趣的:(Hanoi Tower)