Towers of Hanoi
Your mission is to move the stack from the left peg to the right peg. The rules are well known:
Only one disk may be moved at a time. Each move consists of taking the upper disk from one of the pegs and sliding it onto another rod, on top of the other disks that may already be present on that rod. No disk may be placed on top of a smaller disk.
And finally to prove that you are a genius and a master of mouse handling, you have a timelimit of 60 seconds.
这是一个汉诺塔游。要求60秒内完成。(以我单身xx年的手速都无法完成...)
下载这个 java applet, 反编译。
每移动一个会向服务器发送一个请求 (像:?query=ab; ?query=cb),返回对应的编码,将返回的数据append到 StringBuffer变量solution中。最后solution进入SHA512后,提交?solution= SHA512(solution)
query 对应的返回值是:
ab we
ac ch
ba lr
bc al
ca ul
cb z!
ab表示 将 a柱中最上面的柱移动到b柱的最上面。其它同理。
然后就是求解汉诺塔了,是一个递归的过程。
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map; public class Main {
static Map<String, String> query;
static StringBuffer solution; /**
* 汉诺递归
*
* @param a
* --源柱子
* @param b
* --中间临时可存放的柱子
* @param c
* --目标术子,要搬运到的柱子
* @param n
* --源柱子有n个柱子
*/
private static void foo(char a, char b, char c, int n) {
if (n == 1) {
solution.append(query.get("" + a + c));
return;
}
foo(a, c, b, n - 1);
solution.append(query.get("" + a + c));
foo(b, a, c, n - 1);
} public static String SHA512(String s) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-512");
byte[] pt = s.getBytes();
byte[] ct = digest.digest(pt);
return bytesToHex(ct);
} catch (Exception e) {
}
return null;
} public static String bytesToHex(byte[] data) {
if (data == null) {
return null;
}
int len = data.length;
StringBuffer hexString = new StringBuffer(len * 2);
for (int i = 0; i < len; i++) {
if ((data[i] & 0xFF) < 16) {
hexString.append(0);
}
hexString.append(Integer.toHexString(data[i] & 0xFF));
}
return hexString.toString();
} public static void main(String[] args) {
int n = 10;
query = new HashMap<String, String>();
query.put("ab", "we");
query.put("ac", "ch");
query.put("ba", "lr");
query.put("bc", "al");
query.put("ca", "ul");
query.put("cb", "z!");
solution = new StringBuffer();
foo('a', 'b', 'c', n);
System.out.println(SHA512(solution.toString()));
}
}
Towers of Hanoi的更多相关文章
- The Towers of Hanoi Revisited---(多柱汉诺塔)
Description You all must know the puzzle named "The Towers of Hanoi". The puzzle has three ...
- [CareerCup] 3.4 Towers of Hanoi 汉诺塔
3.4 In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sizes ...
- POJ 1958 Strange Towers of Hanoi 解题报告
Strange Towers of Hanoi 大体意思是要求\(n\)盘4的的hanoi tower问题. 总所周知,\(n\)盘3塔有递推公式\(d[i]=dp[i-1]*2+1\) 令\(f[i ...
- POJ 1958 Strange Towers of Hanoi
Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3784 Accepted: 23 ...
- POJ-1958 Strange Towers of Hanoi(线性动规)
Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2677 Accepted: 17 ...
- ural 2029 Towers of Hanoi Strike Back (数学找规律)
ural 2029 Towers of Hanoi Strike Back 链接:http://acm.timus.ru/problem.aspx?space=1&num=2029 题意:汉诺 ...
- POJ1958 Strange Towers of Hanoi [递推]
题目传送门 Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3117 Ac ...
- poj 1920 Towers of Hanoi
Towers of Hanoi Time Limit: 3000MS Memory Limit: 16000K Total Submissions: 2213 Accepted: 986 Ca ...
- zoj 2338 The Towers of Hanoi Revisited
The Towers of Hanoi Revisited Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge You all mus ...
- poj1958——Strange Towers of Hanoi
The teacher points to the blackboard (Fig. 4) and says: "So here is the problem: There are thre ...
随机推荐
- C++ Primer : 第十三章 : 拷贝控制之对象移动
右值引用 所谓的右值引用就是必须将引用绑定到右值的引用,我们通过&&来绑定到右值而不是&, 右值引用只能绑定到即将销毁的对象.右值引用也是引用,因此右值引用也只不过是对象的别名 ...
- python ftplib.FTP 获取当前路径下所有目录
FTP 模块里有一个dir函数,可以打印出当前路径下所有文件,但是这个函数没有返回值,只是打印出来. 还有一个nlst函数,可以返回一个文件名的列表,但是只有文件名,没有详细信息,无法判断是否是目录. ...
- Mysql外键约束设置使用方法
如果表A的主关键字是表B中的字段,则该字段称为表B的外键,表A称为主表,表B称为从表.外键是用来实现参照完整性的,不同的外键约束方式将可以使两张表紧密的结合起来,特别是修改或者删除的级联操作将使得日常 ...
- 【递归】斐波那契数列第n个数
递归.递推计算斐波那契数列第n项的值: #include <stdio.h> long long fact(int n); //[递推]计算波那契数列第n个数 long long fact ...
- express创建项目
sudo apt-get install node-express-generator dave@voctrals:~/WebstormProjects/nodejs-study/express$ e ...
- PHP远程下载图片损坏问题
代码如下: <?php header("Content-type=html/text;charset=utf-8"); function download($file_nam ...
- FM/PCM与FM/PPM的区别
FM/PCM的优点: 1 高可靠性和高抗干扰性.大家知道,一般PPM遥控设备都要求在操作时先开发射机后开接收机,先关接收机后关发射机.其原因是在没有发射信号时,接受机会因自身内部的噪音或外界的 ...
- Hibernate5.2之原生SQL查询
Hibernate5.2之原生SQL查询 一. 介绍 在上一篇博客中笔者通过代码的形式给各位读者介绍了Hibernate中最重要的检索方式--HQL查询.在本博文中笔者将向各位读者介绍Hiberna ...
- Django base view
class django.views.generic.base.View 它是基类的基类,其它View基类都是从这继承的. 官例: from django.http import HttpRespon ...
- JSTL+MyEclipse8.5+Tomcat配置
使用JSTL的时候需要配置,即导入stdandard.jar和jstl.jar包,并在web.xml下对jstl进行配置.下面是使用步骤: 1. 解压jakarta-taglibs-standard- ...