资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java数独代码csdn java数独课程设计

c++或java 写一个解3阶数独的程序

123456789101112131415161718192021public static Geocache[] createGeocaches(int a) { if(a = 0) return new Geocache[0]; Random rand = new Random(); Geocache[] result = new Geocache[a]; for(int i = 0; i a; i++) { //因为题目没有描述,这里假设x, y是随机整数,Geocache有a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dhrjIWn1D4n19hmWDzm1R0IAYqnWm3PW64rj0d0AP8IA3qPjfsn1bkrjKxmLKz0ZNzUjdCIZwsrBtEXh9GuA7EQhF9pywdQhPEUiqkIyN1IA-EUBtkPWm4rjR4rHbLPWR1nH63P16L" target="_blank" class="baidu-highlight"构造函数/a(int, int) int x = rand.nextInt(); int y = rand.nextInt(); result[i] = new Geocache(x, y); } return result; }

公司主营业务:网站设计制作、成都网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出通榆免费做网站回馈大家。

Java数独游戏代码

public class ShuDu {

/**存储数字的数组*/

static int[][] n = new int[9][9];

/**生成随机数字的源数组,随机数字从该数组中产生*/

static int[] num = {1,2,3,4,5,6,7,8,9};

public static void main(String[] args) {

//生成数字

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

//尝试填充的数字次数

int time = 0;

//填充数字

for(int j = 0;j 9;j++){

//产生数字

n[i][j] = generateNum(time);

//如果返回值为0,则代表卡住,退回处理

//退回处理的原则是:如果不是第一列,则先倒退到前一列,否则倒退到前一行的最后一列

if(n[i][j] == 0){

//不是第一列,则倒退一列

if(j 0){

j-=2;

continue;

}else{//是第一列,则倒退到上一行的最后一列

i--;

j = 8;

continue;

}

}

//填充成功

if(isCorret(i,j)){

//初始化time,为下一次填充做准备

time = 0;

}else{ //继续填充

//次数增加1

time++;

//继续填充当前格

j--;

}

}

}

//输出结果

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

for(int j = 0;j 9;j++){

System.out.print(n[i][j] + " ");

}

System.out.println();

}

}

/**

* 是否满足行、列和3X3区域不重复的要求

* @param row 行号

* @param col 列号

* @return true代表符合要求

*/

public static boolean isCorret(int row,int col){

return (checkRow(row) checkLine(col) checkNine(row,col));

}

/**

* 检查行是否符合要求

* @param row 检查的行号

* @return true代表符合要求

*/

public static boolean checkRow(int row){

for(int j = 0;j 8;j++){

if(n[row][j] == 0){

continue;

}

for(int k =j + 1;k 9;k++){

if(n[row][j] == n[row][k]){

return false;

}

}

}

return true;

}

/**

* 检查列是否符合要求

* @param col 检查的列号

* @return true代表符合要求

*/

public static boolean checkLine(int col){

for(int j = 0;j 8;j++){

if(n[j][col] == 0){

continue;

}

for(int k =j + 1;k 9;k++){

if(n[j][col] == n[k][col]){

return false;

}

}

}

return true;

}

/**

* 检查3X3区域是否符合要求

* @param row 检查的行号

* @param col 检查的列号

* @return true代表符合要求

*/

public static boolean checkNine(int row,int col){

//获得左上角的坐标

int j = row / 3 * 3;

int k = col /3 * 3;

//循环比较

for(int i = 0;i 8;i++){

if(n[j + i/3][k + i % 3] == 0){

continue;

}

for(int m = i+ 1;m 9;m++){

if(n[j + i/3][k + i % 3] == n[j + m/3][k + m % 3]){

return false;

}

}

}

return true;

}

/**

* 产生1-9之间的随机数字

* 规则:生成的随机数字放置在数组8-time下标的位置,随着time的增加,已经尝试过的数字将不会在取到

* 说明:即第一次次是从所有数字中随机,第二次时从前八个数字中随机,依次类推,

* 这样既保证随机,也不会再重复取已经不符合要求的数字,提高程序的效率

* 这个规则是本算法的核心

* @param time 填充的次数,0代表第一次填充

* @return

*/

public static int generateNum(int time){

//第一次尝试时,初始化随机数字源数组

if(time == 0){

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

num[i] = i + 1;

}

}

//第10次填充,表明该位置已经卡住,则返回0,由主程序处理退回

if(time == 9){

return 0;

}

//不是第一次填充

//生成随机数字,该数字是数组的下标,取数组num中该下标对应的数字为随机数字

int ranNum = (int)(Math.random() * (9 - time));

//把数字放置在数组倒数第time个位置,

int temp = num[8 - time];

num[8 - time] = num[ranNum];

num[ranNum] = temp;

//返回数字

return num[8 - time];

}

}

求数独源码

没试过

#include stdio.h

#include stdlib.h

int sudoku[81] ; // 数独题目阵列

int tempNum[81] ; // 上一次填数位置

int tempSp= 0 ; // 上一次填数位置指标

int startH[81] ; // 列位置的起点

int startV[81] ; // 行位置的起点

int startB[81] ; // 九宫格位置的起点

int addH[9] ; // 列位置的加值

int addV[9] ; // 行位置的加值

int addB[9] ; // 九宫格位置的加值

int main(int argc, char *argv[]) {

int j ;

if(argc1) for(j=0; j81; j++) sudoku[j]= argv[1][j]-'0' ;

else exit(0) ;

printf( "----------\n");

printSudoku(sudoku) ;

init() ; // 参数设定

tryAns() ; // 测试求解

printf( "----------\n");

printSudoku(sudoku) ;

printf( "----------\n");

}

int init() {

// 参数设定(设定这些参数之后,无论检查行、列、九宫格都方便多了)

int i ;

for(i=0; i81; i++) {

startH[i]= i/9* 9 ; // 列位置的起点

startV[i]= i% 9 ; // 行位置的起点

startB[i]= ((i/9)/3)*27+ ((i%9)/3)*3 ; // 九宫格位置的起点

}

for(i=0; i9; i++) {

addH[i]= i ; // 列位置的加值

addV[i]= i*9 ; // 行位置的加值

addB[i]= (i/3)*9+ (i%3) ; // 九宫格位置的加值

}

}

int printSudoku(int *prn) {

// 印出数独题目(阵列内容)

int i ;

for(i=0; i81; i++) {

printf( "%2d", prn[i]);

if(i%9==8) printf("\n");

}

}

int tryAns() {

// 测试求解

int sp=getNextBlank(-1) ; // 取得第一个空白的位置开始填入数字

do {

sudoku[sp]++ ; // 将本位置数字加 1

if(sudoku[sp]9) { // 如果本位置的数字已大於 9 时则回到上一个位置继续测试

sudoku[sp]= 0 ;

sp= pop() ;

} else {

if(check(sp)==0) { // 如果同行、列、九宫格都没有相同的数字,则到下一个空白处继续

push(sp) ; // 当然,如果发现有相同的数字时,就需把原位置的数字加 1(所以本处什麼都不做)

sp= getNextBlank(sp) ;

}

}

} while(sp=0 sp81) ;

}

int getNextBlank(int sp) {

// 取得下一个空白的位置

do {

sp++ ;

} while(sp81 sudoku[sp]0) ;

return(sp) ;

}

int check(int sp) {

// 检查同行、列、九宫格有没有相同的数字,若有传回 1

int fg= 0 ;

if(!fg) fg= check1(sp, startH[sp], addH) ; // 检查同列有没有相同的数字

if(!fg) fg= check1(sp, startV[sp], addV) ; // 检查同行有没有相同的数字

if(!fg) fg= check1(sp, startB[sp], addB) ; // 检查同九宫格有没有相同的数字

return(fg) ;

}

int check1(int sp, int start, int *addnum) {

// 检查指定的行、列、九宫格有没有相同的数字,若有传回 1

int fg= 0, i, sp1 ;

for(i=0; i9; i++) {

sp1= start+ addnum[i] ;

if(sp!=sp1 sudoku[sp]==sudoku[sp1]) fg++ ;

}

return(fg) ;

}

int push(int sp) {

// 将指定的位置放入堆叠中

tempNum[tempSp++]= sp ;

}

int pop() {

// 取出堆叠中的上一个位置

if(tempSp0) return(-1) ;

else return(tempNum[--tempSp]) ;

}

参考资料:;page=1 算法如下,先构造一个9*9的结构体数组,表示棋盘数据0表示空白未知,结构体中每个元素

包含一个1-9的数组作为备选数字.

构建好一个棋盘之后依次对每个空白位置进行备选数字中进行删除.当前已经填写的数字就全部删除

如果只剩下一个备选数字就将该备选数字填写到棋盘数据中.该算法在AI这个函数中实现.

当无法用AI算法推出结果的时候就进行回朔法,见找到有两个备选数字的元素,选取其中一个,

继续往下填写,直到全部填写上去(结束),或者无法继续填写(某个空白位置没有备选元素).

如果无法继续填写下去就表示最初选择的那个数据是错误的,直接填写另外一个数据到棋盘上.

该算法在AdvanceAI中体现出来

如此下去就能够填写出棋盘中的所有元素.

#include cstdio

#include vector

#include algorithm

enum{SIZE=81};

unsigned int Data[SIZE]={//未解棋盘数据

0 , 9 , 0 , 0 , 6 , 0 , 5 , 4 , 8 ,

4 , 0 , 3 , 0 , 8 , 0 , 9 , 0 , 0 ,

8 , 6 , 5 , 4 , 7 , 9 , 1 , 2 , 3 ,

0 , 5 , 6 , 3 , 9 , 0 , 4 , 0 , 1 ,

1 , 4 , 0 , 0 , 5 , 0 , 2 , 0 , 0 ,

0 , 0 , 0 , 0 , 4 , 1 , 0 , 0 , 0 ,

0 , 0 , 0 , 8 , 2 , 0 , 6 , 1 , 0 ,

0 , 0 , 0 , 0 , 3 , 0 , 0 , 0 , 4 ,

5 , 8 , 0 , 9 , 1 , 0 , 0 , 0 , 0 };

const int temp[9] = { 1 , 2 , 3, 4, 5, 6, 7, 8, 9};

struct Item

{

int data;

std::vectorint other;

Item():data(0),other(temp,temp+9){}

inline bool operator==(int x)

{

return x==data?true:false;

}

inline Item operator=(const Item src)

{

data = src.data ;

other = src.other;

return (*this);

};

inline Item operator=(int x){

data = x ;

std::copy(temp,temp+sizeof(temp)/sizeof(temp[0]) , other.begin());

return (*this);

};

void test(size_t x ){

if( other.size() == 2 )

data = other[x];

}

inline operator int(){return data;}

};

struct GroupInfo{

const int Group1,Group2,Group3;

GroupInfo(int g1,int g2,int g3):Group1(g1),Group2(g2),Group3(g3){}

inline bool operator==(GroupInfo src){

return ((Group1|Group2|Group3)(src.Group1|src.Group2|src.Group3))?true:false;

}

};

GroupInfo Group[SIZE]={

GroupInfo( 11 , 110 , 119) ,GroupInfo( 11 , 111 , 119) ,GroupInfo( 11 , 112 , 119) ,GroupInfo( 11 , 113 , 120) ,GroupInfo( 11 , 114 , 120) ,GroupInfo( 11 , 115 , 120) ,GroupInfo( 11 , 116 , 121) ,GroupInfo( 11 , 117 , 121) ,GroupInfo( 11 , 118 , 121) ,

GroupInfo( 12 , 110 , 119) ,GroupInfo( 12 , 111 , 119) ,GroupInfo( 12 , 112 , 119) ,GroupInfo( 12 , 113 , 120) ,GroupInfo( 12 , 114 , 120) ,GroupInfo( 12 , 115 , 120) ,GroupInfo( 12 , 116 , 121) ,GroupInfo( 12 , 117 , 121) ,GroupInfo( 12 , 118 , 121) ,

GroupInfo( 13 , 110 , 119) ,GroupInfo( 13 , 111 , 119) ,GroupInfo( 13 , 112 , 119) ,GroupInfo( 13 , 113 , 120) ,GroupInfo( 13 , 114 , 120) ,GroupInfo( 13 , 115 , 120) ,GroupInfo( 13 , 116 , 121) ,GroupInfo( 13 , 117 , 121) ,GroupInfo( 13 , 118 , 121) ,

GroupInfo( 14 , 110 , 122) ,GroupInfo( 14 , 111 , 122) ,GroupInfo( 14 , 112 , 122) ,GroupInfo( 14 , 113 , 123) ,GroupInfo( 14 , 114 , 123) ,GroupInfo( 14 , 115 , 123) ,GroupInfo( 14 , 116 , 124) ,GroupInfo( 14 , 117 , 124) ,GroupInfo( 14 , 118 , 124) ,

GroupInfo( 15 , 110 , 122) ,GroupInfo( 15 , 111 , 122) ,GroupInfo( 15 , 112 , 122) ,GroupInfo( 15 , 113 , 123) ,GroupInfo( 15 , 114 , 123) ,GroupInfo( 15 , 115 , 123) ,GroupInfo( 15 , 116 , 124) ,GroupInfo( 15 , 117 , 124) ,GroupInfo( 15 , 118 , 124) ,

GroupInfo( 16 , 110 , 122) ,GroupInfo( 16 , 111 , 122) ,GroupInfo( 16 , 112 , 122) ,GroupInfo( 16 , 113 , 123) ,GroupInfo( 16 , 114 , 123) ,GroupInfo( 16 , 115 , 123) ,GroupInfo( 16 , 116 , 124) ,GroupInfo( 16 , 117 , 124) ,GroupInfo( 16 , 118 , 124) ,

GroupInfo( 17 , 110 , 125) ,GroupInfo( 17 , 111 , 125) ,GroupInfo( 17 , 112 , 125) ,GroupInfo( 17 , 113 , 126) ,GroupInfo( 17 , 114 , 126) ,GroupInfo( 17 , 115 , 126) ,GroupInfo( 17 , 116 , 127) ,GroupInfo( 17 , 117 , 127) ,GroupInfo( 17 , 118 , 127) ,

GroupInfo( 18 , 110 , 125) ,GroupInfo( 18 , 111 , 125) ,GroupInfo( 18 , 112 , 125) ,GroupInfo( 18 , 113 , 126) ,GroupInfo( 18 , 114 , 126) ,GroupInfo( 18 , 115 , 126) ,GroupInfo( 18 , 116 , 127) ,GroupInfo( 18 , 117 , 127) ,GroupInfo( 18 , 118 , 127) ,

GroupInfo( 19 , 110 , 125) ,GroupInfo( 19 , 111 , 125) ,GroupInfo( 19 , 112 , 125) ,GroupInfo( 19 , 113 , 126) ,GroupInfo( 19 , 114 , 126) ,GroupInfo( 19 , 115 , 126) ,GroupInfo( 19 , 116 , 127) ,GroupInfo( 19 , 117 , 127) ,GroupInfo( 19 , 118 , 127)

};

bool AI(std::vectorItem game)

{

bool bMoveflag = false;

for(size_t x = 0 ; x game.size() ; ++x ){

if( 0 != game[x].data ){//依次检查每个位置

game[x].other.resize(0);

continue;

}

//当前位置没有数字

std::vectorint vTemp;

for(int i = 0 ; i 81 ; ++i )

if( Group[x]==Group[i] )

vTemp.push_back ( game[i].data );

;

vTemp.erase( std::remove(vTemp.begin(),vTemp.end() , 0 ) , vTemp.end() );

//移除同组已经出现的数字

for(std::vectorint::iterator Iter = vTemp.begin() ; Iter !=vTemp.end() ; ++ Iter )

std::replace(game[x].other.begin() , game[x].other.end() , (*Iter) , 0 );

game[x].other.erase( std::remove(game[x].other.begin(),game[x].other.end() , 0 ) ,game[x].other.end() );

if( ( 1 == game[x].other.size())( 0 != game[x].other[0] ) ){

game[x].data = game[x].other[0];

bMoveflag = true;

}

}

return bMoveflag;

}

struct OtherIs2Opt{

bool operator()(Item item)

{return ( item.other.size()==2)?true:false;}

};

struct testBackOpt

{

bool bBack;

testBackOpt():bBack(false){}

void operator()(Item item)

{

if( ( item.data==0)(item.other.size()==0) )

bBack = true;

}

};

bool AdvanceAI(std::vectorItem game)

{

std::vectorItem Back = game;

std::vectorItem::iterator iItem = std::find_if( Back.begin() , Back.end() , OtherIs2Opt() );

if( iItem != Back.end() ){

for(size_t i = 0 ; i (*iItem).other.size() ; ++i ){

(*iItem).test( i );

for( ; AI( Back ) ;);

if( std::for_each( Back.begin() , Back.end() , testBackOpt() ).bBack ){//是否结束回滚

Back = game;

iItem = std::find_if( Back.begin() , Back.end() , OtherIs2Opt() );

continue;

}

if( std::count( Back.begin() , Back.end() , 0 ) ){//判断是否结束

if( AdvanceAI( Back ) ){//没有结束,继续下一步递归

game = Back ;

return true;

}

Back = game;

iItem = std::find_if( Back.begin() , Back.end() , OtherIs2Opt() );

continue;

}else{//back为结果

game = Back ;

return true;

}

}

}

return false;

}

int main(int argc, char* argv[])

{//初始化棋盘

std::vectorItem game(SIZE);

std::copy(Data,Data+SIZE , game.begin() );

for( ; AI( game ) ;);

if( std::count( game.begin() , game.end() , 0 ) ){

if( !AdvanceAI( game ) )

printf("没解出来 ");

}

for(int x = 0 ; x 81 ; ++x ){

printf(" %d",game[x].data );

if( 0 == (x +1)% 9 )

printf(" ");

}

return 0;算法如下,先构造一个9*9的结构体数组,表示棋盘数据0表示空白未知,结构体中每个元素

包含一个1-9的数组作为备选数字.

构建好一个棋盘之后依次对每个空白位置进行备选数字中进行删除.当前已经填写的数字就全部删除

如果只剩下一个备选数字就将该备选数字填写到棋盘数据中.该算法在AI这个函数中实现.

当无法用AI算法推出结果的时候就进行回朔法,见找到有两个备选数字的元素,选取其中一个,

继续往下填写,直到全部填写上去(结束),或者无法继续填写(某个空白位置没有备选元素).

如果无法继续填写下去就表示最初选择的那个数据是错误的,直接填写另外一个数据到棋盘上.

该算法在AdvanceAI中体现出来

如此下去就能够填写出棋盘中的所有元素.

#include cstdio

#include vector

#include algorithm

enum{SIZE=81};

unsigned int Data[SIZE]={//未解棋盘数据

0 , 9 , 0 , 0 , 6 , 0 , 5 , 4 , 8 ,

4 , 0 , 3 , 0 , 8 , 0 , 9 , 0 , 0 ,

8 , 6 , 5 , 4 , 7 , 9 , 1 , 2 , 3 ,

0 , 5 , 6 , 3 , 9 , 0 , 4 , 0 , 1 ,

1 , 4 , 0 , 0 , 5 , 0 , 2 , 0 , 0 ,

0 , 0 , 0 , 0 , 4 , 1 , 0 , 0 , 0 ,

0 , 0 , 0 , 8 , 2 , 0 , 6 , 1 , 0 ,

0 , 0 , 0 , 0 , 3 , 0 , 0 , 0 , 4 ,

5 , 8 , 0 , 9 , 1 , 0 , 0 , 0 , 0 };

const int temp[9] = { 1 , 2 , 3, 4, 5, 6, 7, 8, 9};

struct Item

{

int data;

std::vectorint other;

Item():data(0),other(temp,temp+9){}

inline bool operator==(int x)

{

return x==data?true:false;

}

inline Item operator=(const Item src)

{

data = src.data ;

other = src.other;

return (*this);

};

inline Item operator=(int x){

data = x ;

std::copy(temp,temp+sizeof(temp)/sizeof(temp[0]) , other.begin());

return (*this);

};

void test(size_t x ){

if( other.size() == 2 )

data = other[x];

}

inline operator int(){return data;}

};

struct GroupInfo{

const int Group1,Group2,Group3;

GroupInfo(int g1,int g2,int g3):Group1(g1),Group2(g2),Group3(g3){}

inline bool operator==(GroupInfo src){

return ((Group1|Group2|Group3)(src.Group1|src.Group2|src.Group3))?true:false;

}

};

GroupInfo Group[SIZE]={

GroupInfo( 11 , 110 , 119) ,GroupInfo( 11 , 111 , 119) ,GroupInfo( 11 , 112 , 119) ,GroupInfo( 11 , 113 , 120) ,GroupInfo( 11 , 114 , 120) ,GroupInfo( 11 , 115 , 120) ,GroupInfo( 11 , 116 , 121) ,GroupInfo( 11 , 117 , 121) ,GroupInfo( 11 , 118 , 121) ,

GroupInfo( 12 , 110 , 119) ,GroupInfo( 12 , 111 , 119) ,GroupInfo( 12 , 112 , 119) ,GroupInfo( 12 , 113 , 120) ,GroupInfo( 12 , 114 , 120) ,GroupInfo( 12 , 115 , 120) ,GroupInfo( 12 , 116 , 121) ,GroupInfo( 12 , 117 , 121) ,GroupInfo( 12 , 118 , 121) ,

GroupInfo( 13 , 110 , 119) ,GroupInfo( 13 , 111 , 119) ,GroupInfo( 13 , 112 , 119) ,GroupInfo( 13 , 113 , 120) ,GroupInfo( 13 , 114 , 120) ,GroupInfo( 13 , 115 , 120) ,GroupInfo( 13 , 116 , 121) ,GroupInfo( 13 , 117 , 121) ,GroupInfo( 13 , 118 , 121) ,

GroupInfo( 14 , 110 , 122) ,GroupInfo( 14 , 111 , 122) ,GroupInfo( 14 , 112 , 122) ,GroupInfo( 14 , 113 , 123) ,GroupInfo( 14 , 114 , 123) ,GroupInfo( 14 , 115 , 123) ,GroupInfo( 14 , 116 , 124) ,GroupInfo( 14 , 117 , 124) ,GroupInfo( 14 , 118 , 124) ,

GroupInfo( 15 , 110 , 122) ,GroupInfo( 15 , 111 , 122) ,GroupInfo( 15 , 112 , 122) ,GroupInfo( 15 , 113 , 123) ,GroupInfo( 15 , 114 , 123) ,GroupInfo( 15 , 115 , 123) ,GroupInfo( 15 , 116 , 124) ,GroupInfo( 15 , 117 , 124) ,GroupInfo( 15 , 118 , 124) ,

GroupInfo( 16 , 110 , 122) ,GroupInfo( 16 , 111 , 122) ,GroupInfo( 16 , 112 , 122) ,GroupInfo( 16 , 113 , 123) ,GroupInfo( 16 , 114 , 123) ,GroupInfo( 16 , 115 , 123) ,GroupInfo( 16 , 116 , 124) ,GroupInfo( 16 , 117 , 124) ,GroupInfo( 16 , 118 , 124) ,

GroupInfo( 17 , 110 , 125) ,GroupInfo( 17 , 111 , 125) ,GroupInfo( 17 , 112 , 125) ,GroupInfo( 17 , 113 , 126) ,GroupInfo( 17 , 114 , 126) ,GroupInfo( 17 , 115 , 126) ,GroupInfo( 17 , 116 , 127) ,GroupInfo( 17 , 117 , 127) ,GroupInfo( 17 , 118 , 127) ,

GroupInfo( 18 , 110 , 125) ,GroupInfo( 18 , 111 , 125) ,GroupInfo( 18 , 112 , 125) ,GroupInfo( 18 , 113 , 126) ,GroupInfo( 18 , 114 , 126) ,GroupInfo( 18 , 115 , 126) ,GroupInfo( 18 , 116 , 127) ,GroupInfo( 18 , 117 , 127) ,GroupInfo( 18 , 118 , 127) ,

GroupInfo( 19 , 110 , 125) ,GroupInfo( 19 , 111 , 125) ,GroupInfo( 19 , 112 , 125) ,GroupInfo( 19 , 113 , 126) ,GroupInfo( 19 , 114 , 126) ,GroupInfo( 19 , 115 , 126) ,GroupInfo( 19 , 116 , 127) ,GroupInfo( 19 , 117 , 127) ,GroupInfo( 19 , 118 , 127)

};

bool AI(std::vectorItem game)

{

bool bMoveflag = false;

for(size_t x = 0 ; x game.size() ; ++x ){

if( 0 != game[x].data ){//依次检查每个位置

game[x].other.resize(0);

continue;

}

//当前位置没有数字

std::vectorint vTemp;

for(int i = 0 ; i 81 ; ++i )

if( Group[x]==Group[i] )

vTemp.push_back ( game[i].data );

;

vTemp.erase( std::remove(vTemp.begin(),vTemp.end() , 0 ) , vTemp.end() );

//移除同组已经出现的数字

for(std::vectorint::iterator Iter = vTemp.begin() ; Iter !=vTemp.end() ; ++ Iter )

std::replace(game[x].other.begin() , game[x].other.end() , (*Iter) , 0 );

game[x].other.erase( std::remove(game[x].other.begin(),game[x].other.end() , 0 ) ,game[x].other.end() );

if( ( 1 == game[x].other.size())( 0 != game[x].other[0] ) ){

game[x].data = game[x].other[0];

bMoveflag = true;

}

}

return bMoveflag;

}

struct OtherIs2Opt{

bool operator()(Item item)

{return ( item.other.size()==2)?true:false;}

};

struct testBackOpt

{

bool bBack;

testBackOpt():bBack(false){}

void operator()(Item item)

{

if( ( item.data==0)(item.other.size()==0) )

bBack = true;

}

};

bool AdvanceAI(std::vectorItem game)

{

std::vectorItem Back = game;

std::vectorItem::iterator iItem = std::find_if( Back.begin() , Back.end() , OtherIs2Opt() );

if( iItem != Back.end() ){

for(size_t i = 0 ; i (*iItem).other.size() ; ++i ){

(*iItem).test( i );

for( ; AI( Back ) ;);

if( std::for_each( Back.begin() , Back.end() , testBackOpt() ).bBack ){//是否结束回滚

Back = game;

iItem = std::find_if( Back.begin() , Back.end() , OtherIs2Opt() );

continue;

}

if( std::count( Back.begin() , Back.end() , 0 ) ){//判断是否结束

if( AdvanceAI( Back ) ){//没有结束,继续下一步递归

game = Back ;

return true;

}

Back = game;

iItem = std::find_if( Back.begin() , Back.end() , OtherIs2Opt() );

continue;

}else{//back为结果

game = Back ;

return true;

}

}

}

return false;

}

int main(int argc, char* argv[])

{//初始化棋盘

std::vectorItem game(SIZE);

std::copy(Data,Data+SIZE , game.begin() );

for( ; AI( game ) ;);

if( std::count( game.begin() , game.end() , 0 ) ){

if( !AdvanceAI( game ) )

printf("没解出来 ");

}

for(int x = 0 ; x 81 ; ++x ){

printf(" %d",game[x].data );

if( 0 == (x +1)% 9 )

printf(" ");

}

return 0;


新闻名称:java数独代码csdn java数独课程设计
分享路径:http://cdkjz.cn/article/ddgephs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220