- 浏览: 255408 次
- 性别:
- 来自: 南京
-
文章分类
最新评论
-
mabusyao:
漠北空城 写道请问下,你这个是JDK版本是多少呢?!忘记了,应 ...
HashMap 源码解读 -
漠北空城:
请问下,你这个是JDK版本是多少呢?!
HashMap 源码解读 -
schumee:
完美团队~
项目沉思录 - 1.1 -
winie:
整理下 搞成引擎嘛 国产需要这样的engine
简单工作流引擎 -
mabusyao:
某位同学给我提供的堪称完美的解决方案:1. 将三个int数组放 ...
CraneWork
/*Problem Statement
There are three stacks of crates - two of them outside of the warehouse, and one inside the warehouse.
We have a crane that can move one crate at a time, and we would like to move all of the crates to the stack inside the warehouse.
A heavier crate can never be stacked on top of a lighter crate, and all three initial stacks obey that rule.
Create a class CraneWork that contains a method moves that is given int[]s stack1, stack2, and warehouse containing the initial three stacks,
and returns the minimum number of moves required to move all the crates into the warehouse stack.
The elements of stack1, stack2, and warehouse represent the weights of the crates and are given from top to bottom
(and thus in non-decreasing order of weight).
Definition
Class: CraneWork
Method: moves
Parameters: int[], int[], int[]
Returns: int
Method signature: int moves(int[] stack1, int[] stack2, int[] warehouse)
(be sure your method is public)
Constraints
stack1, stack2, and warehouse will each contain between 0 and 20 elements, inclusive.
The total number of elements in the three stacks will be between 1 and 20, inclusive.
Each element in the three stacks will be between 1 and 200, inclusive.
stack1, stack2, and warehouse will each be in non-decreasing order.
Examples
0){3,50} {} {1,2,50,50,50}
Returns: 12
Move 3 to stack2, 1 to stack1,
2 to stack2, 1 to stack2,
50 to warehouse, 1 to warehouse,
2 to stack1, 1 to stack1,
3 to warehouse, 1 to stack2,
2 to warehouse, 1 to warehouse.
1){50} {50} {10,20,30}
Returns: 17
Start by moving 50 from stack2 to stack1.
It then takes 7 moves to transfer the 10,20,30
to stack 2, 2 moves to transfer the 2 50's to the warehouse,
and 7 more to transfer the 10,20,30 to the warehouse.
2){} {} {2,5,6,7}
Returns: 0
All the crates are already in the warehouse.
3){1,2,3} {} {}
Returns: 7
Move 1 from stack1 to warehouse, 2 from stack1 to stack2,
1 from warehouse to stack2, 3 from stack1 to warehouse,
1 from stack2 to stack1, 2 from stack2 to warehouse, 1 from stack1 to warehouse.
This problem statement is the exclusive and proprietary property of TopCoder, Inc.
Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc.
is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. */
package cranework_2;
public class CraneWork {
public static void moves(int[] stack_1, int[] stack_2, int[] ware_house) throws Exception{
CrateStack stack1 = new CrateStack(stack_1);
CrateStack stack2 = new CrateStack(stack_2);
CrateStack warehouse = new CrateStack(ware_house);
show(stack1, stack2, warehouse);
moves(stack1, 0, stack2, 0, warehouse, 0);
}
/**
* @param stack1
* @param cursor1
* @param stack2
* @param cursor2
* @param stack3
* @param cursor3
* @throws Exception
*/
public static void moves(CrateStack stack1, int cursor1, CrateStack stack2, int cursor2, CrateStack stack3, int cursor3) throws Exception{
if(cursor1 == stack1.root && cursor2 == stack2.root) return;
if(cursor1 == stack1.root) {
if(cursor3 == stack3.root) {
moves(stack2, cursor2 + 1, stack3, stack3.root, stack1, stack1.root);
stack3.pushCrate(stack2.popCrate());
show(stack1, stack2, stack3);
moves(stack1, cursor1, stack2, stack2.root, stack3, stack3.root);
} else if (stack2.getValue(cursor2) > stack3.getValue(cursor3)) {
moves(stack2, cursor2 + 1, stack3, cursor3, stack1, stack1.root);
stack3.pushCrate(stack2.popCrate());
show(stack1, stack2, stack3);
moves(stack1, cursor1, stack2, stack2.root, stack3, stack3.root);
}else {
cursor3 = stack3.find(stack2.getValue(cursor2));
moves(stack2, cursor2 + 1, stack3, cursor3, stack1, stack1.root);
stack3.pushCrate(stack2.popCrate());
show(stack1, stack2, stack3);
moves(stack1, cursor1, stack2, stack2.root, stack3, stack3.root);
}
} else if(cursor2 == stack2.root) {
if(cursor3 == stack3.root) {
moves(stack1, cursor1 + 1, stack3, stack3.root, stack2, stack2.root);
stack3.pushCrate(stack1.popCrate());
show(stack1, stack2, stack3);
moves(stack2, cursor2, stack1, stack1.root, stack3, stack3.root);
} else if (stack1.getValue(cursor1) > stack3.getValue(cursor3)) {
moves(stack1, cursor1 + 1, stack3, cursor3, stack2, stack2.root);
stack3.pushCrate(stack1.popCrate());
show(stack1, stack2, stack3);
moves(stack2, cursor2, stack1, stack1.root, stack3, stack3.root);
}else {
cursor3 = stack3.find(stack1.getValue(cursor1));
moves(stack1, cursor1 + 1, stack3, cursor3, stack2, stack2.root);
stack3.pushCrate(stack1.popCrate());
show(stack1, stack2, stack3);
moves(stack2, cursor2, stack1, stack1.root, stack3, stack3.root);
}
} else if(cursor3 == stack3.root) {
if (stack1.getValue(cursor1) > stack2.getValue(cursor2)) {
moves(stack1, cursor1 + 1, stack3, stack3.root, stack2, cursor2);
stack3.pushCrate(stack1.popCrate());
show(stack1, stack2, stack3);
moves(stack2, cursor2, stack1, stack1.root, stack3, stack3.root);
} else {
moves(stack2, cursor2 + 1, stack3, stack3.root, stack1, cursor1);
stack3.pushCrate(stack2.popCrate());
show(stack1, stack2, stack3);
moves(stack1, cursor1, stack2, stack2.root, stack3, stack3.root);
}
} else if (stack3.getValue(cursor3) > stack2.getValue(cursor2) && stack3.getValue(cursor3) > stack1.getValue(cursor1)) {
if (stack1.getValue(cursor1) > stack2.getValue(cursor2)) {
cursor3 = stack3.find(stack1.getValue(cursor1));
moves(stack1, cursor1 + 1, stack3, cursor3, stack2, cursor2);
stack3.pushCrate(stack1.popCrate());
show(stack1, stack2, stack3);
moves(stack2, cursor2, stack1, stack1.root, stack3, stack3.root);
} else {
cursor3 = stack3.find(stack2.getValue(cursor2));
moves(stack2, cursor2 + 1, stack3, cursor3, stack1, cursor1);
stack3.pushCrate(stack2.popCrate());
show(stack1, stack2, stack3);
moves(stack1, cursor1, stack2, stack2.root, stack3, stack3.root);
}
} else {
if (stack1.getValue(cursor1) > stack2.getValue(cursor2)) {
moves(stack1, cursor1 + 1, stack3, cursor3, stack2, cursor2);
stack3.pushCrate(stack1.popCrate());
show(stack1, stack2, stack3);
moves(stack2, cursor2, stack1, stack1.root, stack3, stack3.root);
}
else {
moves(stack2, cursor2 + 1, stack3, cursor3, stack1, cursor1);
stack3.pushCrate(stack2.popCrate());
show(stack1, stack2, stack3);
moves(stack1, cursor1, stack2, stack2.root, stack3, stack3.root);
}
}
}
public static void show(CrateStack stack1, CrateStack stack2, CrateStack stack3){
System.out.println("Round:");
stack1.showStack();
stack2.showStack();
stack3.showStack();
//System.out.print("\n " + cursor1.weight + " " + cursor2.weight + " " + cursor3.weight);
System.out.println("\n");
}
}
class CrateStack {
private int[] stack = new int[20];
public int root = 0;
public CrateStack(int weight) {
stack[0] = weight;
root++;
}
public CrateStack(int[] array) {
for(int i = 0; i < array.length; i++) {
stack[i] = array[i];
}
root = array.length;
}
//Push a Value into Stack
public void pushCrate(int value) {
stack[root] = value;
root++;
}
//Pop a Value into Stack
public int popCrate() throws Exception{
if(root != 0) {
root--;
int result = stack[root];
stack[root] = 0;
return result;
}
throw new NullPointerException();
}
public boolean isTop(int cursor) {
if(cursor == root - 1) return true;
return false;
}
public void showStack() {
try {
if (root == 0) {
System.out.print("{}");
} else {
int temp = root - 1;
System.out.print("{");
while (temp >= 0) {
System.out.print(stack[temp] + ",");
temp--;
}
System.out.print("}");
}
} catch (Exception e) {
//
}
}
public int find (int temp) {
int cursor = 0;
if (temp != 0) {
while(cursor != root) {
if (stack[cursor] < temp) return cursor;
cursor++;
}
}
return root;
}
public int getValue(int index) {
return stack[index];
}
}
遗憾的是如果有重量相同的情况还没有解决。
评论
1. 将三个int数组放在一起排序,生成这样一个char数组[A,B,C,C,C,A,B,B,A].
2. 简单的move方法
private static void move(char[] stack, int index, char to) { if (index == 0) { if (stack[index] == to) { // Do nothing. } else { System.out.println("move stack from " + stack[0] + " to " + to); stack[0] = to; } return; } else { if (stack[index] == to) { move(stack, index - 1, to); } else { move(stack, index - 1, getMidum(stack[index], to)); System.out.println("move stack from " + stack[index] + " to " + to); stack[index] = to; move(stack, index - 1, to); } } }
经测试可行,且为最优解
发表评论
-
大数据下的实体解析
2016-07-07 12:03 671大数据时代的实体解析困境 <!--[if !sup ... -
中文相似度匹配算法
2015-12-30 14:44 1921基于音形码的中文字 ... -
各种语言写的wordcount
2015-09-24 16:07 0Java版本: String input ... -
数组双指针算法的研究
2015-07-14 16:59 2472双指针算法在数组/链 ... -
摩尔投票法
2015-06-30 20:13 18440摩尔投票法 提问: 给定一个int型数组,找出该数 ... -
(转)最长回文字串算法
2015-01-18 14:30 1443来自(http://blog.163.com/zhaohai ... -
STRUTS2 源码 - Logging System
2012-05-24 08:51 1415看了STRUTS2的源码,了解了它的logging系统,觉得还 ... -
在线词典的数据结构实现。
2012-05-18 08:37 0昨天在网上看到了一道百度的面试题: Baidu写道 ... -
存储中间计算结果的动态规划算法
2012-04-18 15:50 1225public class RodCutting { ... -
java写的四则运算器
2011-08-19 22:19 2753本打算做一个从RE到NFA的转换器,思路已经理清了,但是在动手 ... -
什么是最小二乘拟合
2007-09-14 21:27 992对于一个数据点(x1, y1), ... (xn, yn)的集 ... -
palindrome
2008-07-14 17:39 999package palindrome;/*Problem St ... -
SalesRouting
2008-07-15 10:53 817package salesRouting;/*Problem ... -
FactorialSystem
2008-07-21 19:36 863/*Problem StatementIn the facto ... -
RecurringNumbers
2008-07-22 09:41 942/*Problem StatementA rational n ... -
HardDuplicateRemover
2008-07-23 15:17 957/*Problem StatementWe have a se ... -
BlockStructure
2008-07-23 20:55 813/*Problem StatementA group of v ... -
SkipStones
2008-07-28 17:31 833/*Problem StatementWhen a stone ... -
TheaterVisit
2008-07-28 17:31 792/*Problem StatementYou want to ... -
SquareDigits
2010-07-06 12:36 1043Problem Statement ***Note: ...
相关推荐
codemirror版本:https://codemirror.net/5/doc/releases.html
外国电影演员识别系统源码分享
mf3010 打印扫描一体机驱动管理软件。
2024免费毕业设计成品,包括源码+数据库+往届论文资料 启动教程:https://www.bilibili.com/video/BV11ktveuE2d 讲解视频:https://www.bilibili.com/video/BV1YfkHYwEME 二次开发教程:https://www.bilibili.com/video/BV1Cw2rY1ErC
chrome-headless-shell-linux64-135.0.7004.0 (Canary).zip
DeepSeek大模型介绍与展望.pptx
英特尔的公版原理图和PCB,cadence版本
《单容水箱液位精准调控:模糊控制策略的深度研究与复现》,单容水箱液位随动系统的模糊控制研究 模糊控制lunwen复现 期刊:化工与自动化仪表(2021年) 图1为结构图,图9为原文结构图, 版本不一样,器件略有调整 图7为结果图,图8为原文结果图 ,单容水箱液位;模糊控制;研究;论文复现;期刊;化工与自动化仪表;结构图;结果图;版本差异;器件调整,"模糊控制研究在单容水箱液位随动系统中的应用与复现"
一个windows上使用的搜索小工具
内容: 这份数据集包含了来自国际大洋发现计划(IODP)第342航次站点U1405、U1406、U1407、U1409和U1410的浮游有孔虫碳酸盐团簇同位素、稳定氧和碳同位素,以及沉积物中的GDGT(甘油二烷基甘油四醚)和烯酮数据。这些站点位于北大西洋的新foundland脊(U1407、U1409和U1410)和J-异常脊(U1405和U1406),用于创建覆盖整个新生代的几乎连续但低分辨率(约每92万年一个样本)的数据拼接,并重建了碳酸盐团簇同位素、TEX86和UK'37海表温度。每个样本包含20立方厘米的沉积物,覆盖2厘米的核心深度区间。年龄模型主要基于详细的船上生物-磁性地层学研究(Norris等,2014)。然而,在40.8 Ma至44.8 Ma时间段内,使用了Cappelli等人(2019)更新的U1410站点年龄模型,通过与U1408站点的年龄模型对比来确定。 访问此数据集,请点击这里:"" ()。
厨房用品分割系统源码&数据集分享
.
监控鞋类物品检测系统源码分享
2024免费毕业设计成品,包括源码+数据库+往届论文资料 启动教程:https://www.bilibili.com/video/BV11ktveuE2d 讲解视频:https://www.bilibili.com/video/BV1YfkHYwEME 二次开发教程:https://www.bilibili.com/video/BV1Cw2rY1ErC
曲线图异常波形检测系统源码分享
内容概要:本文介绍了动车组车号自动识别的现状及其存在的问题,提出了基于图像识别技术的新方法。文中详述了传统人工识别与RFID识别方法的不足,重点阐述了一种新的图像识别系统的设计与实施方案,该系统能够实现在多种恶劣环境下高效精确地获取动车组车号,并通过实际案例展示了这套系统的优势以及其在铁路行业的广阔应用前景。 适用人群:从事铁路运输管理、轨道交通系统开发维护的技术人员,尤其是负责动车组调度、监控及维修工作的相关人员。 使用场景及目标:①用于替代现有人工记录与RFID标签方式,提升动车组车号识别精度与效率;②适用于各种天气状况下的户外作业场景;③旨在构建更加智能化、信息化程度更高的铁路运输体系,助力智慧动车段建设。 其他说明:文中还包括具体的实验对比和技术细节分析,如不同的开机触发装置选择、图像采集设备参数设置、补光措施及识别算法的设计,强调了实际应用场景中可能遇到的问题以及相应的解决方案。
基于AnythingLLM框架和Ollama环境本地运行deepseek,并可以通过用户自己的文档来针对性地回答用户问题,用户也可以上传文件来构建模型回复问题所需要的所有参考资料的知识库,使得模型相对于在线模型更加专业地解答用户的问题。同时本地部署保证了隐私性和针对性。
指针式表盘指针关键部位分割系统源码&数据集分享
多策略增强:MWOA鲸鱼优化算法与其他变体及2024年最新算法的实证比较与结果分析——新颖策略实施效果显著且复杂度无增加的研究,多策略改进的鲸鱼优化算法(MWOA),与其他三种变体和几种2024最新算法比较,策略都是很新颖的策略,可以直接写了发文章,并且没有增加复杂度上改进效果 ,MWOA; 变体算法; 最新算法; 策略新颖; 复杂度未增加; 改进效果显著,"多策略改进MWOA算法:与多种变体及2024新算法比较展示优越性"
织物缺陷检测系统源码分享[一条龙教学YOLOV8标注好的数据集一键训练_70+全套改进创新点发刊_Web前端展示]