当前位置:文档之家› 完整的九宫格算法程序

完整的九宫格算法程序

for (int j = 0; j < 9; j++) {
int aa = jiuGong.get_nine()[i][j]; boolean bb = jiuGong.get_bool()[i][j]; d[i][j] = aa; b[i][j] = bb; } } temp.set_bool(b); temp.set_nine(d); return temp; }
//空格中数字已经存在,则返回null if (existsGrid[x][y]) {
return null; } List<Integer> tempList = new ArrayList<Integer>(); // 找到x轴剩余的数字 for (int i = 1; i < 10; i++) {
//压栈 public void push(NineGrid jiuGong) {
nineGridList.add(jiuGong); }
//弹栈 public NineGrid pop() {
if (nineGridList.size() > 0) {
NineGrid jiuGong = newJiuGong((NineGrid) nineGridList.remove(nineGridList.size() - 1)); return jiuGong; } else { // System.out.println("题目给定九宫格无解"); return null; } }
return existsGrid;
} public void set_bool(Boolean[][] _bool) {
this.existsGrid = _bool; } public int getX() {
return x; } public void setX(int x) {
this.x = x; } public int getY() {
System.out.println("此题无解, 请检查题目给定九宫格是否错误! "); break; } else { char t = least.charAt(0);//取出x、y坐标标志
package ninegrid; import java.util.ArrayList; import java.util.List;
/**
* 功能说明:九宫格功能实现bean
*
* @author ClarkJia
*
*/
public class NineGrid
{
protected Integer nineGrid[][] = new Integer[9][9]; //九宫格阵列图
if (nineGrid[j][i] == 0) {
temp++; } } if (temp > 0 && temp < xLeast) { yLeast = temp; yI = i; } } }
if (xLeast < yLeast)
{ return "x" + xI;
} else if (yLeast <= xLeast) {
Integer t = (Integer) tempList.get(i);
// 检查这个是否能满足小三宫格 int starti = (x / 3) * 3; int startj = (y / 3) * 3; boolean hasEqual = false;// 表示是否已经存在,若存在则删除 for (int m = starti; m < starti + 3; m++) {
boolean hasValue = false; for (int k = 0; k < 9; k++) {
if (nineGrid[x][k] == i) {
hasValue = true; break; } } // 在x轴中没有找到 if (!hasValue) { tempList.add(i); } }
for (int n = startj; n < startj + 3; n++) {
if (nineGrid[m][n] != null && t.equals(nineGrid[m][n])) {
hasEqual = true; break; } } } if (hasEqual) { tempList.remove(i); i--; } }
// 找xy轴都没有的数字 for (int i = 0; i < tempList.size(); i++) {
Integer t = (Integer) tempList.get(i); boolean hasValue = false; for (int k = 0; k < 9; k++) {
} package ninegrid;
/** * 功能说明:九宫格计算 * * @author ClarkJia * */
public class NineGridTest {
/** * 九宫格功能实现核心算法 * @return */
public NineGrid genGridResult(NineGrid nineGrid) {
for (int j = 0; j < 9; j++) {
System.out.print(" | " + nineGrid[i][j]); } System.out.println(" |"); System.out.println(" ------------------------------------"); } }
package ninegrid; import java.util.ArrayList; import java.util.List; /**
* 功能说明:九宫格堆栈模拟 * * @author ClarkJia * */
public class NineGridList {
private List<NineGrid> nineGridList = new ArrayList<NineGrid>();//模拟九宫格 list 堆栈
if (nineGrid[k][y] == t.intValue()) {
hasValue = true; break; } } // y轴中找到x的数字则删除它 if (hasValue) { tempList.remove(i); i--; } }
for (int i = 0; i < tempList.size(); i++) {
return y; } public void setY(int y) {
this.y = y; } public void setValue(int value) {
nineGrid[x][y] = value; existsGrid[x][y] = true; } public int getValue() { return nineGrid[x][y]; } public int getTimes() { return times; } public void setTimes(int times) { this.times = times; } }
// 初始构造九宫格 public NineGrid(Integer[][] initData) {
//ArrayUtils.cloneArray(nineGrid, initData);//复制原始九宫格 for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
return returnInts; }
/** * 查找横行或竖列中空格最少的那个点位置 * @return */
public String getLeastLocation() {
int xLeast = 10; // x 轴中空缺数最少个数 int xI = 10; // 索引 int yLeast = 100; int yI = 10; for (int i = 0; i < 9; i++) {
//复制九宫格 public NineGrid newJiuGong(final NineGrid jiuGong) {
NineGrid temp = new NineGrid(); Intege] b = new Boolean[9][9]; for (int i = 0; i < 9; i++) {
NineGridList stack = new NineGridList(); int times = 0;//计算次数
while (nineGrid != null && !nineGrid.checkResult()) {
times++; String least = nineGrid.getLeastLocation(); if (least == null) {
/** * 检查是否生成成功 * * @return */
public boolean checkResult() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (nineGrid[i][j].intValue() == 0) {
相关主题