题目链接:传送门

题目大意:

汉诺塔,给定n个盘子(n <= 45),起始状态和结束状态,求最小的步数以及路径。

思路:

考虑用dfs贪心地将剩余最大盘归位。

#include<bits/stdc++.h>

using namespace std;
const int MAX_N = ;
const int SUM = ; int N, ans;
int f1[MAX_N], f2[MAX_N]; void dfs(int cur, int st, int ed, bool now)
{
int mid = SUM - st - ed;
if (st == ed) {
if (cur > )
dfs(cur-, f1[cur-], now ? f2[cur-] : ed, now);
return;
}
if (cur > )
dfs(cur-, f1[cur-], mid, false);
ans++;
printf("move %d from %c to %c\n", cur, 'A' + st, 'A' + ed);
f1[cur] = ed;
if (cur > )
dfs(cur-, f1[cur-], now ? f2[cur-] : ed, now);
} void input()
{
ans = ;
cin >> N;
for (int i = ; i < ; i++) {
int x;
cin >> x;
while (x--) {
int cur;
cin >> cur;
if (i/)
f2[cur] = i%;
else
f1[cur] = i%;
}
}
} int main(){
input();
dfs(N, f1[N], f2[N], true);
cout << ans << endl;
return ;
}

以上代码会被这组数据hack。

/*
3
1 3
0
2 2 1
2 2 1
0
1 3
*/

但是大多数情况下贪心思路没有问题,所以用模拟退火优化。

#include<bits/stdc++.h>

using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX_N = ;
const int SUM = ; int N, ans, icur;
string sans, scur;
int ff1[MAX_N], ff2[MAX_N];
int f1[MAX_N], f2[MAX_N]; void mov(int cur, int st, int ed)
{
icur++;
scur += "move ";
if (cur >= )
scur += char(cur/ + '');
scur += char(cur% + '');
scur += " from ";
scur += char(st + 'A');
scur += " to ";
scur += char(ed + 'A');
scur += "\n";
} void dfs(int cur, int st, int ed, bool now)
{
int mid = SUM - st - ed;
if (st == ed) {
if (cur > )
dfs(cur-, f1[cur-], now ? f2[cur-] : ed, now);
return;
}
if (cur > )
dfs(cur-, f1[cur-], mid, false);
mov(cur, st, ed);
f1[cur] = ed;
if (cur > )
dfs(cur-, f1[cur-], now ? f2[cur-] : ed, now);
} void input()
{
ans = INF;
cin >> N;
for (int i = ; i < ; i++) {
int x;
cin >> x;
while (x--) {
int cur;
cin >> cur;
if (i/)
ff2[cur] = i%;
else
ff1[cur] = i%;
}
}
} int main(){
input();
int T = ;
srand();
while (T--) {
icur = ;
scur = "";
for (int i = ; i <= N; i++) {
f1[i] = ff1[i];
f2[i] = ff2[i];
}
for (int i = N; i >= ; i--) {
if (rand()%(i+) != )
dfs(i, f1[i], f2[i], true);
else
dfs(i, f1[i], SUM-f1[i]-f2[i], true);
}
dfs(N, f1[N], f2[N], true);
if (ans > icur) {
ans = icur;
sans = scur;
}
}
cout << sans << ans << endl;
return ;
}
/*
3
1 3
0
2 2 1
2 2 1
0
1 3
*/

P1242 新汉诺塔(搜索+模拟退火)的更多相关文章

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

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

  2. 洛谷 P1242 新汉诺塔

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

  3. 洛谷P1242 新汉诺塔

    传送门啦 首先要将第n个盘子从x到y,那么就要把比n小的盘子全部移到6-x-y,然后将n移到y 仔细想想:6代表的是3根初始柱,3根目标柱. 6-(x+y) 便是我们的中转柱了,因为到这个位置是最优的 ...

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

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

  5. P1242 新汉诺塔(hanio)

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

  6. P1242 新汉诺塔

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

  7. 大白_uva10795_新汉诺塔

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

  8. UVA 10795 新汉诺塔问题

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

  9. UVa新汉诺塔问题(A Different Task,Uva 10795)

    主要需要理递归函数计算 #define MAXN 60+10 #include<iostream> using namespace std; int n,k,S[MAXN],F[MAXN] ...

随机推荐

  1. this 锁与 static 锁

    一. this 锁 同步函数其实用到的锁就是 this 锁,为什么他用到的是 this 锁呢?为了证实这个结论我 们本节将会有两个实验性的程序来作为支撑,说服自己和读者证明同步函数用到的就是 this ...

  2. axios 参数为payload的解决方法

    1. 添加头部headers headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, axios.post(url, {a ...

  3. windows 文本操作

    type命令 作用:显示文件类文件内容 (例如:.txt .Java .cpp .c .html 等属于文本类文件 .word excel ppt exe dll 等都不属于文本类文件) fc命令 作 ...

  4. laravel5.2 开发中打印sql语句

    在 AppServiceProvider 的boot方法中写入 use DB;use Event; if ( env('APP_ENV') == 'dev' ) { DB::connection()- ...

  5. .NetCore发布到Centos docker

    将.netcore mvc项目发布到centos7的docker中.环境 vmware14+Centos7+docker-ce 1.使用vs将.netcoremvc项目发布到本地,修改发布后的目录 名 ...

  6. mac mysql 操作

    参考 http://www.cnblogs.com/chenmo-xpw/p/6102933.html http://www.cnblogs.com/uoar/p/6492521.html 1.启动M ...

  7. CCF关于对NOIP2018复赛违规处罚的公告

    NOIP2018复赛于11月10-11日在全国31个赛区同时举行,现已结束.总体有序,但也有赛区出现违规现象.现将复赛中违规情况进行通报. 一.数据提交情况 CCF要求NOI各省组织单位在考试结束后在 ...

  8. java唯一ID生成

    有时我们不依赖于数据库中自动递增的字段产生唯一ID,比如多表同一字段需要统一一个唯一ID,这时就需要用程序来生成一个唯一的全局ID,然后在数据库事务中同时插入到多章表中实现同步. 在java中有个类工 ...

  9. oracle中的对象创建及删除语句【原创】

    oracle对象 1.表 a)创建表1 create table students( id number(10), stuno number(10) , sex varchar2(2), age in ...

  10. NTT模板(无讲解)

    #include<bits/stdc++.h>//只是在虚数部分改了一下 using namespace std; typedef long long int ll; ; ; ; ; ll ...