蜂窝小区最短距离实现 (数学归纳法+广度优先算法)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

?

题目描述:

如图:由正六边形组成的蜂窝小区中,每个正六边形的编号也如图所示。求任意2点间的距离。(规定最大编号不超过100000)







分析:

1、求2点间的距离,第一印象就想到了广度优先算法(广度优先算法简单介绍:如图中的1点找编号为9的点时,可以第一次广度优先找到第二圈的所有元素,第二圈的元素再广度优先可以找到了9号点,所以必有一点可以从1点到9点,且距离为2)。但广度优先算法必须以知道任意点的相邻节点为前提。仔细观察又觉得点与点之间的关联又不是很有规律。

2、很容易看出6个点可以组成一个圆环,如:2,3,4,5,6,7 19,20,38,61,60,36等。为了找出规律,特按照编号顺序进行查找规律,那图看多了容易眼花,特意画了圆环虚线标识一个个圈,然后进行仔细分析;

3、得到所有点的相邻节点的前提是确定所有点的位置,然后再根据位置找规律得出其相邻的节点;




下面说下详细过程:

1、分析如何根据真实编号得到它所对应的圈号即圈内编号:




分析下每个圈的情况(以1号为1圈):






圈号 容量 最小编号 最大编号
1 1 1 1
2 6 2 7
3 12 8 19
4 18 20 37
图示1

1)发现从第2圈开始,元素数量呈等差趋势,依次增加6,容量规律即有:size(n)=6*(n-1) ,同时n>=2;


2)继续观察发现每圈的最小编号减去相邻内圈的最小编号刚好等于内圈的容量即有cycle(n,1)-cycle(n-1,1) = 6*(n-2),同时n>2(第2圈减第一圈规律不成立,n必须大于2),其中cycle(n,1)表示第n圈的第1个元素;

3)根据数列求和可知:



cycle(n,1) -cycle(n-1,1) =6*(n - 2)
cycle(n-1,1)-cycle(n-2,1) =6*((n-1)- 2)
cycle(n-2,1)-cycle(n-3,1) =6*((n-2)- 2)
……
cycle(3,1) -cycle(2,1) =6*((3) - 2)
对以上等式分别左边与左边相加,右边与右边相加可知:cycle(n,1)-cycle(2,1)= 6*((3+4+…n)-(2*(n-2)))=6*((n-2)(n+3)/2-2*(n-2))= 6*(n-2)(n-1)/2=3(n-1)(n-2)


所以cycle(n,1)=cycle(2,1)+3(n-1)(n-2),由图示1可知cycle(2,1)=2;所以cycle(n,1)=2+3(n-1)(n-2),n>2;当n=2时,cycle(n,1)=2+3*(2-1)*(2-2)=2满足要求,所以cycle(n,1)=2+3(n-1)(n-2)的必需条件为n>=2;

3)由上可以推出:第n圈的m个元素可表示成:cycle(n,m)= 2+3(n-1)(n-2) -1+m,其中1<=m<6*(n-1)且n>=2,当n=1时,cycle(1,1)=1;


至此已经可以推出任意一个真实编号对应的圈号与圈内的编号了:

思路:对给定的任意真实编号与第n圈的极大值比较,若大于则取n+1圈继续比较,小于时停止比较,当前圈号为真实编号对应的圈号,根据cycle(n,m)公式可以计算出m值(圈内编号);




2、分析一个点与所有相邻点之间的关系(直接考虑n>=2的情况,n=1时可做特殊处理):


任意一

个点在蜂窝小区中都有6个相邻节点,把它围在中间。

取第3圈的起始点8(第3圈的1号点)的相邻点进行分析,它有2个相邻点在其内圈上,2个点在当前圈,且在它左右;9号点(第3圈的第2点)则只有一个内圈节点,3个相邻外圈节点;

继续分析可知:第4圈的第1、2号点有2个内圈节点,第3号点只有一个内圈节点

……

继续拿第五圈的点进行分析……

可得出如下结论(以第n圈的圈内编号为m的点为例,n>2):

1),当m
2)当m%n-1等于0时,它只有一个圈内节点,其圈内编号为:m*(n-2)/(n-1),外圈编号为:n+m*n/(n-2),n+m*n/(n-2)+1,n+m*n/(n-2)+2;


3)当m>n-1且m%n-1不等于0时,它有2个圈内节点,其编号可参考比它小的、且有3个内圈节点、且最接近的点,根据偏移来计算,公式如下:

m1= (m-m%(n-1))*(n-3)/(n-1)+m%(n-1)-1,m2=m1+1,其外圈的圈内编号为:w1 = (n-1)+((m-m%(n-1))/(n-1)-1)*n+2+m%(n-1)-1,w2 = w1+1


……

不敢再想一遍了,肯定有优化的空间,欢迎给思路给建议……




思路就说到这了:)




说说实现:

1、定义Position类,根据输入的真实编号转换成对应的圈号及圈内编号,并提供找到所有相邻节点的方法;

2、定义蜂窝类Honeycomb,封装了计算蜂窝最短距离的入口及广度优先搜索算法;

3、辅助工具类HoneycombUtils,封装了几个推导出来的数列公式;




[java] view plaincopy
01.import java.util.HashSet;
02.import java.util.Set;
03.
04./**
05. *

 
06. * 蜂窝中的一个点
07. *
08. *

09. */
10.public class Position
11.{
12. /**
13. * 圈号
14. */
15. private int cycleNo;
16.
17. /**
18. * 圈中的编号
19. */
20. private int columnNo;
21.
22. /**
23. * 真实编号
24. */
25. private int realNum;
26.
27. public Position(int realNum)
28. {
29. this.realNum = realNum;
30. transfer();
31. }
32.
33. /**
34. *
 
35. * 获取其所有的6个相邻点
36. *
37. * @return
38. *

39. */
40. public Set getClosestPositions()
41. {
42. Set closestPositions = new HashSet();
43. Position nextPosition = null;
44. Position outerPosition = null;
45.
46. int outerCycleNo = getCycleNo() + 1;
47. int outerRealNum = 0;
48.
49. // 第一圈没有内圈,只有外圈相邻点
50. if (getCycleNo() == 1)
51. {
52. for (int i = 2; i <= 7; i++)
53. {
54.

nextPosition = new Position(i);
55. closestPositions.add(nextPosition);
56. }
57.
58. return closestPositions;
59. }
60.
61. Position innerPosition = null;
62. int innerRealNum = 0;
63. int innerCycleNo = getCycleNo() - 1;
64.
65. // 当前点的列号低于圈号减1时,它与2个相邻的内圈点相邻
66. if (getColumnNo() < innerCycleNo)
67. {
68. // 表示取最后一列(列号最大的那个点)
69. if (getColumnNo() == 1)
70. {
71. innerPosition = getMaxCyclePosition(innerCycleNo);
72. }
73. // 否则内圈列号取getColumnNo()-1
74. else
75. {
76. innerRealNum = HoneycombUtils.getMinOfRealNumInCycle(innerCycleNo) - 1 + getColumnNo() - 1;
77. innerPosition = new Position(innerRealNum);
78. }
79. closestPositions.add(innerPosition);
80.
81. // 添加另一个内圈相邻点
82. innerRealNum = HoneycombUtils.getMinOfRealNumInCycle(innerCycleNo) - 1 + getColumnNo();
83. innerPosition = new Position(innerRealNum);
84. closestPositions.add(innerPosition);
85.
86. // 添加2个外圈相邻点
87. outerRealNum = HoneycombUtils.getMinOfRealNumInCycle(outerCycleNo) - 1 + getColumnNo();
88. outerPosition = new Position(outerRealNum);
89. closestPositions.add(outerPosition);
90.
91. closestPositions.add(outerPosition.getNextPosition());
92. }
93. // 内环只有一个相邻点
94. else if (getColumnNo() % innerCycleNo == 0)
95. {
96. if (innerCycleNo == 1)
97. {
98. innerPosition = new Position(1);
99. }
100. else
101. {
102. innerRealNum =
103. HoneycombUtils.getMinOfRealNumInCycle(innerCycleNo) - 1 + getColumnNo() * (innerCycleNo - 1)
104. / innerCycleNo;
105. innerPosition = new Position(innerRealNum);
106. }
107. closestPositions.add(innerPosition);
108.
109. // 添加3个外圈相邻的点
110. outerRealNum =
111. HoneycombUtils.getMinOfRealNumInCycle(outerCycleNo) - 1 + innerCycleNo
112. + (getColumnNo() / innerCycleNo - 1) * getCycleNo();
113.
114. outerPosition = new Position(outerRealNum);
115. closestPositions.add(outerPosition);
116.
117. nextPosition = outerPosition.getNextPosition();
118. closestPositions.add(nextPosition);
119.


120. nextPosition = nextPosition.getNextPosition();
121. closestPositions.add(nextPosition);
122.
123. }
124. // 否则内环有2个相邻点
125. else
126. {
127. // 找出同一圈中列号较小且只有一个相邻点的点
128. int addColumnNo = getColumnNo() % innerCycleNo;
129. int criticalColumnNo = getColumnNo() - addColumnNo;
130.
131. innerRealNum =
132. HoneycombUtils.getMinOfRealNumInCycle(innerCycleNo) - 1 + criticalColumnNo * (innerCycleNo - 1)
133. / innerCycleNo + addColumnNo - 1;
134.
135. innerPosition = new Position(innerRealNum);
136. closestPositions.add(innerPosition);
137.
138. closestPositions.add(innerPosition.getNextPosition());
139.
140. // 添加另一个外圈点
141. outerRealNum =
142. HoneycombUtils.getMinOfRealNumInCycle(outerCycleNo) - 1 + innerCycleNo
143. + (criticalColumnNo / innerCycleNo - 1) * getCycleNo() + 2 + addColumnNo - 1;
144.
145. outerPosition = new Position(outerRealNum);
146. closestPositions.add(outerPosition);
147.
148. closestPositions.add(outerPosition.getNextPosition());
149. }
150.
151. // 添加本圈中的2个与之相邻的点
152. closestPositions.add(getLastPosition());
153. closestPositions.add(getNextPosition());
154.
155. return closestPositions;
156. }
157.
158. /**
159. * 重载方法
160. *
161. * @return
162. */
163. @Override
164. public int hashCode()
165. {
166. final int prime = 31;
167. int result = 1;
168. result = prime * result + realNum;
169. return result;
170. }
171.
172. /**
173. * 重载方法
174. *
175. * @param obj
176. * @return
177. */
178. @Override
179. public boolean equals(Object obj)
180. {
181. if (this == obj)
182. return true;
183. if (obj == null)
184. return false;
185. if (getClass() != obj.getClass())
186. return false;
187. Position other = (Position)obj;
188. if (realNum != other.realNum)
189. return false;
190. return true;
191. }
192.
193. /**
194. * 重载方法
195. *
196. * @return
197. */
198. @Override
199. public String toString()
200. {
201. return "Position [realNum=" + realNum + "]";
202. }
203.
204. /**
205. *

 
206. * 把蜂窝上一个点的编号转换

成圈号 + 圈内的编号
207. *


208. */
209. private void transfer()
210. {
211. // 在第一圈
212. if (getRealNum() == 1)
213. {
214. setCycleNo(1);
215. setColumnNo(1);
216. }
217. // 在其它圈
218. else
219. {
220. int cycle = 2;
221. while (HoneycombUtils.getMaxOfRealNumInCycle(cycle) < getRealNum())
222. {
223. cycle++;
224. }
225. setCycleNo(cycle);
226.
227. // 计算真实编号对应圈中的编号
228. int minCycleNum = HoneycombUtils.getMinOfRealNumInCycle(cycle);
229. int columnNo = getRealNum() - minCycleNum + 1;
230. setColumnNo(columnNo);
231. }
232. }
233.
234. /**
235. *
 
236. * 获取一圈中的下一个位置,如果是最后一个位置,下一个指向第一个;否则列号加1
237. *
238. * @return
239. *

240. */
241. private Position getNextPosition()
242. {
243. // 最后一个的下一个指向圈中的第一个
244. if (getRealNum() == HoneycombUtils.getMaxOfRealNumInCycle(getCycleNo()))
245. {
246. return getMinCyclePosition();
247. }
248. else
249. {
250. return new Position(getRealNum() + 1);
251. }
252. }
253.
254. /**
255. *
 
256. * 获取同一圈中列号小1的点
257. *
258. * @return
259. *

260. */
261. private Position getLastPosition()
262. {
263. if (getRealNum() == HoneycombUtils.getMinOfRealNumInCycle(getCycleNo()))
264. {
265. return getMaxCyclePosition();
266. }
267. else
268. {
269. return new Position(getRealNum() - 1);
270. }
271. }
272.
273. /**
274. *
 
275. * 获取指定圈的最大列号对应的点
276. *
277. * @param cycleNo
278. * @return
279. *

280. */
281. private Position getMaxCyclePosition(int cycleNo)
282. {
283. return new Position(HoneycombUtils.getMaxOfRealNumInCycle(cycleNo));
284. }
285.
286. /**
287. *
 
288. * 获取当前圈的最大点
289. *
290. * @return
291. *

292. */
293. private Position getMaxCyclePosition()
294. {
295. return getMaxCyclePosition(getCycleNo());
296. }
297.
298. /**
299. *
 
300. * 获取指定圈中最小列号对应的最小点
301. *
302. * @return
303. *

304. */
305. private Position getMinCyclePosition(int cycleNo)
306. {
307. return new Po

sition(HoneycombUtils.getMinOfRealNumInCycle(cycleNo));
308. }
309.
310. /**
311. *

 
312. * 获取当前圈的最小点
313. *
314. * @return
315. *

316. */
317. private Position getMinCyclePosition()
318. {
319. return getMinCyclePosition(getCycleNo());
320. }
321.
322. /**
323. * 获取 cycleNo
324. *
325. * @return 返回 cycleNo
326. */
327. private int getCycleNo()
328. {
329. return cycleNo;
330. }
331.
332. /**
333. * 获取 columnNo
334. *
335. * @return 返回 columnNo
336. */
337. private int getColumnNo()
338. {
339. return columnNo;
340. }
341.
342. /**
343. * 获取 realNum
344. *
345. * @return 返回 realNum
346. */
347. private int getRealNum()
348. {
349. return realNum;
350. }
351.
352. /**
353. * 设置 cycleNo
354. *
355. * @param 对cycleNo进行赋值
356. */
357. private void setCycleNo(int cycleNo)
358. {
359. this.cycleNo = cycleNo;
360. }
361.
362. /**
363. * 设置 columnNo
364. *
365. * @param 对columnNo进行赋值
366. */
367. private void setColumnNo(int columnNo)
368. {
369. this.columnNo = columnNo;
370. }
371.}





[java] view plaincopy
01./**
02. *
 
03. * 蜂窝工具类
04. *
05. *

06. */
07.public final class HoneycombUtils
08.{
09.
10. /**
11. * <默认构造函数>
12. */
13. private HoneycombUtils()
14. {
15. }
16.
17. /**
18. *
 
19. * 获取蜂窝一圈中的最大真实编号(圈号>=2)
20. *
21. * @param cycleNo 圈号
22. * @return
23. *

24. */
25. public static int getMaxOfRealNumInCycle(int cycleNo)
26. {
27. return getMinOfRealNumInCycle(cycleNo) + getMaxColumnNumInCycle(cycleNo) - 1;
28. }
29.
30. /**
31. *
 
32. * 获取蜂窝一圈中的最小真实编号
33. *
34. * @param cycleNo 圈号
35. * @return
36. *

37. */
38. public static int getMinOfRealNumInCycle(int cycleNo)
39. {
40. return 2 + 3 * (cycleNo - 1) * (cycleNo - 2);
41. }
42.
43. /**
44. *
 
45. * 获取一圈中的最大列号(圈号>=2)
46. *
47. * @param cycleNo 圈号
48. * @return
49. *

50. */
51. public static int getMaxColumnNumInCycle(int cycleNo)
52. {
53. return 6 * (cycleNo - 1);
54. }
55.}





[java] view plaincopy
01.import java.util.HashSet;
02.import java.util.Set;
03.
04./**
05. *
 
06. * 蜂窝
07. *
08

. *


09. */
10.public final class Honeycomb
11.{
12. private static int count;
13.
14. private static Set positions = new HashSet();
15.
16. private static Set closestPositions = new HashSet();
17.
18. private Honeycomb()
19. {
20. }
21.
22. /**
23. *
 
24. * 启动入口,获取蜂窝中2个点的距离
25. *
26. * @param startPos 起始位置
27. * @param endPos 查找目标位置
28. * @return
29. *

30. */
31. public static int getDistance(int startPos, int endPos)
32. {
33. if (!check(startPos) || !check(endPos))
34. {
35. return -1;
36. }
37.
38. Position startPosition = new Position(Math.min(startPos, endPos));
39. Position endPosition = new Position(Math.max(startPos, endPos));
40.
41. return getDistance(startPosition, endPosition);
42. }
43.
44. private static boolean check(int pos)
45. {
46. if (pos <= 0 || pos > 100000)
47. {
48. return false;
49. }
50. return true;
51. }
52.
53. /**
54. *
 
55. * 初始化
56. *
57. *

58. */
59. private static void init()
60. {
61. positions.clear();
62. count = 0;
63. closestPositions.clear();
64. }
65.
66. /**
67. *
 
68. * 封装成内部需要的入口
69. *
70. * @param startPosition 起始点
71. * @param endPosition 目标点
72. * @return
73. *

74. */
75. private static int getDistance(Position startPosition, Position endPosition)
76. {
77. init();
78. closestPositions.add(startPosition);
79. searchNext(endPosition);
80. return count;
81. }
82.
83. /**
84. *
 
85. * 广度优先遍历,每次取当前点的所有相邻点跟目标点比较,相等就退出,否则一直迭代查找
86. *
87. * @param endPosition
88. *

89. */
90. private static void searchNext(Position endPosition)
91. {
92. if (closestPositions.contains(endPosition))
93. {
94. return;
95. }
96.
97. Set copyPositions = new HashSet(closestPositions.size());
98. copyPositions.addAll(closestPositions);
99.
100. closestPositions.clear();
101.
102. for (Position closestPosition : copyPositions)
103. {
104. addClosestPositions(closestPosition.getClosestPositions());
105. }
106.
107. count++;
108. searchNext(endPosition);
109. }
110.
111. private

static void addClosestPositions(Set oneOfpositions)
112. {
113. for (Position position : oneOfpositions)
114. {
115. if (!positions.contains(position))
116. {
117. closestPositions.add(position);
118. }
119.
120. positions.add(position);
121. }
122. }
123.}


测试类:

PositionTest:验证获取点的相邻节点是否正确:





[java] view plaincopy
01.import static org.junit.Assert.assertEquals;
02.import static org.junit.Assert.assertTrue;
03.
04.import java.util.Set;
05.
06.import org.junit.Test;
07.
08./**
09. *

 
10. * <一句话功能简述>
11. *
12. *

13. */
14.public class PositionTest
15.{
16.
17. /**
18. * case01:极限情况:第一圈的所有相邻节点是否正确
19. */
20. @Test
21. public void testGetClosestPositions01()
22. {
23. Position pos = new Position(1);
24.
25. Set positions = pos.getClosestPositions();
26.
27. assertEquals(positions.size(), 6);
28.
29. for (int i = 2; i <= 7; i++)
30. {
31. assertTrue(positions.contains(new Position(i)));
32. }
33. }
34.
35. /**
36. * case02:正常情况:第二圈的某点所有相邻节点是否正确
37. */
38. @Test
39. public void testGetClosestPositions02()
40. {
41. Position pos = new Position(2);
42.
43. Set positions = pos.getClosestPositions();
44.
45. assertEquals(positions.size(), 6);
46.
47. assertTrue(positions.contains(new Position(1)));
48. assertTrue(positions.contains(new Position(3)));
49. assertTrue(positions.contains(new Position(7)));
50. assertTrue(positions.contains(new Position(8)));
51. assertTrue(positions.contains(new Position(9)));
52. assertTrue(positions.contains(new Position(10)));
53.
54. pos = new Position(7);
55. positions = pos.getClosestPositions();
56. assertTrue(positions.contains(new Position(1)));
57. assertTrue(positions.contains(new Position(2)));
58. assertTrue(positions.contains(new Position(6)));
59. assertTrue(positions.contains(new Position(8)));
60. assertTrue(positions.contains(new Position(18)));
61. assertTrue(positions.contains(new Position(19)));
62.
63. pos = new Position(6);
64. positions = pos.getClosestPositions();
65. assertTrue(positions.contains(new Position(1)));
66. assertTrue(positions.contains(new Position(5)));
67. assertTrue(positions.contains(new Position(7)));
68. assertTrue(positions.contains(new Position(

16)));
69. assertTrue(positions.contains(new Position(17)));
70. assertTrue(positions.contains(new Position(18)));
71. }
72.
73. /**
74. * case03:正常情况:第三圈的某点所有相邻节点是否正确
75. */
76. @Test
77. public void testGetClosestPositions03()
78. {
79. Position pos = new Position(8);
80.
81. Set positions = pos.getClosestPositions();
82.
83. assertEquals(positions.size(), 6);
84.
85. assertTrue(positions.contains(new Position(2)));
86. assertTrue(positions.contains(new Position(7)));
87. assertTrue(positions.contains(new Position(9)));
88. assertTrue(positions.contains(new Position(19)));
89. assertTrue(positions.contains(new Position(20)));
90. assertTrue(positions.contains(new Position(21)));
91.
92. pos = new Position(9);
93. positions = pos.getClosestPositions();
94. assertTrue(positions.contains(new Position(2)));
95. assertTrue(positions.contains(new Position(8)));
96. assertTrue(positions.contains(new Position(10)));
97. assertTrue(positions.contains(new Position(21)));
98. assertTrue(positions.contains(new Position(22)));
99. assertTrue(positions.contains(new Position(23)));
100.
101. pos = new Position(11);
102. positions = pos.getClosestPositions();
103. assertTrue(positions.contains(new Position(3)));
104. assertTrue(positions.contains(new Position(10)));
105. assertTrue(positions.contains(new Position(12)));
106. assertTrue(positions.contains(new Position(24)));
107. assertTrue(positions.contains(new Position(25)));
108. assertTrue(positions.contains(new Position(26)));
109.
110. pos = new Position(12);
111. positions = pos.getClosestPositions();
112. assertTrue(positions.contains(new Position(3)));
113. assertTrue(positions.contains(new Position(4)));
114. assertTrue(positions.contains(new Position(11)));
115. assertTrue(positions.contains(new Position(13)));
116. assertTrue(positions.contains(new Position(27)));
117. assertTrue(positions.contains(new Position(26)));
118.
119. pos = new Position(19);
120. positions = pos.getClosestPositions();
121. assertTrue(positions.contains(new Position(7)));
122. assertTrue(positions.contains(new Position(8)));
123. assertTrue(positions.contains(new Position(18)));
124. assertTrue(positions.contains(new Position(20)));
125. assertTrue(positions.contains(new Position(36)));
126. assertTrue(positions.contains(new Position(37)));
127. }
128.
129. /**
130. * case03:正常情况:第三圈的

某点所有相邻节点是否正确
131. */
132. @Test
133. public void testGetClosestPositions04()
134. {
135. Position pos = new Position(20);
136.
137. Set positions = pos.getClosestPositions();
138.
139. assertEquals(positions.size(), 6);
140.
141. assertTrue(positions.contains(new Position(8)));
142. assertTrue(positions.contains(new Position(19)));
143. assertTrue(positions.contains(new Position(21)));
144. assertTrue(positions.contains(new Position(37)));
145. assertTrue(positions.contains(new Position(38)));
146. assertTrue(positions.contains(new Position(39)));
147.
148. pos = new Position(21);
149. positions = pos.getClosestPositions();
150. assertTrue(positions.contains(new Position(9)));
151. assertTrue(positions.contains(new Position(8)));
152. assertTrue(positions.contains(new Position(40)));
153. assertTrue(positions.contains(new Position(20)));
154. assertTrue(positions.contains(new Position(22)));
155. assertTrue(positions.contains(new Position(39)));
156.
157. pos = new Position(25);
158. positions = pos.getClosestPositions();
159. assertTrue(positions.contains(new Position(11)));
160. assertTrue(positions.contains(new Position(24)));
161. assertTrue(positions.contains(new Position(26)));
162. assertTrue(positions.contains(new Position(44)));
163. assertTrue(positions.contains(new Position(45)));
164. assertTrue(positions.contains(new Position(46)));
165.
166. pos = new Position(29);
167. positions = pos.getClosestPositions();
168. assertTrue(positions.contains(new Position(13)));
169. assertTrue(positions.contains(new Position(14)));
170. assertTrue(positions.contains(new Position(28)));
171. assertTrue(positions.contains(new Position(30)));
172. assertTrue(positions.contains(new Position(50)));
173. assertTrue(positions.contains(new Position(51)));
174.
175. pos = new Position(37);
176. positions = pos.getClosestPositions();
177. assertTrue(positions.contains(new Position(19)));
178. assertTrue(positions.contains(new Position(20)));
179. assertTrue(positions.contains(new Position(36)));
180. assertTrue(positions.contains(new Position(38)));
181. assertTrue(positions.contains(new Position(60)));
182. assertTrue(positions.contains(new Position(61)));
183. }
184.}


HoneycombTest类,验证距离计算是否正确



[java] view plaincopy
01.import static org.junit.Assert.assertEquals;
02.
03.import org.junit.Test;
04.
05./**
06. *

 
07. * <一句话功能简述>
08. *
09.

*


10. */
11.public class HoneycombTest
12.{
13. /**
14. *
 
15. * CASE01:参数非法
16. *
17. *

18. */
19. @Test
20. public void testGetDistance01()
21. {
22. assertEquals(Honeycomb.getDistance(1, 0), -1);
23. assertEquals(Honeycomb.getDistance(-1, 1), -1);
24. assertEquals(Honeycomb.getDistance(-1, -1), -1);
25. }
26.
27. /**
28. *
 
29. * CASE02:临界值
30. *
31. *

32. */
33. @Test
34. public void testGetDistance02()
35. {
36. assertEquals(Honeycomb.getDistance(1, 1), 0);
37. assertEquals(Honeycomb.getDistance(2, 2), 0);
38. }
39.
40. /**
41. *
 
42. * CASE03:正常情况
43. *
44. *

45. */
46. @Test
47. public void testGetDistance03()
48. {
49. assertEquals(Honeycomb.getDistance(2, 40), 3);
50.
51. assertEquals(Honeycomb.getDistance(49, 61), 8);
52.
53. assertEquals(Honeycomb.getDistance(50, 44), 5);
54.
55. assertEquals(Honeycomb.getDistance(47, 59), 8);
56.
57. assertEquals(Honeycomb.getDistance(49, 57), 8);
58.
59. assertEquals(Honeycomb.getDistance(50, 44), 5);
60.
61. assertEquals(Honeycomb.getDistance(100000, 1), 183);
62.
63. assertEquals(Honeycomb.getDistance(100000, 88888), 355);
64. }
65.}























分享到:

上一篇:中缀式转后缀式工具类实现
.
顶9踩0.



查看评论

12楼 cmdblock 2013-07-16 19:37发表 [回复] 阁下那么帅气的蜂窝图是用什么软件画的11楼 z00171981 2013-07-04 00:05发表 [回复] 坐标循环赋值即可

while (AfterIter->second.HoneyPointId <= MaxPointNum)
{
m_step = AfterIter->second.CycleId - 1;
MOVE(BeginIter, AfterIter, DOWN, 1);
NUMISOUT(MaxPointNum);
MOVE(BeginIter, AfterIter, LDOWN, m_step - 1);
NUMISOUT(MaxPointNum);
MOVE(BeginIter, AfterIter, LUP, m_step);
NUMISOUT(MaxPointNum);
MOVE(BeginIter, AfterIter, UP, m_step);
NUMISOUT(MaxPointNum);
MOVE(BeginIter, AfterIter, RUP, m_step);
NUMISOUT(MaxPointNum);
MOVE(BeginIter, AfterIter, RDOWN, m_step);
NUMISOUT(MaxPointNum);
MOVE(BeginIter, AfterIter, DOWN, m_step);
NUMISOUT(MaxPointNum);
}10楼 z00171981 2013-07-04 00:05发表 [回复] [引用] [举报]用状态机保存反应坐标,下-》左下-》左上-》上-》右上-》右下-》下状态,移动函数
void HoneyOperation::MOVE(map::iterator& BePoint, map::iterator& AfPoint, MOVEACTION moveact, int step)
{
for (int i = 0; i {
AfPoint->second.PointCoord.xRay = BePoint->second.PointCoord.xRay + ACTIONARRAY[moveact][0];
AfPoint->second.PointCoord.yRay = BePoint->se

cond.PointCoord.yRay + ACTIONARRAY[moveact][1];
AfPoint->second.PointCoord.zRay = BePoint->second.PointCoord.zRay + ACTIONARRAY[moveact][2];
BePoint = AfPoint;
if (AfPoint->second.HoneyPointId == g_honey_num)
{
break;
}
AfPoint++;
}
return;
}

相关文档
最新文档