HDU 2511 汉诺塔X
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2511
Input第1行是整数T,表示有T组数据,下面有T行,每行2个整数n (1 ≤ n ≤ 63) ,m≤ 2^n-1
Output输出第m次移动的盘子号数和柱子的号数.
Sample Input
4
3 2
4 5
39 183251937942
63 3074457345618258570
Sample Output
2 1 2
1 3 1
2 2 3
2 2 3
模拟(+递归)盘子移动的过程:
1.如果m=a[n-1]+1,即刚好移动n号盘子,移动方向A-->C
2.如果m>a[n-1],则考虑n-1号盘子,移动方向是B-->C,移动次数是m-(a[n-1]+1)
3.如果m<=a[n-1],考虑n-1个盘子,移动方向是A-->B,移动次数是m;
4.重复1,2,3点
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
bool cmp(int x,int y)
{
return x>y;
}
const int N=;
const int mod=1e9+;
int n;
ll m,a[];
void hanio(int x,int y,int z,int n,ll m)
{
if(m==a[n-]+){
printf("%d %d %d\n",n,x,z);
return ;
}
if(m>a[n-]) hanio(y,x,z,n-,m-a[n-]-);
if(m<=a[n-]) hanio(x,z,y,n-,m);
}
int main()
{
int t,i;
scanf("%d",&t);
a[]=;
for(i=;i<=;i++) {
a[i]=*a[i-]+;
}
while(t--){
scanf("%d%lld",&n,&m);
hanio(,,,n,m);
}
return ;
}
HDU 2511 汉诺塔X的更多相关文章
- HDU 1207 汉诺塔II (找规律,递推)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1207 汉诺塔II Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 2077 汉诺塔IV (递推)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2077 还记得汉诺塔III吗?他的规则是这样的:不允许直接从最左(右)边移到最右(左)边(每次移动一定是 ...
- HDU 2064 汉诺塔III (递推)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2064 约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下.由小到 ...
- hdu 1207 汉诺塔II (DP+递推)
汉诺塔II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- HDU 2064 汉诺塔III
汉诺塔III Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HDU 2064 汉诺塔III(递归)
题目链接 Problem Description 约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下.由小到大顺序串着由64个圆盘构成的塔.目的是将最左边杆上的盘 ...
- HDU 1207 汉诺塔II (递推)
经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往上按大小顺序摞着64片黄金圆盘.上 ...
- HDU——2064汉诺塔III
汉诺塔III Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU 1997汉诺塔VII
又是汉诺塔~ 回顾一下汉诺塔的移动过程. 从左到右设为A,B,C 3个盘子的时候 1: No.1 A -> C 2: No.2 A -> B 3: No.1 C -> B 4 ...
随机推荐
- mysql explain中的 “Select tables optimized away”
mysql explain中的 “Select tables optimized away” http://blog.chinaunix.net/uid-10449864-id-2956845.htm ...
- python创建有序字典及字典按照值的大小进行排序
有序字典 在Python中,字典类型里面的元素默认是无序的,但是我们也可以通过collections模块创建有序字典 # -*- coding:utf-8 -*- # python有序字典需导入模块c ...
- vue-电脑端导出-txt
// fakeClick(obj) { // var ev = document.createEvent("MouseEvents"); // ev.initMouseEvent( ...
- 动态添加class的一种方法
外面可以写一层class再用:class 绑定新的clss进去 而且可以用三目运算.爽歪歪
- Boolean数据类型
boolean 数据类型 boolean 变量存储为 8位(1 个字节)的数值形式,但只能是 True 或是 False,可以把它看做是一个和1代替,并且一定要小写.boolean operate是指 ...
- [xdoj] 1301&1302 数字计数 数字计数的复仇
1.首先需要掌握二进制数的一种特性,00=0,01=1,10=2,11=3.每一个二进制的值代表他前面的二进数的个数,比如11=3,他的前面就有三个二进制的数字,不过在本题中,题目数据是1-n,故把0 ...
- Unity 2D入门基础教程之僵尸先生
注:这是根据网上教程完成的. 翻译:http://blog.1vr.cn/?p=1422 原文:http://www.raywenderlich.com/61532/unity-2d-tutorial ...
- 路径遍历:ZIP条目覆盖
程序在解压zip文件时,如果没有验证zip条目,攻击者可能对条目覆盖,从而造成路径遍历 例如:以下代码示例解压zip文件. static final int BUFFER = 512; / ...
- selenium获取文本
# 标题list_title = driver.find_elements_by_xpath('//*[@id="share-content"]/div/div[1]/ul/li/ ...
- DOS命令下映射虚拟磁盘(驱动器)
---恢复内容开始--- subst命令,用于路径替换,进入dos窗口,键入“subst /?”(t和/之间有空格),会看到关于subst的用法如下: C:\Users\Administrator&g ...