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 ...
随机推荐
- 007-docker-安装-mysql:5.6
1.搜索镜像 docker search mysql 2.拉取合适镜像 docker pull mysql:5.6 docker images 3.使用镜像 docker run -p 3306:33 ...
- HP1020打印机“传递给系统调用的数据区域太小” 如何处理?
如果电脑上曾经安装过 HP LaserJet 激光打印机的驱动程序,重新安装驱动程序之前,需要完全卸载以前安装的驱动程序,否则可能会出现无法找到设备或者安装不上驱动程序的现象. 安装网站下载的即插即用 ...
- Oracle(1)之虚拟机下安装与简单使用
Oracle介绍与安装 简介 Oracle 数据库系统是美国 ORACLE 公司(甲骨文)提供的以分布式数据库为核心的一组软件产品,是目前最流行的客户/服务器 (CLIENT/SERVER) 或 B/ ...
- [redis]redis常用
https://redis.io/topics/quickstart $ redis-cli ping PONG redis-server is the Redis Server itself. re ...
- GRU门控制循环单元【转载】
转自:https://www.infoq.cn/article/sliced-recurrent-neural-networks 1.门控循环单元 GRU GRU 由 reset gate r 和 u ...
- 【LeetCode每天一题】Reverse String
Write a function that reverses a string. The input string is given as an array of characters char[]. ...
- vue proxyTable 跨域问题。
- MJExtension代码解释
Runtime 是什么? objective-C会把函数调用的转换为消息发送,objc_MsgSend(receiver, msg), 注意,recevier指的是消息的接受者.那么self, sup ...
- iOS 测试企业应用的分发
开发者能够方便地来做iOS应用的测试分发,目前可以选用“浦公英”和“Fir.im”来做. 生成IPA文件 生成应用的 IPA 文件可以使用命令行 xcodebuild exportArchive -e ...
- xcode如何支持8.0以下
1. shell打开 open /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSuppor ...