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[]; int movef(int x,char a,char b,char c)
{
if(x==)
{
if(s[x-]==a||s[x-]==b) return ;
}
else
{
if(s[x-]==a) return movef(x-,a,c,b);
if(s[x-]==b) return movef(x-,c,b,a);
}
return ;
} 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\n");else printf("NO\n");
}

Codeforces Gym 100114 A. Hanoi tower 找规律的更多相关文章

  1. codeforces Gym 100418D BOPC 打表找规律,求逆元

    BOPCTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  2. Codeforces Gym 100015A Another Rock-Paper-Scissors Problem 找规律

    Another Rock-Paper-Scissors Problem 题目连接: http://codeforces.com/gym/100015/attachments Description S ...

  3. Codeforces Gym 100425D D - Toll Road 找规律

    D - Toll RoadTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...

  4. Codeforces GYM 100114 C. Sequence 打表

    C. Sequence Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description ...

  5. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  6. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  7. Codeforces Gym 100114 H. Milestones 离线树状数组

    H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...

  8. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  9. Codeforces GYM 100114 B. Island 水题

    B. Island Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description O ...

随机推荐

  1. InnoDB一定会在索引中加上主键吗

    InnoDB一定会在索引中加上主键吗 http://www.penglixun.com/tech/database/will_innodb_store_pk_in_index.html

  2. delphi ole word

    源代码如下: //Word打印(声明部分) wDoc,wApp:Variant; function PrnWordBegin(tempDoc,docName:String):boolean; func ...

  3. Codeforces 167B Wizards and Huge Prize(概率dp)

    题意: n个人,开始有一个容量为k得背包,击败一个人背包可以获得一定容量或得到一个财富(放入背包内),给出击败每个人的概率,求至少击败l个人,且背包容量大于获得的总财富值的概率 分析: 状态好确定,d ...

  4. cocos2d-x 详解之 CCAction(动作)

    关于动作部分,总的来说使用起来比较简单,创建一个动作,然后让可渲染节点如精灵去执行这个动作即可.cocos2dx提供了很多类型的动作,使用起来也很方便.本节重点介绍动作CCAction的子类之一时间动 ...

  5. MongoDB@入门一

    安装MongoDB自行搜索, 我这里提供GUI版本类似navicat. 1. 数据库层面 show dbs #查看服务器上的数据库  [local  0.000GB] use test  #切换到指定 ...

  6. RPC框架motan: 通信框架netty( 1)

    服务器端编程都离不开底层的通信框架,在我们刚学习java的时候,主要接触都是Socket和ServerSocket 的阻塞编程,后来开始了解NIO,这种非阻塞的编程模式,它可以一个线程管理很多的Soc ...

  7. Tkinter教程之Label篇

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811293 #Tkinter教程之Label篇'''1.Label的第一个例子text属性使用 ...

  8. leetcode—sqrt

    1.题目描述   Implement int sqrt(int x).   Compute and return the square root of x. 2.解法分析 很明显,用二分搜索可解,但是 ...

  9. .net组件技术

    .NET是什么? •.NET是一个平台,而不是一种语言. •.NET是Microsoft的用以创建XML Web服务(下一代软件)平台,该平台将信息.设备和人以一种统一的.个性化的方式联系起来.   ...

  10. MSSql得到表的结构和字段

    得到数据库中所有的表 select name from sysobjects where xtype='u' and name='{0}' 1.获取表的基本字段属性 --获取SqlServer中表结构 ...