您的位置:首页 > 编程语言 > Java开发

【基于java的九宫格求解程序】

2013-09-14 15:12 134 查看
今日,一则腾讯的新闻称中国老头三天破解世界最难九宫格,虽然最后老人是改了一个数字,但是引起本人一时兴趣,想通过热门主题之家计算机程序求解该问题,于是在宿舍呆了一下午,终于成功求解,程序源码如下。

[java] package numberGame;

public class Point {

private int col;// 行号

private int row;// 列号

private boolean flag;// 真为未设置。

private int value;

// 构造点

public Point(int col, int row, boolean flag, int value) {

super();

this.col = col;

this.row = row;

this.flag = flag;

this.value = value;

}

public void changeFlag() {

flag = !flag;

}

public boolean getFlag() {

return flag;

}

public int getValue() {

return value;

}

public void setValue(int value) {

this.value = value;

}

public boolean canHere(Point[][] pArr) {

boolean cb = canCol(pArr);

boolean cr = canRow(pArr);

boolean cminiArr = canMiniArr(pArr);

return cb && cr && cminiArr;

}

//判断在小3*3格子里是否有相同元素

private boolean canMiniArr(Point[][] pArr) {

int coltemp = this.col % 3;

int rowtemp = this.row % 3;

for (int i = this.col - coltemp; i < col + (3 - coltemp); i++) {

for (int j = this.row - rowtemp; j < row + (3 - rowtemp); j++) {

if(i == this.col && j == this.row){

continue;

}else{

if(this.value == pArr[i][j].getValue()){

return false;

}

}

}

}

return true;

}

// 判断列上是否有相同元素

private boolean canRow(Point[][] pArr) {

for (int i = 0; i < 9; i++) {

if (i == this.col) {

continue;

} else {

if (this.value == pArr[i][this.row].value) {// 行变,列不变

return false;

}

}

}

return true;

}

// 判断行上是否有相同元素

private boolean canCol(Point[][] pArr) {

for (int i = 0; i < 9; i++) {

if (i == this.row) {

continue;

} else {

if (this.value == pArr[this.col][i].value) {// 列边,行不变

return false;

}

}

}

return true;

}

}

package numberGame;

public class Point {

private int col;// 行号

private int row;// 列号

private boolean flag;// 真为未设置。

private int value;

// 构造点

public Point(int col, int row, boolean flag, int value) {

super();

this.col = col;

this.row = row;

this.flag = flag;

this.value = value;

}

public void changeFlag() {

flag = !flag;

}

public boolean getFlag() {

return flag;

}

public int getValue() {

return value;

}

public void setValue(int value) {

this.value = value;

}

public boolean canHere(Point[][] pArr) {

boolean cb = canCol(pArr);

boolean cr = canRow(pArr);

boolean cminiArr = canMiniArr(pArr);

return cb && cr && cminiArr;

}

//判断在小3*3格子里是否有相同元素

private boolean canMiniArr(Point[][] pArr) {

int coltemp = this.col % 3;

int rowtemp = this.row % 3;

for (int i = this.col - coltemp; i < col + (3 - coltemp); i++) {

for (int j = this.row - rowtemp; j < row + (3 - rowtemp); j++) {

if(i == this.col && j == this.row){

continue;

}else{

if(this.value == pArr[i][j].getValue()){

return false;

}

}

}

}

return true;

}

// 判断列上是否有相同元素

private boolean canRow(Point[][] pArr) {

for (int i = 0; i < 9; i++) {

if (i == this.col) {

continue;

} else {

if (this.value == pArr[i][this.row].value) {// 行变,列不变

return false;

}

}

}

return true;

}

// 判断行上是否有相同元素

private boolean canCol(Point[][] pArr) {

for (int i = 0; i < 9; i++) {

if (i == this.row) {

continue;

} else {

if (this.value == pArr[this.col][i].value) {// 列边,行不变

return false;

}

}

}

return true;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: