欢迎来到天天文库
浏览记录
ID:12049160
大小:65.50 KB
页数:7页
时间:2018-07-15
《基于java解荷兰数学家设计世界最难九宫格(2)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
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.
2、public Point(int col, int row, boolean flag, 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.
3、 23. public boolean getFlag() { 24. return flag; 25. } 26. 27. public int getValue() { 28. return value; 29. } 30. 31. public void setValue(int value) { 32. this.value = value; 33. } 34. 35. public boolean canHere(Point[][] pArr) { 36.
4、 boolean cb = canCol(pArr); 37. boolean cr = canRow(pArr); 38. boolean cminiArr = canMiniArr(pArr); 39. return cb && cr && cminiArr; 1. } 2. //判断在小3*3格子里是否有相同元素 3. private boolean canMiniArr(Point[][] pArr) { 4. int coltemp = this.col % 3;
5、 5. int rowtemp = this.row % 3; 6. 7. for (int i = this.col - coltemp; i < col + (3 - coltemp); i++) { 8. for (int j = this.row - rowtemp; j < row + (3 - rowtemp); j++) { 9. if(i == this.col && j == this.row){ 10. continu
6、e; 11. }else{ 12. if(this.value == pArr[i][j].getValue()){ 13. return false; 14. } 15. } 16. } 17. } 18. return true; 19. } 20. 21.
7、 // 判断列上是否有相同元素 22. private boolean canRow(Point[][] pArr) { 23. for (int i = 0; i < 9; i++) { 24. if (i == this.col) { 25. continue; 26. } else { 27. if (this.value == pArr[i][this.row].value) {// 行变,列不变 28.
8、 return false; 29.
此文档下载收益归作者所有