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. Android设计模式源码解析之桥接模式

    模式介绍 模式的定义 将抽象部分与实现部分分离,使它们都可以独立的变化. 模式的使用场景 如果一个系统需要在构件的抽象化角色和具体化角色之间添加更多的灵活性,避免在两个层次之间建立静态的联系. 设计要 ...

  2. 安装--SambaServce

    参考地址:快跑蚂蚁的linux之旅--redhat安装配置samba实验win共享linux主机目录 1.使用rpm -qa|grep "samba",查看samba安装包是否安装 ...

  3. [Irving] Wpf DevexPress GridControl 获取选中行

    WPF前台绑定事件代码: <RelayAction TargetControl="{Binding ElementName=GCInstoragePart}" MethodN ...

  4. 读pomelo的教程-1

    pomelo教程的例子是一个聊天室,包括一个webserver客户端,和一个gameserver的pomelo服务器.这个例子挺好,一个聊天系统逻辑简单,还包括了用户管理,客户端request,服务器 ...

  5. 大连网络赛 1006 Football Games

    //大连网络赛 1006 // 吐槽:数据比较水.下面代码可以AC // 但是正解好像是:排序后,前i项的和大于等于i*(i-1) #include <bits/stdc++.h> usi ...

  6. Node.js中的事件

    var util = require("util"); //导入util模块 var events = require("events"); //导入event ...

  7. Python覆盖率分析工具_Coverage

    easy_install安装: easy_install coverage 运行: coverage run test.py coverage report

  8. elisp语法

    http://stackoverflow.com/questions/3855862/setq-and-defvar-in-lisp defvar, let, setq的语法分析: defvar in ...

  9. SpringDataMongoDB介绍(二)-MongoOperations介绍

    MongoOperations是一个很强大的接口,有了这个接口,基本上什么都搞定了. 其介绍 Interface that specifies a basic set of MongoDB opera ...

  10. RSS阅读器(一)——dom4j读取xml(opml)文件

    接触java不久,偶有收获,最近想做一个web版RSS阅读器来锻炼一下.手头有几个从不同版本的foxmail中导出的opml文件,大家应该都知道,opml文件就是xml格式的.那么就先从这里入手,练习 ...