- 浏览: 79458 次
文章分类
最新评论
-
wodentt:
....为什么楼主都英文的博客
用java解决百度之星移动火柴的问题 part 1 -
wensonzhou86:
思路明确,代码清晰,通俗易懂
visit 淘宝面试题:如何充分利用多核CPU,计算很大的List中所有整数的和 -
carydeepbreathing:
代码比较干净
visit 淘宝面试题:如何充分利用多核CPU,计算很大的List中所有整数的和 -
meiowei:
yangguo 写道i don't think it is a ...
用java解决百度之星移动火柴的问题 part 2 -
yangguo:
i don't think it is a good way ...
用java解决百度之星移动火柴的问题 part 2
The next step is to find the actually path of floors that we are going through to find the breaking floor. Here is the code to do that.
The last method is to generate series (A), (B), ... dynamically. The main code is a recursion.
Here we use two other classes, Building and Trial. Building is to define the number of floors and the broken floor, which will be used for testing purpose. The Trial class is to record each trial, plus a flag to indicate whether this is a logic conclusion or not, as we discussed in the previous post listed in (i) and (ii).
and
The test cases are a little complex, which is in the next post.
java 代码
- package glassball;
- import java.util.List;
- import java.util.ArrayList;
- /**
- * For a given building and balls, find out the path of floors we go through
- * on the way to find the minimal floor where the ball is broken if thrown.
- */
- public class PathFinder
- {
- private Building building;
- private GlassBallPuzzle puzzle;
- public PathFinder(Building building, int numOfGlassballs)
- {
- this.building = building;
- int totalFloors = building.getNumOfFloors();
- this.puzzle = new GlassBallPuzzle(numOfGlassballs, totalFloors);
- }
- public GlassBallPuzzle getPuzzle() { return puzzle; }
- public List findPuzzlePath()
- {
- int totalFloors = building.getNumOfFloors();
- int[][] sums = puzzle.getRecursiveSums();
- // we don't need extra balls. If we don't want this optimization
- // we have to check whether a floor is higher than building's height,
- // and ignore them if it's true.
- int ballIndex = puzzle.getOptimalTrialsAndBalls()[1] - 1;
- List path = new ArrayList();
- int baseFloor = 1;
- generatePathForBallWithIndex(ballIndex, path, baseFloor, totalFloors, sums);
- return path;
- }
- // This is a recursive call
- private void generatePathForBallWithIndex(int ballIndex, List path,
- int baseFloor, int totalFloors, int[][] sums)
- {
- int[] floorArray = generateFloorArray(sums, ballIndex, totalFloors);
- for (int i=0; i<floorArray.length; i++)
- {
- // -1 here because baseFloor starts with 1.
- int currentFloor = baseFloor + Math.min(totalFloors, floorArray[i] - 1);
- boolean result;
- if (ballIndex == 0 && currentFloor == totalFloors)
- {
- result = true; // only one ball case
- }
- else if (currentFloor < baseFloor + totalFloors)
- {
- result = building.isBrokenIfThrowAt(currentFloor);
- path.add(new Trial(currentFloor, ballIndex, result, false));
- }
- else
- {
- result = true; // we have been here, no point to do it again.
- }
- if (result)
- {
- if (i > 0) baseFloor += floorArray[i-1];
- int floorsBetween = currentFloor - baseFloor;
- if (ballIndex - 1 <= 0) // 0 or 1
- {
- // try from bottom, one floor at a time
- for (int k=baseFloor; k<baseFloor+floorsBetween; k++)
- {
- if (k <= 0) continue; // this happens when floor 1 is broken.
- boolean stepResult = building.isBrokenIfThrowAt(k);
- path.add(new Trial(k, ballIndex - 1, stepResult, false));
- if (stepResult) return;
- }
- // this indicate the last broken one is the floor
- // if there is no broken one, then we need to add the last floor
- boolean hasBroken = false;
- boolean allBroken = true;
- for (int j=0; j<path.size(); j++)
- {
- Trial trial = (Trial)path.get(j);
- if (trial.isBroken()) hasBroken = true;
- else allBroken = false;
- }
- if (!hasBroken) path.add(new Trial(currentFloor, ballIndex, true, true));
- if (allBroken) path.add(new Trial(currentFloor, ballIndex, true, true));
- return;
- }
- else
- {
- // now try on ballIndex - 1 array.
- if (floorsBetween > 0)
- {
- generatePathForBallWithIndex(ballIndex - 1, path,
- baseFloor, floorsBetween, sums);
- return;
- }
- }
- }
- // continue
- }
- }
- // This method is similar to the one in GlassBallPuzzle, but is dynamic
- // since the underlying sum array varies - depends on totalFloors variable.
- private int[] generateFloorArray(int[][] sums, int ballIndex, int totalFloors)
- {
- if (ballIndex == 0)
- {
- return sums[0];
- }
- // find the end element not to exceed totalFloors
- int[] sumArray = sums[ballIndex];
- int index = 0;
- for (int j=0; j<sumArray.length; j++)
- {
- // The equal sign is corresponding to the last trial
- // when the ball is broken.
- if (sumArray[j] >= totalFloors)
- {
- index = j;
- break;
- }
- }
- // now start from the end element, sum up going to the beginning;
- sumArray = sums[ballIndex - 1];
- int[] floors = new int[index + 1];
- int total=0;
- for (int j=0; j<=index; j++)
- {
- total += sumArray[index - j];
- floors[j] = total;
- }
- return floors;
- }
- }
The last method is to generate series (A), (B), ... dynamically. The main code is a recursion.
Here we use two other classes, Building and Trial. Building is to define the number of floors and the broken floor, which will be used for testing purpose. The Trial class is to record each trial, plus a flag to indicate whether this is a logic conclusion or not, as we discussed in the previous post listed in (i) and (ii).
java 代码
- package glassball;
- /**
- * To hold the state of each trial
- */
- public class Trial
- {
- private int floor;
- private int ballNum;
- private boolean broken;
- private boolean isDeducted = false;
- public Trial(int floor, int ballNum, boolean broken, boolean isDeducted)
- {
- this.floor = floor;
- this.ballNum = ballNum;
- this.broken = broken;
- this.isDeducted = isDeducted;
- }
- public int getFloor() { return floor; }
- public int getBallNum() { return ballNum; }
- public boolean isBroken() { return broken; }
- public boolean isDeducted() { return isDeducted; }
- public String toString()
- {
- return "(f=" + floor + ", b=" + ballNum + ", " + (broken ? "broken" : "whole")
- + ", " + (isDeducted ? "deducted" : "tried") + ")"; }
- public boolean equals(Object obj)
- {
- if ( !(obj instanceof Trial) ) return false;
- Trial trial = (Trial)obj;
- return this.floor == trial.floor && this.ballNum == trial.ballNum &&
- this.broken == trial.broken && this.isDeducted == trial.isDeducted;
- }
- }
and
java 代码
- package glassball;
- /**
- * Building with floors.
- */
- public class Building
- {
- private int numOfFloors;
- // the lowest floor where the ball is broken if thrown.
- // This field is sealed inside this class.
- private int ballbreakingfloor;
- public Building(int numOfFloors, int ballbreakingfloor)
- {
- this.numOfFloors = numOfFloors;
- this.ballbreakingfloor = ballbreakingfloor;
- }
- public int getNumOfFloors() { return numOfFloors; }
- public boolean isBrokenIfThrowAt(int floor) { return floor >= ballbreakingfloor; }
- }
The test cases are a little complex, which is in the next post.
发表评论
-
Revisit 一道应聘智力题的编程求解, Einstein puzzle
2010-03-09 00:36 1426Another brain teaser, titled 一道 ... -
Another Google question - drop glassballs from a building 5
2007-05-13 04:01 1077Here are some results are the t ... -
Another Google question - drop glassballs from a building 4
2007-05-13 03:42 1537The test cases are divided into ... -
Another Google question - drop glassballs from a building 2
2007-05-13 03:02 1109Here is the code following the ... -
Another Google question - drop glassballs from a building 1
2007-05-12 04:43 1203Here is the question: 原题: ... -
Given n coins and a pan balance,find the only counterfeit 4
2007-02-27 05:54 1116The Utils class is composed of ... -
Given n coins and a pan balance,find the only counterfeit 3
2007-02-27 05:50 1502In the 1-ball case, if the stat ... -
Given n coins and a pan balance,find the only counterfeit 2
2007-02-27 05:29 1107Now it comes to the Scale class ... -
Given n coins and a pan balance, find the only counterfeit 1
2007-02-27 04:49 1336The problem is: There is one ... -
A Google's Interview Question - GLAT #20 series 4
2007-02-08 02:36 1100The entire class is as follows: ... -
A Google's Interview Question - GLAT #20 series 3
2007-02-08 02:36 1067The roadmap to compute all fixe ... -
A Google's Interview Question - GLAT #20 series 2
2007-02-08 02:24 1072Now, we can deduct the recursio ... -
A Google's Interview Question - GLAT #20 series 1
2007-02-08 02:22 1159Question: Consider a function ...
相关推荐
《Redis桌面管理器Another-Redis-Desktop-Manager详解》 Redis,全称为Remote Dictionary Server,是一种高性能的键值存储系统,常被用作数据库、缓存和消息中间件。其简洁的数据结构和丰富的数据类型使其在分布式...
《Redis桌面视图工具Another-Redis-Desktop-Manager在Windows环境下的应用详解》 Redis,全称Remote Dictionary Server,是一款开源、高性能的键值存储系统,常被用作数据库、缓存和消息中间件。其丰富的数据结构和...
标题中的“Another-Redis-Desktop-Manager.1.5.6”指的是Another Redis Desktop Manager的1.5.6版本。这是一款专为Redis数据库设计的桌面管理工具,它提供了直观的图形用户界面(GUI),方便用户进行数据的查看、...
标题 "Another-Redis-Desktop-Manager.1.5.5" 指向的是一款名为 Another Redis Desktop Manager 的软件,其版本号为 1.5.5。这是一款图形用户界面(GUI)工具,专为管理和操作 Redis 数据库而设计。Redis 是一个开源...
3. **数据浏览与操作**:在数据浏览窗口,用户可以直观地看到Redis中的键值对,包括字符串、哈希、列表、集合、有序集合等各种数据类型。支持搜索、排序和过滤功能,便于查找和管理数据。 4. **命令执行**:除了...
《Redis桌面管理器——Another-Redis-Desktop-Manager 1.3.9详解》 Redis,作为一款高性能的键值数据库,广泛应用于缓存、消息队列等多种场景。为了更好地管理和操作Redis数据库,开发者们设计了一系列的桌面管理...
**Redis桌面管理工具Another-Redis-Desktop-Manager 1.3.7版详解** Redis,全称为Remote Dictionary Server,是一款高性能的键值存储系统,常用于数据库、缓存和消息中间件等场景。其轻量级、速度快的特性使得它在...
Redis桌面(GUI)管理客户端
Redis桌面(GUI)管理客户端
免费神器,redis可视化客户端,支持5.0 stream数据结构
Redis桌面(GUI)管理客户端
Redis桌面(GUI)管理客户端
Another Redis Desktop Manager(简称ARDM)是一个开源的Redis桌面管理工具,它提供了一个图形用户界面(GUI)来方便用户对Redis数据库进行管理和操作。以下是对ARDM 1.6.6版本的简单介绍: 版本:1.6.6 发布日期:...
"Another-Redis-Desktop-Manager.1.4.8"就是这样一个适用于Windows的Redis桌面管理工具。 该工具提供了图形化的界面,使得Redis的日常管理和维护变得更加简单。它支持多种功能,包括连接到本地或远程的Redis服务器...
标题中的"Another-Redis-Desktop-Manager.1.3.8.rar"指的是一个名为"Another Redis Desktop Manager"的软件的版本1.3.8的压缩文件。这是一款针对Redis数据库的桌面管理工具,用于帮助用户在Windows操作系统上更加...
《Another Redis Desktop Manager 1.6.1:免费的Redis可视化管理工具》 Redis,一个高性能的键值数据库,广泛应用于缓存、消息队列、数据存储等多个领域。为了更方便地管理和操作Redis数据库,出现了许多桌面管理...
Redis可视化界面
Redis桌面(GUI)管理客户端
Another-Redis-Desktop-Manager.1.4.2
"Another-Redis-Desktop-Manager.1.5.5.7z" 是一个针对Redis的桌面图形用户界面(GUI)管理工具的压缩包,版本为1.5.5。 Redis Desktop Manager是一款跨平台的Redis管理软件,它提供了一个直观且功能丰富的界面,...