English 中文(简体)
php 易于使用背轨法的Dosoku解决器
原标题:php easy sudoku solver using backtracking

我最近想看到,我是否能够在营地内(首先)解决一个容易的骗局。 我知道,由于方案拟定的原因,营地实际上不是巧妙的,但我知道,营地是最好的,我在java和c的设计方面存在问题。 尽管如此,我看不出为什么不工作的原因。

首先,我不想问你,因为那里有一些已经解决的read子。 但我发现,这些解决办法对我来说过于复杂,难以理解(其他语言、复杂的障碍),也不符合我的目标。

我的问题是:一些人能否在我的目标的基础上向我提出 h? 我只想一个简单的“oku”解决器,而不作gues,而只是背后。

算法认为:

$cell;  // 1-81 - as parameter of the recursive function solve()
$value; // 1-9  - as parameter ...

class Sudoku {

function solve($cell = 1, $value = 1) {

    // skipping values

    if the current cell is fix:

        return solve(cell++, $value);

    // testing values (logic)

    if not:

        if the value is within the square (3x3) itself:

            return solve($cell, $value++);

        if the value is within the row:

            return solve($cell, $value++);

        if the value is within the col:

            return solve($cell, value++);

        if the value is bigger than 9:

            return solve($cell--, $value_prev);

        // all test passed, add the new value to list
        $this->values[$cell] = $value;

        if all fields are filled:
            return;

        if there are fields left:
            return solve($cell++, 1);
}
}

如果我设立空白处,它将在43个牢房之前正确填满。 脚印有致命错误: 致命错误: 允许的活度为134217728英寸(用于分配261904英特)。

这些数值如下:

1 2 3 | 4 5 6 | 7 8 9
4 5 6 | 7 8 9 | 1 2 3
7 8 9 | 1 2 3 | 4 5 6
2 1 4 | 3 6 5 | 8 9 7
3 6 5 | 2 1 4 | . . .

I guess there is an infinite loop or something that causes this crash. Maybe it is not solvable like this. I just wanted to know if I am doing right or what I forgot to check. I also tried this algorithm with fixed values from an easy-sudoku. It crashes too ... maybe there is to much backtracking.

Finally, I want to say that I m not against better solutions but I just want this to work. If you cannot give me an answer based on this you can have a look at the php file:

rel=“nofollow”>.php

Edit: sudoku2.php

提前感谢。

最佳回答

这是你的主要问题:

if the value is bigger than 9:
    return solve($cell--, $value_prev);

当你到这一点时(在什么地方工作,你不得不回头并改变以前的东西),你会像你那样变得更深,因为你的 st将随着错误的积累而变得太大。 您实际上需要回到原来的四分五级,并留在那里。

E.g. you might make solve return TRUE if it s complete, or FALSE if it ran out of options. Then any time you call solve recursively, if it returns TRUE, you return TRUE, and if it returns FALSE, you call it again with $value++.

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签