Knight's Tour的递归问题 [英] Knight's Tour recursive problem

查看:51
本文介绍了Knight's Tour的递归问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力编写一个解决(或其他)骑士之旅的程序。我知道这是一个非常常见的问题,但我找不到一个我可以真正比较我的解决方案,并找出问题是什么。我知道问题是什么,但让我先发布我的代码:

Hi, I''m trying to write a program to solve (or whatever) the Knight''s Tour. I know this is a pretty common problem, but I haven''t found a solution that I can really compare mine to and isolate what the problem(s) is/are. I have an idea as to what the problem is, but let me first post my code:

展开 | 选择 | Wrap | 行号

推荐答案


我正在努力编写一个程序来解决(或其他)Knight's Tour。我知道这是一个非常常见的问题,但我找不到一个我可以真正比较我的解决方案,并找出问题是什么。我知道问题是什么,但让我先发布我的代码:


...

看来moveNum正在某处重置(它从1到20,然后又回到18,并且21也重复),但我无法弄清楚在哪里。任何有关这方面的帮助将不胜感激。
Hi, I''m trying to write a program to solve (or whatever) the Knight''s Tour. I know this is a pretty common problem, but I haven''t found a solution that I can really compare mine to and isolate what the problem(s) is/are. I have an idea as to what the problem is, but let me first post my code:

...
It appears moveNum is being reset somewhere (it goes from 1 to 20, then back to 18 again, and 21 is also repeated), but I can''t figure out where. Any help on this would be greatly appreciated.



有趣。在你提起之前我没有听说过这个问题。你的代码在风格上似乎相当不错,但我建议对不直接的部分提出意见,例如:

Interesting. I''ve not heard of this problem till you brought it up. Your code seems to be fairly good stylistically, but I would suggest comments on sections that are not straight forward such as:

< span class =codeLinkonclick =Blur(this,this.parentNode.parentNode,getChildren(this),true);>展开 | 选择 | Wrap | 行号


谢谢你的回复。我添加了一些评论。希望这会有所帮助:

Thanks for your reply. I''ve added some commenting. Hopefully this helps:

展开 | 选择 | Wrap | 行号


我很确定moveNum重置的原因是因为涉及递归。例如,如果我从20-23找到了一条好路。但是移动24是不可能的,我应该能够回溯并尝试从20开始的新路径 - 但是你的功能已经在棋盘中设置了21,22和23,所以这些空间将被视为占用。您可以将条件(board [xCoor] [yCoor] == 0)更改为board [xCoor] [yCoor]< moveNum。


这是一个非常有趣的问题......
I''m pretty sure the reason moveNum is reseting is because of the recursion involved. For instance, if I''ve found a good path from 20-23. but move 24 is impossible, I should be able to backtrack and try a new path from 20 - but your function has already set 21, 22, and 23 in the board, so those spaces will count as occupied. You might be able to change your condition (board[xCoor][yCoor] == 0) to board[xCoor][yCoor] < moveNum.

This is a very interesting problem...


这篇关于Knight's Tour的递归问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆