zoukankan      html  css  js  c++  java
  • 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 disk is placed on the top and every next one is placed in an increasing order of their diameters. The second and the third pivots are still empty. You have to move all the disks from pivot A to pivot B, using pivot C as an auxiliary. By one step you can take off 1 upper disk and put it either on an empty pivot or on another pivot over a disk with a bigger diameter. Almost all books on programming contain a recursive solution of this task. In the following example you can see the procedure, written in Pascal. Procedure Hanoi (A, B, C: integer; N:integer); Begin If N>0 then Begin Hanoi (A, C, B, N-1); Writeln(‘диск ’, N, ‘ from ‘, A, ‘ to ‘, B); Hanoi (C, B, A, N-1) End End; The number of step Disk From To Combination 0. AAA 1. 1 A B BAA 2. 2 A C BCA 3. 1 B C CCA 4. 3 A B CCB 5. 1 C A ACB 6. 2 C B ABB 7. 1 A B BBB It is well known that the solution given above requires (2n –1) steps. Taking into account the initial disposition we totally have 2n combinations of n disks disposition between three pivots. Thus, some combinations don’t occure during the algorithm execution. For example, the combination «CAB» will not be reached during the game with n = 3 (herein the smallest disk is on pivot C, the medium one is on pivot A, the biggest one is on pivot B). Write a program that establishes if the given combination is occurred during the game.

    Input

    The first line of an input file contains a single integer n – the number of disks, and the second line contains n capital letters (“A”, “B” or “C”) – the disposition of the n disks between the pivots. The first (leftmost) letter designates position (a pivot name) of the smallest disk, the second letter – position of the second lagest, and so on…
    1 ≤ n ≤ 250

    Output

    The output file must contain “YES” or “NO” depending on the reachability of the disks disposition during the game.

    Sample Input

    3

    ACB

    Sample Output

    YES

    HINT

     

    题意

    汉诺塔,给你个状态,问你由题中所给的代码是否能跑到这个状态

    题解:

    找规律,找规律……

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    
    int n,flag;
    char s[400];
    
    int movef(int x,char a,char b,char c)
    {
        if(x==1)
        {
            if(s[x-1]==a||s[x-1]==b) return 1;
        }
        else
        {
            if(s[x-1]==a) return movef(x-1,a,c,b);
            if(s[x-1]==b) return movef(x-1,c,b,a);
        }
        return 0;
    }
    
    main()
    {
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
        scanf("%d",&n);
        scanf("%s",s);
        flag=movef(n,'A','B','C');
        if(flag) printf("YES
    ");else printf("NO
    ");
    }
  • 相关阅读:
    Linux find 用法示例
    PostgreSQL 之 yum安装 postgis 插件
    解决sql中上下左右backspace不能用的方法
    MySQL 之 MySQL数据库的优化
    mysql命令行批量插入100条数据命令
    MySQL 之 MyTop实时监控MySQL
    MySQL 之 mysqlbinlog解析binlog乱码问题解密
    MySQL 之数据库增量数据恢复案例
    Mysql 之多实例 安装以及配置
    mysql 之审计 init-connect+binlog完成审计功能
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4780539.html
Copyright © 2011-2022 走看看