传送门啦

首先要将第n个盘子从x到y,那么就要把比n小的盘子全部移到6-x-y,然后将n移到y

仔细想想:6代表的是3根初始柱,3根目标柱。

6-(x+y) 便是我们的中转柱了,因为到这个位置是最优的。

感觉题目有锅啊。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 55; inline int read(){
char ch = getchar();
int f = 1 , x = 0;
while(ch > '9' || ch < '0'){if(ch == '-')f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + ch - '0';ch = getchar();}
return x * f;
} int n,flag,x,ans;
int a[maxn],b[maxn]; void dfs(int x,int dep){
if(a[x] == dep) return;
for(int i=x-1;i>=1;i--)
dfs(i , 6 - dep - a[x]);
printf("move %d from %c to %c\n",x,a[x] + 64 , dep + 64);
a[x] = dep;
ans++;
} int main(){
n = read();
if(n == 3){
puts("move 3 from A to B");
puts("move 1 from C to B");
puts("move 2 from C to A");
puts("move 1 from B to A");
puts("move 3 from B to C");
puts("5");
return 0;
}
flag = read();
for(int i=1;i<=flag;i++){x = read();a[x] = 1;}
flag = read();
for(int i=1;i<=flag;i++){x = read();a[x] = 2;}
flag = read();
for(int i=1;i<=flag;i++){x = read();a[x] = 3;}
flag = read();
for(int i=1;i<=flag;i++){x = read();b[x] = 1;}
flag = read();
for(int i=1;i<=flag;i++){x = read();b[x] = 2;}
flag = read();
for(int i=1;i<=flag;i++){x = read();b[x] = 3;}
for(int i=n;i>=1;i--)
dfs(i , b[i]);
printf("%d",ans);
return 0;
}

说明:本人蒟蒻,第11个hack数据至今没过,就90分的代码 + 偷偷打表(嘘)。

洛谷P1242 新汉诺塔的更多相关文章

  1. 洛谷P1242 新汉诺塔(dfs,模拟退火)

    洛谷P1242 新汉诺塔 最开始的思路是贪心地将盘子从大到小依次从初始位置移动到目标位置. 方法和基本的汉诺塔问题的方法一样,对于盘子 \(i\) ,将盘子 \(1\to i-1\) 放置到中间柱子上 ...

  2. 洛谷 P1242 新汉诺塔

    原题链接 题目描述 设有n个大小不等的中空圆盘,按从小到大的顺序从1到n编号.将这n个圆盘任意的迭套在三根立柱上,立柱的编号分别为A.B.C,这个状态称为初始状态. 现在要求找到一种步数最少的移动方案 ...

  3. 洛谷P1242 新汉诺塔 【神奇的递归】

    题目描述 设有n个大小不等的中空圆盘,按从小到大的顺序从1到n编号.将这n个圆盘任意的迭套在三根立柱上,立柱的编号分别为A.B.C,这个状态称为初始状态. 现在要求找到一种步数最少的移动方案,使得从初 ...

  4. P1242 新汉诺塔(搜索+模拟退火)

    题目链接:传送门 题目大意: 汉诺塔,给定n个盘子(n <= 45),起始状态和结束状态,求最小的步数以及路径. 思路: 考虑用dfs贪心地将剩余最大盘归位. #include<bits/ ...

  5. BZOJ1019 汉诺塔/洛谷P4285 [SHOI2008]汉诺塔

    汉诺塔(BZOJ) P4285 [SHOI2008]汉诺塔 居然是省选题,还是DP!(我的DP菜得要死,碰见就丢分) 冥思苦想了1h+ \(\to\) ?! 就是普通的hanoi NOI or HNO ...

  6. P1242 新汉诺塔(hanio)

    这道题加深了hanio的理解 如果我们要移动第n个盘子.那么就是说,n+1以后(包括n+1)的盘子都已经到位了 #include<iostream> #include<cstdio& ...

  7. P1242 新汉诺塔

    题目描述 设有n个大小不等的中空圆盘,按从小到大的顺序从1到n编号.将这n个圆盘任意的迭套在三根立柱上,立柱的编号分别为A.B.C,这个状态称为初始状态. 现在要求找到一种步数最少的移动方案,使得从初 ...

  8. 大白_uva10795_新汉诺塔

    题意:给出所有盘子的初态和终态,问最少多少步能从初态走到终态,其余规则和老汉诺塔一样. 思路: 若要把当前最大的盘子m从1移动到3,那么首先必须把剩下的所有盘子1~m-1放到2上,然后把m放到3上. ...

  9. UVA 10795 新汉诺塔问题

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. phpredis -- redis_cluster

    https://github.com/phpredis/phpredis/tree/feature/redis_cluster https://github.com/phpredis/phpredis

  2. mysql source导入多个sql文件

    mysql>use dbtest; mysql>set names utf8; mysql>source D:/mysql/all.sql; 通过source命令导入多个文件,可以新 ...

  3. PACS&DICOM

    What is DICOM, PACS, and Workstation? What is DICOM? We will take them one at a time – So first of a ...

  4. spring cloud-zuul的Filter详解(十一)

    本文转自:http://blog.csdn.net/liuchuanhong1/article/details/62236793 关于网关的作用,这里就不再次赘述了,我们今天的重点是zuul的Filt ...

  5. web项目中classPath指的是哪里?

    classpath可以是SRC下面的路径 但是项目最终编译会到WEB-INF下面,所以有时候WEB-INF下面的classes也可以放配置文件,也可以读取到. 因为最终src都会放到WEB-INF下面 ...

  6. 《xxx重大需求征集系统的》可用性和可修改性战术分析

    阅读<大型网站技术架构:核心与案例分析>,第五,六章,结合<河北省重大需求填报系统>,列举实例分析采用的可用性和可修改性战术,将上述内容撰写成一篇1500字左右的博客阐述你的意 ...

  7. pyqt5的代码

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  8. python中高阶函数与装饰器(2)

    函数返回值为内置函数名: def sum(*args):    def sum_in():        ax = 0        for n in args:            ax = ax ...

  9. bzoj千题计划135:bzoj1066: [SCOI2007]蜥蜴

    http://www.lydsy.com/JudgeOnline/problem.php?id=1066 每个柱子拆成两个点 i<<1,i<<1|1,之间连流量为高度的边 如果 ...

  10. android onActivityResult的执行

    1.如果activity中重写了onActivityResult函数,同时添加在该activity的fragment也重写了onActivtyResult函数,那么会执行Activity的onActi ...