日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

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

發(fā)布時間:2023/12/14 java 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java数独最快解,Java解数独--世界最难数独 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在其他地方看到這個方法,稍微修改了一下,貼出來

用到Shudu和Grid兩個類,

運行 Shudu里的main方法,

我讀取的是本地文件,據(jù)說這個是神馬大師設(shè)計的最難數(shù)獨,大家可以自行修改

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();

}

}

http://www.dengb.com/Javabc/526513.htmlwww.dengb.comtruehttp://www.dengb.com/Javabc/526513.htmlTechArticle在其他地方看到這個方法,稍微修改了一下,貼出來 用到Shudu和Grid兩個類, 運行 Shudu里的main方法, 我讀取的是本地文件,據(jù)說這個是神馬...

總結(jié)

以上是生活随笔為你收集整理的java数独最快解,Java解数独--世界最难数独的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。