A-star用于查找图中startnode和endnode之间的最短路径.使用什么算法来解决某些问题,目标状态并不是专门知道的,而是我们只有目标状态的标准?
例如,可以用类似Astar的算法解决数独难题吗?我们不知道国家的样子(哪个数字在哪里),但我们知道数独的规则,一个获胜国家的标准.因此,我有一个startnode,只是endnode的标准,使用哪种算法?
所以我认为我认为java中的数独求解器的代码非常好,但我需要一些帮助.当我将它嵌入main方法时,它给了我一个堆栈溢出.问题是我的方法不知道如何扭转并修复它的错误.我需要一个布尔标志(一个与下面的代码中使用的标志不同,实际上最好工作)或其他东西让它知道什么时候应该转回来,什么时候它可以再次前进并继续解决游戏.谢谢你提供的所有帮助
public void play(int r, int c){//this method throws the StackOverflowError
if(needAtLoc(r,c).size()==9){
int num=1+generator.nextInt(9);
setCell(r,c,num,this);
if(c<8){
System.out.println(this);///////////////
play(r, c+1);
}
else{
play(r+1, 0);
}
}
else{
if(needAtLoc(r,c).size()==0){//no possible moves THIS IS THE PROBLEM LINE!!!
if(c>0){
play(r, c-1);//play last cell, in column to left
}
else{
if(r==0){
play(r,c);//first square, so must play again (can't go back)
}
else{
play(r-1, 8);/*first cell of row so must go to previous row and
the end column*/
}
}
}
else{//if there are possible moves …Run Code Online (Sandbox Code Playgroud) 我正在编写一个函数,可以为模拟项目生成随机的数独游戏; 这个函数将生成的单元格数作为argoument,然后生成要放入这些单元格的单元格和数字的索引.我在生成单元格索引时遇到问题,我不是编程方面的专家,我找不到一个好的例程来生成索引,并且检查不是两次或更多次相同的索引.功能是:
void gen_puzzle(int quanti)
{
if(quanti>81) exit(1);
indexes* ij=new indexes[quanti];
int f,g,k, controllo=1;
do
{
for(f=0; f<9; f++)
for(g=0; g<9; g++)
{
puzzle[f][g].num=0;//puts 0 in the sudoku puzzle
puzzle[f][g].p=0;
}
//////////////section to improve
out:
srand(int(time(0)+clock()));
for(k=0; k<quanti; k++)
ij[k].i=casuale()-1, ij[k].j=casuale()-1;//generates random indexes of sudoku cells where put random nubers
for(f=0; f<quanti; f++)
for(g=f+1; g<quanti; g++)
{
if(ij[f].i==ij[g].i && (ij[f].j==ij[g].j)) goto out;
}
////////////////////
for(k=0; k<quanti; k++)
puzzle[ij[k].i][ij[k].j] . num=casuale();//puts random numbers in cells
}
while(dataNotGood()); //till sudoku isn't good …Run Code Online (Sandbox Code Playgroud) 我这里有问题.我没有得到完美的输出.以下是我为编写Sudoku而编写的代码.即使我知道它的原因,因为它无法创建任何新的唯一编号,它打印默认值为0.我知道我错过了一些我无法想到的东西.任何人都可以建议一个解决方案吗?提前致谢.
public class FinalSudoku
{
int a[][]=new int[9][9];
public void initialize1()
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
a[i][j]=0;
}
}
}
protected boolean detectRow( int row, int num )
{
for( int col = 0; col < 9; col++ )
if( a[row][col] == num )
return false;
return true ;
}
protected boolean detectCol( int col, int num )
{
for( int row = 0; row < 9; row++ )
if( a[row][col] == num )
return false ;
return …Run Code Online (Sandbox Code Playgroud) 我正在使用此YouTube视频中描述的回溯算法.
现在,我应该能够获得所有可能的解决方案.我能用回溯算法做到这一点吗?如果不可能,我应该使用哪种(简单)算法?
我很难理解数独求解器的这一部分.我不知道扩展功能如何工作.
expand :: Matrix Choices -> [Matrix Choices]
expand m =
[rows1 ++ [row1 ++ [c] : row2] ++ rows2 | c <- cs]
where
(rows1,row:rows2) = break (any (not . single)) m
(row1,cs:row2) = break (not . single) row
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?简短的解释将非常有帮助.
我正在进行一个数独谜题,所以我将所有项目都放在一个数组中.
因此,每当我得到一个无效的数字,我必须递归调用一个函数,但我不能这样做.我无法理解id的问题.
我的方法是:
function checkValidity(x,y) {
var number = Math.floor((Math.random()) * 10);
var validnumber = true;
for (i = 0; i < 9; i++) {
if (sudokuValueArray[i][y] == number) {
validnumber = false;
}
}
for (i = 0; i < 9; i++) {
if (sudokuValueArray[x][i] == number) {
validnumber = false;
}
}
if(validnumber==true) {
return number;
}
else if(validnumber == false) {
return checkValidity(x, y);
}
}
Run Code Online (Sandbox Code Playgroud)
第二个功能是:
function CreateSudokeSample() {
for (var x = 0; x < …Run Code Online (Sandbox Code Playgroud) 我创建了一个简单的结构对象,它包含2个值 - 数字(特定数字)和计数(计数出现次数的计数器).
typedef struct matrixMissNumber {
int number;
int count = 0;
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个名为missingNumsObjects的列表来保存这些对象.missNums是一个单独保存整数的单独列表.
list<matrixMissNumber> missingNumsObjects;
for (auto m : missNums)
{
matrixMissNumber mn;
mn.number = m;
missingNumsObjects.push_back(mn);
}
Run Code Online (Sandbox Code Playgroud)
然后我有3个for循环通过并检查2个条件.如果满足这些条件,则将计数增加1.(我为测试目的添加了一个cout语句).我调试了程序,一切都很完美,直到循环结束.那时,missingNumsObjects中每个matrixMissNumber对象的count变量重置为0.我不确定是否存在操作不同内存地址的问题,或者列出了问题的指针.
for (auto m : missingNumsObjects)
{
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{ …Run Code Online (Sandbox Code Playgroud) 我正在制作自己的数独解算器版本。是否有任何开源测试用例可以用来测试我的算法的效率(我不只是想要随机测试用例。我想要标记为简单、中等、困难的测试用例)。
非常感谢!
我是C++的新手,只是编程了几天所以这可能看起来很愚蠢,但是你能发现为什么我的数组不能正常工作吗?这是我正在设计的解决Sudoku谜题的程序的开始,但我用来解决它的2D数组工作不正常.
#include <iostream>
#include <string>
using namespace std;
int main () {
char dash[9][9];
for (int array=0; array<9; array++) {
for (int array2=0; array2<9; array2++) {
dash[array][array2]=array2;
cout << dash[array][array2];
}
}
cout << dash[1][4] << endl; //This is temporary, but for some reason nothing outputs when I do this command.
cout << "?????????????????????????????????????" << endl;
for (int count=0; count<3; count++) {
for (int count2=0; count2<3; count2++) {
cout << "?_" << dash[count][count2*3] << "_|_" << dash[count] [count2*3+1] << "_|_" …Run Code Online (Sandbox Code Playgroud)