java数独最快解,Java解数独--世界最难数独

在其他地方看到这个方法,稍微修改了一下,贴出来

用到Shudu和Grid两个类,

运行 Shudu里的main方法,

我读取的是本地文件,据说这个是神马大师设计的最难数独,大家可以自行修改

2520a938c02c4bb8a2f5fff1d923b47c.png

package shudu;

import java.io.*;

import java.text.SimpleDateFormat;

import java.util.*;

public class Shudu {

public static void main(String[] args) throws Exception {

SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

Date begin=dfs.parse(dfs.format(new Date()));

String arg = "C:/shudu.txt";

FileReader rd = new FileReader(arg);

while (true) {

Grid grid = Grid.create(rd);

if (grid == null) {

break;

}

List solutions = new ArrayList();

solve(grid, solutions);

printSolutions(grid, solutions);

}

Date end=dfs.parse(dfs.format(new Date()));

long between=end.getTime()-begin.getTime();

System.out.println("use Time:"+between+"ms");

}

private static void solve(Grid grid, List solutions) {

if (solutions.size() >= 2) {

return;

}

int loc = grid.findEmptyCell();

if (loc < 0) {

solutions.add(grid.clone());

return;

}

for (int n=1; n<10; n++) {

if (grid.set(loc, n)) {

solve(grid, solutions);

grid.clear(loc);

}

}

}

private static void printSolutions(Grid grid, List solutions) {

System.out.println("Original");

System.out.println(grid);

if (solutions.size() == 0) {

System.out.println("Unsolveable");

} else if (solutions.size() == 1) {

System.out.println("Solved");

} else {

System.out.println("At least two solutions");

}

for (int i=0; i

System.out.println(solutions.get(i));

}

System.out.println();

System.out.println();

}

}

package shudu;

import java.io.Reader;

public class Grid implements Cloneable {

int[] cells = new int[81];

int[] colsSet = new int[9];

int[] rowsSet = new int[9];

int[] subgridSet = new int[9];

public static Grid create(Reader rd) throws Exception {

Grid grid = new Grid();

for (int loc=0; loc

int ch = rd.read();

if (ch < 0) {

return null;

}

if (ch == '#') {

while (ch >= 0 && ch != '\n' && ch != '\r') {

ch = rd.read();

}

} else if (ch >= '1' && ch <= '9') {

grid.set(loc, ch-'0');

loc++;

} else if (ch == '.' || ch == '0') {

loc++;

}

}

return grid;

}

public int findEmptyCell() {

for (int i=0; i

if (cells[i] == 0) {

return i;

}

}

return -1;

}

public boolean set(int loc, int num) {

// Compute row and column

int r = loc/9;

int c = loc%9;

int blockLoc = (r/3)*3+c/3;

boolean canSet = cells[loc] == 0

&& (colsSet[c] & (1<

&& (rowsSet[r] & (1<

&& (subgridSet[blockLoc] & (1<

if (!canSet) {

return false;

}

cells[loc] = num;

colsSet[c] |= (1<

rowsSet[r] |= (1<

subgridSet[blockLoc] |= (1<

return true;

}

public void clear(int loc) {

// Compute row and column

int r = loc/9;

int c = loc%9;

int blockLoc = (r/3)*3+c/3;

int num = cells[loc];

cells[loc] = 0;

colsSet[c] ^= (1<

rowsSet[r] ^= (1<

subgridSet[blockLoc] ^= (1<

}

public Grid clone() {

Grid grid = new Grid();

grid.cells = cells.clone();

grid.colsSet = colsSet.clone();

grid.rowsSet = rowsSet.clone();

grid.subgridSet = subgridSet.clone();

return grid;

}

public String toString() {

StringBuffer buf = new StringBuffer();

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

if (r%3 == 0) {

buf.append("-------------------------\n");

}

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

if (c%3 == 0) {

buf.append("| ");

}

int num = cells[r*9+c];

if (num == 0) {

buf.append("0 ");

} else {

buf.append(num+" ");

}

}

buf.append("|\n");

}

buf.append("-------------------------");

return buf.toString();

}

}

1f58bf67c3bd54c4fed1fbaf1b7da277.png

http://www.dengb.com/Javabc/526513.htmlwww.dengb.comtruehttp://www.dengb.com/Javabc/526513.htmlTechArticle在其他地方看到这个方法,稍微修改了一下,贴出来 用到Shudu和Grid两个类, 运行 Shudu里的main方法, 我读取的是本地文件,据说这个是神马...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值