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. Informatica 9.1常用查询

    select a.mapping_name, a.mapping_id, a.subject_id, a.is_valid, b.pv_precision, c.pv_value, b.pv_defa ...

  2. POJ 2136 Vertical Histogram

    题意:按样例那样模拟…… 解法:模拟…… 代码: #include<stdio.h> #include<iostream> #include<algorithm> ...

  3. hdu 2457(ac自动机+dp)

    题意:容易理解... 分析:这是一道比较简单的ac自动机+dp的题了,直接上代码. 代码实现: #include<stdio.h> #include<string.h> #in ...

  4. HDU5764 After a Sleepless Night 树形乱搞题

    分析(官方题解): 假设根已确定,可以发现新树若合法,需满足以下性质:根节点是n:儿子的值不大于父亲:具有相同值的节点形成一条链,并且链不会发生“分叉”(即有多个最低点).所以对于新树中有出现的值x, ...

  5. Away 3D 之 交互和渐变----Interactivity and Tweening

    在这个教程中,你将学会如何创建一个地板对象,本教程中的地板是可交互的并且能够移动小方块到鼠标的点击的地方. 1. 设置场景: 你正在创建的场景包含了一个平面,地板和一个看起来像一个饰品的方块,还有一个 ...

  6. 基本输入输出系统BIOS---显示输出

    显示器通过显示适配卡与系统相连, 显示适配卡是显示输出的接口卡,照相的显示器是CGA和EGA,目前的显示适配卡是VGA和TVGA,他们都支持两种显示方式,文本显示和图形显示 在BIOS中提供的显示I/ ...

  7. [转] Web前端优化之 Cookie篇

    原文链接: http://lunax.info/archives/3095.html Web 前端优化最佳实践第三部分面向 Cookie .目前只有 2 条实践规则. 1. 缩小 Cookie (Re ...

  8. 超简单fedora20(linux)下JDK1.8的安装

    (博客园-番茄酱原创) 去官网下载linux版本的jdk,如果你的fedora是64位,就选择64位的jdk,jdk-8u20-linux-x64.tar.gz. 将下载好的jdk解压到当前目录下,解 ...

  9. HDU-4749 Parade Show KMP算法 | DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题意:给两个串S和P,求S串中存在多少个与P串的大小关系一样的串. 因为数字的范围是1<= ...

  10. linux 和 android 源码的 cross reference (即网页浏览代码的引用)

    linux: http://lxr.free-electrons.com/               相当好 android: http://androidxref.com