收藏 分享(赏)

基于Java解荷兰数学家设计世界最难九宫格(2).doc

上传人:wspkg9802 文档编号:6356629 上传时间:2019-04-09 格式:DOC 页数:7 大小:65.50KB
下载 相关 举报
基于Java解荷兰数学家设计世界最难九宫格(2).doc_第1页
第1页 / 共7页
基于Java解荷兰数学家设计世界最难九宫格(2).doc_第2页
第2页 / 共7页
基于Java解荷兰数学家设计世界最难九宫格(2).doc_第3页
第3页 / 共7页
基于Java解荷兰数学家设计世界最难九宫格(2).doc_第4页
第4页 / 共7页
基于Java解荷兰数学家设计世界最难九宫格(2).doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

1、今日,一则腾讯的新闻称中国老头三天破解世界最难九宫格,虽然最后老人是改了一个数字,但是引起本人一时兴趣,想通过计算机程序求解该问题,于是在宿舍呆了一下午,终于成功求解,程序源码如下。1. package numberGame; 2. 3. 4. public class Point 5. private int col;/ 行号 6. private int row;/ 列号 7. private boolean flag;/ 真为未设置。 8. private int value; 9. / 构造点 10. public Point(int col, int row, boolean fla

2、g, int value) 11. super(); 12. this.col = col; 13. this.row = row; 14. this.flag = flag; 15. this.value = value; 16. 17. 18. public void changeFlag() 19. flag = !flag; 20. 21. 22. 23. public boolean getFlag() 24. return flag; 25. 26. 27. public int getValue() 28. return value; 29. 30. 31. public voi

3、d setValue(int value) 32. this.value = value; 33. 34. 35. public boolean canHere(Point pArr) 36. boolean cb = canCol(pArr); 37. boolean cr = canRow(pArr); 38. boolean cminiArr = canMiniArr(pArr); 39. return cb 40. 41. /判断在小 3*3 格子里是否有相同元素 42. private boolean canMiniArr(Point pArr) 43. int coltemp =

4、this.col % 3; 44. int rowtemp = this.row % 3; 45. 46. for (int i = this.col - coltemp; i al = new ArrayList(); 14. 15. initNumMat(numMat,al); 16. 17. 18. setNum(numMat,al); 19. printMat(numMat); 20. 21. 22. private static void setNum(Point numMat,ArrayList al) 23. int i = 0; 24. int j = 0; 25. do 26

5、. if (numMatij.getFlag() 27. for (int v = numMatij.getValue()+1; v al) throws IOException 69. for (int i = 0; i al) throws IOException 79. BufferedReader br = new BufferedReader(new InputStreamReader(System.in); 80. String p = new String3; 81. String line=null; 82. System.out.println(“请按格式输入点信息(i 行号

6、, j 列号 v 值),输入结束输入 over: i j v “); 83. 84. while(line = br.readLine()!=null) 85. if(line.equals(“over“) 86. break; 87. p = line.trim().split(“ +“); 88. numMatInteger.parseInt(p0)Integer.parseInt(p1).setValue(Integer.parseInt(p2); 89. numMatInteger.parseInt(p0)Integer.parseInt(p1).changeFlag(); 90. a

7、l.add(numMatInteger.parseInt(p0)Integer.parseInt(p1); 91. 92. 93. 94. public static void printMat(Point numMat) 95. System.out.println(“-“); 96. 97. for (int i = 0; i numMat.length; i+) 98. for (int j = 0; j numMati.length; j+) 99. if (j + 1) % 3 = 0) 100. System.out.print(numMatij.getValue() + “ |

8、“);101. else 102. System.out.print(numMatij.getValue() + “ “); 103. 104. if (i + 1) % 3 = 0) 105. System.out.println(“rn-“); 106. else 107. System.out.println(); 108. 109. 110. 111. -运行程序请按格式输入点信息(i 行号, j 列号 v 值),输入结束输入 over: i j v0 0 81 2 31 3 62 1 72 4 92 6 23 1 53 5 74 4 44 5 54 6 75 3 15 7 36 2 16 7 66 8 87 2 87 3 57 7 18 1 98 6 4over-8 1 2 | 7 5 3 | 6 4 9 |9 4 3 | 6 8 2 | 1 7 5 |6 7 5 | 4 9 1 | 2 8 3 |-1 5 4 | 2 3 7 | 8 9 6 |3 6 9 | 8 4 5 | 7 2 1 |2 8 7 | 1 6 9 | 5 3 4 |-5 2 1 | 9 7 4 | 3 6 8 |4 3 8 | 5 2 6 | 9 1 7 |7 9 6 | 3 1 8 | 4 5 2 |-原文链接:http:/

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 网络科技 > Java

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报