- 浏览: 80104 次
文章分类
最新评论
-
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 test cases are divided into 3 classes.
The first one is just a pinpoint test class that contains some corner cases:
The util class is like this:
The second one is to vary the broken level to see whether it's good enough:
The third one is a brutal force testing to test all cases in a certain range:
Now we should have enough confidence.
The first one is just a pinpoint test class that contains some corner cases:
java 代码
- package glassball;
- import junit.framework.TestCase;
- import java.util.List;
- /**
- * Testcases for path.
- */
- public class PathFinderTest extends TestCase
- {
- public void test1BallSingleCase()
- {
- int broken = 10;
- Building building = new Building(10, broken);
- PathFinder finder = new PathFinder(building, 1);
- List path = finder.findPuzzlePath();
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- public void test2BallSingleCase1()
- {
- // 70 is the beginning of the 2nd level search
- int broken = 70;
- Building building = new Building(100, broken);
- PathFinder finder = new PathFinder(building, 2);
- List path = finder.findPuzzlePath();
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- public void test2BallSingleCase2()
- {
- // 77 is the end of the 2nd level search.
- // 77 jumps out of the loop without going to return line.
- int broken = 77;
- Building building = new Building(100, broken);
- PathFinder finder = new PathFinder(building, 2);
- List path = finder.findPuzzlePath();
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- public void test3BallSingleCase()
- {
- int broken = 53;
- Building building = new Building(100, broken);
- PathFinder finder = new PathFinder(building, 3);
- List path = finder.findPuzzlePath();
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- public void test4BallSingleCase()
- {
- // 89 is a special number to test whether
- // totalFloors == baseFloor + totalFloors, it caused
- // 91 has been hit 3 times. Without this check, it kills 3 balls.
- int broken = 89;
- Building building = new Building(100, broken);
- PathFinder finder = new PathFinder(building, 4);
- List path = finder.findPuzzlePath();
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- public void test4BallForLowerBound1()
- {
- int broken = 1;
- Building building = new Building(10, broken);
- PathFinder finder = new PathFinder(building, 4);
- List path = finder.findPuzzlePath();
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- public void test4BallForLowerBound2()
- {
- int broken = 2;
- Building building = new Building(10, broken);
- PathFinder finder = new PathFinder(building, 4);
- List path = finder.findPuzzlePath();
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- public void test4BallForLowerBound3()
- {
- int broken = 6;
- Building building = new Building(10, broken);
- PathFinder finder = new PathFinder(building, 4);
- List path = finder.findPuzzlePath();
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- public void test2Ball4Floors()
- {
- int broken = 1;
- Building building = new Building(4, broken);
- PathFinder finder = new PathFinder(building, 2);
- List path = finder.findPuzzlePath();
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- public void test3Ball17Floors()
- {
- int broken = 13;
- Building building = new Building(17, broken);
- PathFinder finder = new PathFinder(building, 3);
- List path = finder.findPuzzlePath();
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- public void test4Ball100Floors()
- {
- int broken = 70;
- Building building = new Building(100, broken);
- PathFinder finder = new PathFinder(building, 4);
- List path = finder.findPuzzlePath();
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- public void test5Ball100Floors()
- {
- int broken = 70;
- Building building = new Building(100, broken);
- PathFinder finder = new PathFinder(building, 5);
- List path = finder.findPuzzlePath();
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- public void test6Ball100Floors()
- {
- int broken = 70;
- Building building = new Building(100, broken);
- PathFinder finder = new PathFinder(building, 6);
- List path = finder.findPuzzlePath();
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- public void test3Ball9Floors()
- {
- int broken = 9;
- Building building = new Building(100, broken);
- PathFinder finder = new PathFinder(building, 3);
- List path = finder.findPuzzlePath();
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- }
The util class is like this:
java 代码
- package glassball;
- import java.util.List;
- /**
- *
- */
- public class TestUtil
- {
- public static void printPath(List path)
- {
- System.out.print("path=");
- for (int i=0; i<path.size(); i++)
- {
- System.out.print(path.get(i).toString() + "-->");
- }
- System.out.println("END");
- System.out.println("number of trials=" + path.size());
- }
- public static int getPathSize(List path)
- {
- int size = 0;
- for (int i=0; i<path.size(); i++)
- {
- Trial trial = (Trial)path.get(i);
- if (!trial.isDeducted()) size++;
- }
- return size;
- }
- public static int getBrokenFloor(List path)
- {
- // several cases:
- // 1. the last one is broken,
- // 2. the last broken is before several wholes.
- // 3. all are broken, the one we are looking for is not in the list because:
- // it's the last one.
- for (int i=path.size()-1; i>=0; i--)
- {
- Trial trial = (Trial)path.get(i);
- // The last broken piece is the one we are looking for.
- if (trial.isBroken()) return trial.getFloor();
- }
- return -1;
- }
- public static boolean compareTwoIntegerArrays(List path1, List path2)
- {
- if (path1.size() != path2.size()) return false;
- for (int i=0; i<path1.size(); i++)
- {
- if (path1.get(i).equals(path2.get(i))) return false;
- }
- return true;
- }
- }
The second one is to vary the broken level to see whether it's good enough:
java 代码
- package glassball;
- import junit.framework.TestCase;
- import java.util.List;
- /**
- * This is to test the minimal trial numbers.
- */
- public class PathFinderMinimalFullTest extends TestCase
- {
- public void test3Ball100Floors()
- {
- int numOfTrials = 0;
- for (int broken=1; broken < 100; broken++)
- {
- Building building = new Building(100, broken);
- PathFinder finder = new PathFinder(building, 3);
- List path = finder.findPuzzlePath();
- if (TestUtil.getPathSize(path) > numOfTrials)
- numOfTrials = TestUtil.getPathSize(path);
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- System.out.println("numOfTrials=" + numOfTrials);
- }
- public void test4Ball100Floors()
- {
- int numOfTrials = 0;
- for (int broken=1; broken < 100; broken++)
- {
- Building building = new Building(100, broken);
- PathFinder finder = new PathFinder(building, 4);
- List path = finder.findPuzzlePath();
- if (TestUtil.getPathSize(path) > numOfTrials)
- numOfTrials = TestUtil.getPathSize(path);
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- System.out.println("numOfTrials=" + numOfTrials);
- }
- public void test5Ball100Floors()
- {
- int numOfTrials = 0;
- for (int broken=1; broken < 100; broken++)
- {
- Building building = new Building(100, broken);
- PathFinder finder = new PathFinder(building, 5);
- List path = finder.findPuzzlePath();
- if (TestUtil.getPathSize(path) > numOfTrials)
- numOfTrials = TestUtil.getPathSize(path);
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- System.out.println("numOfTrials=" + numOfTrials);
- }
- public void test5Ball1000Floors()
- {
- int numOfTrials = 0;
- for (int broken=1; broken < 200; broken++)
- {
- Building building = new Building(200, broken);
- PathFinder finder = new PathFinder(building, 5);
- List path = finder.findPuzzlePath();
- if (TestUtil.getPathSize(path) > numOfTrials)
- numOfTrials = TestUtil.getPathSize(path);
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- System.out.println("numOfTrials=" + numOfTrials);
- }
- public void test6Ball1000Floors()
- {
- int numOfTrials = 0;
- for (int broken=1; broken < 200; broken++)
- {
- Building building = new Building(200, broken);
- PathFinder finder = new PathFinder(building, 6);
- List path = finder.findPuzzlePath();
- if (TestUtil.getPathSize(path) > numOfTrials)
- numOfTrials = TestUtil.getPathSize(path);
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- System.out.println("numOfTrials=" + numOfTrials);
- }
- public void test10Ball1000Floors()
- {
- int numOfTrials = 0;
- for (int broken=1; broken < 200; broken++)
- {
- Building building = new Building(200, broken);
- PathFinder finder = new PathFinder(building, 50);
- List path = finder.findPuzzlePath();
- if (TestUtil.getPathSize(path) > numOfTrials)
- numOfTrials = TestUtil.getPathSize(path);
- TestUtil.printPath(path);
- assertTrue(broken == TestUtil.getBrokenFloor(path));
- assertTrue(TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- }
- System.out.println("numOfTrials=" + numOfTrials);
- }
- }
The third one is a brutal force testing to test all cases in a certain range:
java 代码
- package glassball;
- import junit.framework.TestCase;
- import java.util.List;
- /**
- * The brutal force loops.
- */
- public class PathFinderFullTest extends TestCase
- {
- public void test1BallSingleCase()
- {
- for (int floors=3; floors<=300; floors++)
- {
- for (int broken=1; broken<floors; broken++)
- {
- for (int balls=1; balls<6; balls++)
- {
- Building building = new Building(floors, broken);
- PathFinder finder = new PathFinder(building, balls);
- List path = finder.findPuzzlePath();
- //TestUtil.printPath(path);
- String errorMsg = "floors=" + floors + " broken=" + broken + " balls=" + balls;
- errorMsg += " path size=" + TestUtil.getPathSize(path) + " minimal trials=" +
- finder.getPuzzle().getMinimalTrials();
- errorMsg += " result floor=" + TestUtil.getBrokenFloor(path);
- assertTrue(errorMsg, TestUtil.getPathSize(path) <= finder.getPuzzle().getMinimalTrials());
- assertTrue(errorMsg, broken == TestUtil.getBrokenFloor(path));
- }
- }
- }
- }
- }
Now we should have enough confidence.
发表评论
-
Revisit 一道应聘智力题的编程求解, Einstein puzzle
2010-03-09 00:36 1451Another brain teaser, titled 一道 ... -
Another Google question - drop glassballs from a building 5
2007-05-13 04:01 1078Here are some results are the t ... -
Another Google question - drop glassballs from a building 3
2007-05-13 03:33 1095The next step is to find the ac ... -
Another Google question - drop glassballs from a building 2
2007-05-13 03:02 1110Here is the code following the ... -
Another Google question - drop glassballs from a building 1
2007-05-12 04:43 1209Here is the question: 原题: ... -
Given n coins and a pan balance,find the only counterfeit 4
2007-02-27 05:54 1119The Utils class is composed of ... -
Given n coins and a pan balance,find the only counterfeit 3
2007-02-27 05:50 1506In the 1-ball case, if the stat ... -
Given n coins and a pan balance,find the only counterfeit 2
2007-02-27 05:29 1109Now it comes to the Scale class ... -
Given n coins and a pan balance, find the only counterfeit 1
2007-02-27 04:49 1340The problem is: There is one ... -
A Google's Interview Question - GLAT #20 series 4
2007-02-08 02:36 1107The entire class is as follows: ... -
A Google's Interview Question - GLAT #20 series 3
2007-02-08 02:36 1070The roadmap to compute all fixe ... -
A Google's Interview Question - GLAT #20 series 2
2007-02-08 02:24 1074Now, we can deduct the recursio ... -
A Google's Interview Question - GLAT #20 series 1
2007-02-08 02:22 1161Question: 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 是一个开源...
4. **命令执行**:除了基本的键值操作,软件还允许用户直接执行Redis命令,如`GET`、`SET`、`DEL`、`PUSH`等,提供了一个命令输入框,方便用户测试和调试命令。 5. **事务处理**:对于需要批量操作的场景,another-...
《Redis桌面管理器——Another-Redis-Desktop-Manager 1.3.9详解》 Redis,作为一款高性能的键值数据库,广泛应用于缓存、消息队列等多种场景。为了更好地管理和操作Redis数据库,开发者们设计了一系列的桌面管理...
**Redis桌面管理工具Another-Redis-Desktop-Manager 1.3.7版详解** Redis,全称为Remote Dictionary Server,是一款高性能的键值存储系统,常用于数据库、缓存和消息中间件等场景。其轻量级、速度快的特性使得它在...
**Redis桌面连接工具Another-Redis-Desktop-manager** Redis是一款高性能的键值数据库,广泛应用于缓存、消息队列以及数据存储等领域。为了方便开发者管理和操作Redis服务器,出现了各种桌面管理工具,其中Another-...
Redis桌面(GUI)管理客户端
Redis桌面(GUI)管理客户端
Another-Redis-Desktop-Manager是一款流行的开源Redis图形化管理工具。版本1.6.7是其稳定发布版之一,提供了对Redis数据库的全面管理功能。该安装包包含了Windows、macOS和Linux系统的可执行文件。 主要特点包括: ...
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可视化界面