- 浏览: 75954 次
文章分类
最新评论
-
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 1366Another brain teaser, titled 一道 ... -
Another Google question - drop glassballs from a building 5
2007-05-13 04:01 1030Here are some results are the t ... -
Another Google question - drop glassballs from a building 4
2007-05-13 03:42 1480The test cases are divided into ... -
Another Google question - drop glassballs from a building 2
2007-05-13 03:02 1055Here is the code following the ... -
Another Google question - drop glassballs from a building 1
2007-05-12 04:43 1159Here is the question: 原题: ... -
Given n coins and a pan balance,find the only counterfeit 4
2007-02-27 05:54 1087The Utils class is composed of ... -
Given n coins and a pan balance,find the only counterfeit 3
2007-02-27 05:50 1441In the 1-ball case, if the stat ... -
Given n coins and a pan balance,find the only counterfeit 2
2007-02-27 05:29 1054Now it comes to the Scale class ... -
Given n coins and a pan balance, find the only counterfeit 1
2007-02-27 04:49 1295The problem is: There is one ... -
A Google's Interview Question - GLAT #20 series 4
2007-02-08 02:36 1041The entire class is as follows: ... -
A Google's Interview Question - GLAT #20 series 3
2007-02-08 02:36 1037The roadmap to compute all fixe ... -
A Google's Interview Question - GLAT #20 series 2
2007-02-08 02:24 1010Now, we can deduct the recursio ... -
A Google's Interview Question - GLAT #20 series 1
2007-02-08 02:22 1106Question: Consider a function ...
相关推荐
Another-Redis-Desktop-Manager.1.6.1
Another-Redis-Desktop-Manager-v1.5.5 | redis 桌面视图工具 |windows 客户端
another-redis-desktop-manager.1.5.5.exe Redis数据库连接软件,界面优美惊喜,方便好用
官网下载太慢,这里做了提取,可以在这里下载
Another-Redis-Desktop-Manager.1.5.5
免费神器,redis可视化客户端,支持5.0 stream数据结构
Redis桌面(GUI)管理客户端
Another-Redis-Desktop-Manager.1.3.8.zip
Another-Redis-Desktop-Manager.1.4.2
Another-Redis-Desktop-Manager.1.4.3
redis的 免费客户端,好用,redis desktop manager已经是收费版本了,然后找到这个好用的客户端,免费,支持中文显示,页面美观清晰大方
redis客户端工具
Redis桌面(GUI)管理客户端
一款免费好用的redis可视化管理工具,Another-Redis-Desktop-Manager
Redis客户端,相对来说比较稳定,在数据量比较大的时候不会崩溃。
Another-Redis-Desktop-Manager.1.6.1.exe 免费连接Redis可视化工具
Redis-Desktop-Manager的替代Another-Redis-Desktop-Manager,挺好用的,此版本为1.3.9
最新版windows Another-Redis-Desktop-Manager.1.4.9.zip
redis可视化管理工具,1.3.7版本,github资源,github下载太慢的可是试试下载这个,Another-Redis-Desktop-Manager.1.3.7.exe
Another-Redis-Desktop-Manager.1.3.6是一款免费的Redis可视化管理工具,基于nodejs开发,可以运行在Windows、Linux、Mac平台,更快、更好、更稳定的redis桌面管理器,更重要的是,当加载大量的key时,它不会崩溃。