且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过M×N个网格之旅号码?

更新时间:2023-02-26 08:05:11

既然你是新的回溯,这可能会给你一个想法如何,你可以解决这个问题:

Since you're new to backtracking, this might give you an idea how you could solve this:

您需要一些数据结构来重新present细胞的状态,对电网(访问/未被访问)。

You need some data structure to represent the state of the cells on the grid (visited/not visited).

您的算法:

step(posx, posy, steps_left)
    if it is not a valid position, or already visited
        return
    if it's the last step and you are at the target cell
        you've found a solution, increment counter
        return
    mark cell as visited             
    for each possible direction:
       step(posx_next, posy_next, steps_left-1)
    mark cell as not visited

和运行

step(0, 0, sizex*sizey)

回溯的基本构建块是:评估当前状态,标记,递归步骤和不标记

The basic building blocks of backtracking are: evaluation of the current state, marking, the recursive step and the unmarking.

这将正常工作的小板。真正的乐趣开始,你必须切枝对不属于可解树大板。(如:有没有到过的细胞的不可达区域)

This will work fine for small boards. The real fun starts with larger boards where you have to cut branches on the tree which aren't solvable (eg: there's an unreachable area of not visited cells).