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 ...
随机推荐
- 【微软2014实习生及秋令营技术类职位在线測试】题目2 : K-th string
时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description Consider a string set that each of them consists of ...
- 006-Object.assign
一.Object.assign简要使用 是ES6新添加的接口,主要的用途是用来合并多个JavaScript的对象. Object.assign()接口可以接收多个参数,第一个参数是目标对象,后面的都是 ...
- Win7各版本功能对比
- 15个Node.js项目列表
前言: Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台,是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎,V8引擎执行Javascri ...
- [LeetCode] 162. Find Peak Element_Medium tag: Binary Search
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- js分页器插件
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- IDEA2017及DataGrip2017注册码
访问http://idea.lanyus.com/,网页中有相关说明,最简单的方式是将“0.0.0.0 account.jetbrains.com”添加到hosts文件中,然后点击页面底部的“获得注册 ...
- Adobe Flash Builder 调试器无法连接,无法进行调试,构建停止在57%
参考:https://blog.csdn.net/wuboyaogun1/article/details/9105373 谷歌浏览器下载Flash debugger :Download the Win ...
- shell基础:用户自定义变量
- webpack使用七
产品阶段的构建 目前为止,我们已经使用webpack构建了一个完整的开发环境.但是在产品阶段,可能还需要对打包的文件进行额外的处理,比如说优化,压缩,缓存以及分离CSS和JS. 对于复杂的项目来说,需 ...