[SHOI2008]汉诺塔_weixin_30767921的博客-程序员宝宝

题意

规则同汉诺塔,强制规定移动操作的优先级,每次选择合法的优先级最高的操作,两次操作不能移动同一个盘子,保证有解,求移动次数

思路

将普通汉诺塔问题的思路用在这道题上面,容易证明\(f\)满足线性递推关系:\(f[i]=k*f[i-1]+b\),暴力\(dfs\)出前三个\(f\),就可以求出\(k=\frac{f[3]-f[2]}{f[2]-f[1]},b=f[2]-f[1]*k\)

Code

#include<bits/stdc++.h>
#define N 35 
using namespace std;
typedef long long ll;
int n;
char a[7][3];
ll f[N],k,b;
int st[4][4],top[4];

template <class T>
void read(T &x)
{
    char c;int sign=1;
    while((c=getchar())>'9'||c<'0') if(c=='-') sign=-1; x=c-48;
    while((c=getchar())>='0'&&c<='9') x=x*10+c-48; x*=sign;
}

void dfs(int step,int opt,int las)
{
    if(top[2]==opt||top[3]==opt) {f[opt]=step; return;}
    for(int i=1;i<=6;++i)
    {
        int fr=a[i][0]-'A'+1,to=a[i][1]-'A'+1;
        if(!top[fr]) continue;
        if(st[fr][top[fr]]==las) continue;
        if(top[to]&&st[fr][top[fr]]>st[to][top[to]]) continue;
        st[to][++top[to]]=st[fr][top[fr]];
        --top[fr];
        dfs(step+1,opt,st[to][top[to]]);
        break;
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=6;++i) scanf("%s",a[i]);
    for(int i=1;i<=3;++i) top[i]=0;
    st[1][++top[1]]=1;
    dfs(0,1,-1);
    for(int i=1;i<=3;++i) top[i]=0;
    st[1][++top[1]]=2; st[1][++top[1]]=1;
    dfs(0,2,-1);
    for(int i=1;i<=3;++i) top[i]=0;
    st[1][++top[1]]=3; st[1][++top[1]]=2; st[1][++top[1]]=1;
    dfs(0,3,-1);
    k=(f[3]-f[2])/(f[2]-f[1]);
    b=f[2]-f[1]*k;
    for(int i=4;i<=n;++i) f[i]=k*f[i-1]+b;
    printf("%lld\n",f[n]);
    return 0;
}

转载于:https://www.cnblogs.com/Chtholly/p/11436912.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30767921/article/details/101930540

智能推荐

使用java将数据写入文件,并下载到客户端_小糖豆巴拉巴拉的博客-程序员宝宝

最近做了一个功能,觉得挺有意思,决定记录下来,以前也见过类似的功能,以为很高端,其实很简单。第一步:写一个创建文件的工具类public class CSVUtils{ /** * CSV文件生成方法 * @param head * @param dataList * @param outPutPath * @param filename ...

用ASP.NET创建数据库_weixin_30648587的博客-程序员宝宝

小白的第一次使用:程序员写程序,就好比一个物品的慢慢诞生,我们今天的这个例子就可以想象成一个物品慢慢的在编译的过程中,让我们所看到一、创建我们所测试的项目  1.创建一个简单的带有模型层(Model)和数据访问层(DAL)的控制台应用程序架构。DAL:数据访问层,实现对数据库的操作控制Model:模型层,创建表Text:控制台,进行控制二、开始创建模型...

com.alibaba.fastjson.JSONException: illegal identifier : \pos 1, line 1, column 2_illegal identifier : \pos 1, line 1, column 2{__Mr. White的博客-程序员宝宝

对json字符串进行转对象操作时报错:com.alibaba.fastjson.JSONException: illegal identifier : \pos 1, line 1, column 2at com.alibaba.fastjson.parser.JSONLexerBase.scanSymbolUnQuoted(JSONLexerBase.java:830) ~[fastjson-1.2.59.jar:?] at com.alibaba.fastjson.parser.DefaultJS

tensorflow-gpu运行测试代码,卡在 I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu_卡在created tensorflow device_是江姑娘呀的博客-程序员宝宝

新建一个环境解释器后,想要运行测试代码如下,会发现不管是cmd运行还是pycharm运行,都会卡在“Device mapping”的上一句I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0然后就不动了如下图,我试着重新运行了两次就好了,貌似第一次运行GPU的时候就会卡在这里会...

随便推点

mysql模糊查询语句怎么不区分大小写_mysql模糊查询不区分大小写_劭兮劭兮的博客-程序员宝宝

近期,一直在忙着写一个小小的个人博客项目,在实现 “全局搜索” 功能时,一直想让 “全局搜索” 功能实现**“不区分大小写”**,方法介绍如下:(在本小白的另外一篇博客中,介绍的比较详细,有兴趣的可以看一下:mysql模糊查询语句是否区分大小写?)方法一:设置“COLLATE”属性值为“utf8_general_ci”,mysql采用utf8mb4编码格式,模糊查询不区分大小写方法二:在创建表的时候,指定表字段COLLATE 为“utf8_general_ci”,或者修改指定表字段COLLATE.

springboot项目中dubbo应用启动报错_sinJack的博客-程序员宝宝

主要报错信息:Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled.com.alibaba.dubbo.remoting.RemotingException: Failed to bind NettyServer on /192.168.65.1:20880, cause: Failed to bind to: /0.0.0.0:20

解决Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4_weixin_30855099的博客-程序员宝宝

Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from http://uk.maven.org/maven2 was cached in the local repository, resolution will not be reattempted until the upda...

Springboot设置跨域的三种方式_剑雪封喉r的博客-程序员宝宝

方式一(精细配置)在需要跨域的整个Controller或者单个方法上添加@CrossOrigin注解方式二(全局配置)@ConfigurationpublicclassWebMvcConfigextendsWebMvcConfigurerAdapter{@OverridepublicvoidaddCorsMappings(CorsRegistryregistry){registry.addMapping("/**")...

sql convert 转换时间格式_chen29717的博客-程序员宝宝

将sqlserver中table表的[datetime]字段值‘2007-11-07 16:41:35.033’ 改为‘2007-11-07 00:00:00‘去除了时分秒.[datetime]字段要为datetime类型的哦. UPDATE table SET [datetime]= Convert(char(11),[datetime],120)获取当前日期利用 convert 来转换成我们需要的datetime格式.select CONVERT(varchar(12) , getdate(), 1

ubuntu12.04下安装QT_ubuntu 12.04 安装qt_sengeiou的博客-程序员宝宝

下载QT creator  :地址:http://qt-project.org/downloads下面方法小白的做法  有不对的地方希望大牛指出  刚入门Qt   不知如何安装学习   求指教一:输入以下命令:<[email protected] {margin:0.79in}p {margin-bottom:0.08in; direction:ltr;

推荐文章

热门文章

相关标签