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];
}

}


遗憾的是如果有重量相同的情况还没有解决。

资源下载链接为: https://pan.quark.cn/s/5c50e6120579 在Android移动应用开发中,定位功能扮演着极为关键的角色,尤其是在提供导航、本地搜索等服务时,它能够帮助应用获取用户的位置信息。以“baiduGPS.rar”为例,这是一个基于百度地图API实现定位功能的示例项目,旨在展示如何在Android应用中集成百度地图的GPS定位服务。以下是对该技术的详细阐述。 百度地图API简介 百度地图API是由百度提供的一系列开放接口,开发者可以利用这些接口将百度地图的功能集成到自己的应用中,涵盖地图展示、定位、路径规划等多个方面。借助它,开发者能够开发出满足不同业务需求的定制化地图应用。 Android定位方式 Android系统支持多种定位方式,包括GPS(全球定位系统)和网络定位(通过Wi-Fi及移动网络)。开发者可以根据应用的具体需求选择合适的定位方法。在本示例中,主要采用GPS实现高精度定位。 权限声明 在Android应用中使用定位功能前,必须在Manifest.xml文件中声明相关权限。例如,添加<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />,以获取用户的精确位置信息。 百度地图SDK初始化 集成百度地图API时,需要在应用启动时初始化地图SDK。通常在Application类或Activity的onCreate()方法中调用BMapManager.init(),并设置回调监听器以处理初始化结果。 MapView的创建 在布局文件中添加MapView组件,它是地图显示的基础。通过设置其属性(如mapType、zoomLevel等),可以控制地图的显示效果。 定位服务的管理 使用百度地图API的LocationClient类来管理定位服务
资源下载链接为: https://pan.quark.cn/s/0c983733fad2 自助打印系统源码是一个包含完整打印服务软件开发代码的压缩包,旨在为用户提供便捷的打印方式。用户无需借助电脑等设备,只需在支持该系统的终端操作即可完成打印。深入分析源码有助于我们理解系统架构和功能实现,对学习和定制打印系统极具价值。 系统架构:该系统由前端用户界面、后端服务器处理逻辑和数据库三部分构成。前端负责用户交互,如文件选择、支付方式选择等;后端处理用户请求,包括文件上传、打印任务调度、费用计算等;数据库则用于存储用户信息、打印记录和配置数据。 文件管理:源码包含文件上传、存储和检索模块。用户可选择本地文件或云存储中的文件进行打印,系统需支持多种文件格式,并具备安全的文件存储机制以保护用户数据安全。 支付集成:系统需与第三方支付平台(如支付宝、微信支付)集成,实现在线支付功能,涉及API调用、交易状态跟踪和错误处理。 打印作业管理:系统需管理打印作业,包括作业提交、队列排序、打印状态更新、错误处理等,并可能提供作业预览功能,让用户在打印前查看效果。 硬件接口:为与打印机通信,源码包含特定的硬件驱动程序或API接口,需适应不同品牌和型号的打印机,确保打印质量和效率。 用户认证与权限控制:系统需识别并验证用户身份,可能采用账号密码、二维码扫描等方式。不同用户可能有不同的打印权限,如打印份数限制、彩色打印权限等。 日志与统计:源码包含日志记录功能,用于监控系统运行状况和故障排查。统计功能可帮助管理者了解打印业务的使用情况,如打印量、费用收入等。 安全性:源码安全性至关重要,需防止未授权访问和数据泄露。开发者可能采用加密技术保护用户数据,并定期进行安全更新和漏洞修复。 扩展性与可维护性:优秀的源码设计应具备良好的结构和模块化,便于未来功能扩展和问题修复,例如轻松添加新功能,支持更多文件格式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值