我在学年的最后一个项目(我作为CS学生的第一年)的代码中找不到错误.在执行骑士巡回赛问题时,我一直坚持递归.这是有问题的文件:

https://github.com/sheagunther/tsp161knightstour/blob/master/KnightsTourRecursiveSearch.java

具体来说,我的问题是这部分代码(从第265行开始):

else{

for(int i = 0; i < numberOfPossibleNextMoves; i++){

Cell nextCellToMoveTo = candidateNextMoves.get(i);

int currentRowStorage = currentRow;

int currentColumnStorage = currentColumn;

currentRow = nextCellToMoveTo.row;

currentColumn = nextCellToMoveTo.column;

listOfMoves.add(nextCellToMoveTo);

chessBoard[currentRow][currentColumn] = 1;

currentMoveNumber++;

boolean tourFound = findTour();

if(tourFound){

return true;

}

else{ // Undo the last move just made

backtrackCount++;

chessBoard[currentRow][currentColumn] = -1;

currentRow = currentRowStorage;

currentColumn = currentColumnStorage;

listOfMoves.remove(nextCellToMoveTo);

currentMoveNumber--;

}

}

return false;

这是findTour()的结尾.这是程序的一部分,用于测试当前正方形(也称为单元格)的所有可能移动,如果可以从新移动到正方形完成巡回,则返回true.如果游览不能从广场完成,它会进入其他地方{并撤消移动.这就是我认为的问题所在.

现在,如上面的代码设置,程序陷入无限递归循环.

记下else {声明的这一部分:

chessBoard[currentRow][currentColumn] = -1;

currentRow = currentRowStorage;

currentColumn = currentColumnStorage;

这部分代码将chessBoard中的方块更改为-1,这意味着它是未访问的(1 =已访问).如上所述,新移动的currentRow和currentColumn用于将方块设置为未访问的.然后使用currentRowStorage和currentColumnStorage将这些值重置为先前的跳转值.

如果我将代码更改为

currentRow = currentRowStorage;

currentColumn = currentColumnStorage;

chessBoard[currentRow][currentColumn] = -1;

它成功地找到了一个不正确的巡回演出,其中最后1/3左右的动作只是在几个方格之间来回跳跃.这是预期的,因为它没有正确处理重置过程.

我怀疑我的问题是由于我在宣布变量的地方.这是我的第一个复杂的递归问题,我不确定我是否正确处理currentRow / Column和currentRow / ColumnStorage之间的切换.我应该在当地或多或少地宣布它们吗?

以下是要求的相关部分:

If the tour is not completed, then findTour determines the (possibly

empty) list of vacant cells that are reachable from the knight’s

current cell, and stores this list in a locally declared list

variable, candidateNextMoves. It is critical that this list variable

be declared local to the method. If this list is empty, then there is

no way to extend the current partial tour, so findTour should return

false. If the list is not empty, then findTour tries extending the

tour by each move on the list as follows. It iterates over the list,

and for each cell of the list it makes the next move to that cell,

updating all of L (the list of moves in the tour), B (the 2D array of

the state of the board (visited, not visted)), currRow, and currCol to

reflect this move. It then recursively calls itself, assigning the

result of the call to a locally declared boolean variable, which you

might name “success”. If success is assigned true, findTour returns

true. If success is false, findTour undoes the move it just made, or

“backtracks”, and tries the next move of candidateNextMoves. You will

maintain a static int variable, backtrackCount, which is initialized

to 0, and incremented for each undo of a move.

一个注意事项 – 我调用了我的布尔值’tourFound’而不是’success’.

骑士 java_在递归骑士之旅中正确声明变量(Java作业)相关推荐

  1. python变量需要声明吗_python中可以声明变量类型吗

    变量(variable)是Python语言中一个非常重要的概念.变量的主要作用就是为Python程序中的某个值起一个名字.类似于"张三"."李四"." ...

  2. python是什么语言、即变量不需要显示声明数据类型-python中可以声明变量类型吗...

    变量(variable)是Python语言中一个非常重要的概念.变量的主要作用就是为Python程序中的某个值起一个名字.类似于"张三"."李四"." ...

  3. python变量类型声明_python中可以声明变量类型吗

    变量(variable)是Python语言中一个非常重要的概念.变量的主要作用就是为Python程序中的某个值起一个名字.类似于"张三"."李四"." ...

  4. shell中强制声明变量

    可以在shell中强制必须声明变量,shopt 指令,如下面的指令mustpreset.sh 运行结果为50 #!/bin/sh declare Informix=50 echo $Informix ...

  5. Java 中把声明变量的语句如果写在循环体内,每次执行时栈内存中的变量和数据是如何变化的?

    问题一:如下面的代码示例 1,JVM 是不是会反复回收旧的变量 a 再重新创建新的变量 a 呢?还是旧的变量 a 一直保留在栈内,只是反复赋值 0 而已呢? 代码示例 1: while (true) ...

  6. java方法中的循环里的变量_Java中循环声明变量方法

    Java循环声明变量 之前想这样做,但是网上一直搜索不到,下面是我的方式 项目中 // 得到需要查询外表的数量,然后分别创建缓存,插入数据多的时候如果编码在缓存里面,就不需要再去查询数据库了.key: ...

  7. 下列叙述中正确的是 java语言_1. 下列关于JAVA语言特点的叙述中,错误的是[   ] A、Java是面向过程的编程语言...

    1. 下列关于JAVA语言特点的叙述中,错误的是[  ]A.Java是面向过程的编程语言   B.Java支持分布式计算C.Java是跨平台的编程语言   &... 1. 下列关于JAVA语言 ...

  8. protobuf windows java_如何通过Eclipse在Windows中构建Protobuf for Java

    我下载源Protobuf zip文件.然后我打开我的Classic Eclipse并选择File-> Import-> Existing Maven Projects. 我选择根文件夹为/ ...

  9. Vue超好玩的新特性:在CSS中引入JS变量

    授权转载自: 手撕红黑树 https://juejin.im/post/6856668819344392206 幻想 以前做项目的时候经常会这么想: <template><h1> ...

最新文章

  1. 每日一问一周汇总:第1期
  2. 第七篇:使用 CUDA 进行计算优化的两种思路
  3. 大白话解析模拟退火算法、遗传算法入门
  4. 支持向量机SVM(四)
  5. 查看SQL SERVER数据库的连接数
  6. phpcms v9 index.php,【转】phpcms v9中tags列表页url用拼音作为路径的方法
  7. 字段中存在空值的问题测试
  8. java导出mysql数据库失败_利用Java进行MySql数据库的导入和导出
  9. 探究Java File类中list()、listFiles()的使用及区别,认识和使用匿名内部类
  10. 少拿游戏来骗我,虚幻引擎5上的《黑客帝国》全新体验,画面帅到爆
  11. Hadoop YARN配置参数剖析—RM与NM相关参数
  12. x86从实模式到保护模式 pdf_【自制操作系统04】从实模式到保护模式
  13. 基于单片机的电子密码锁的实现
  14. Code blocks调试教程
  15. 复试c语言笔试题,考研计算机复试(C语言复试笔试题)(精华题选)
  16. Modern Standby 调试的准备
  17. WindowsLive™ OneCare™初体验
  18. 1+X 网络系统 建设与运维(中级)实验
  19. Geoserver发布切片地图组完整教程
  20. OutLook Express关联QQ邮箱

热门文章

  1. 计算天数java_Java,计算两个日期之间的天数
  2. [云炬创业基础笔记]第六章商业模式测试18
  3. 科大星云诗社动态20201222
  4. 小米8 twrp recovery_小米手机、红米手机通用刷机教程
  5. 关于动态生成输入界面的一些想法及实践
  6. sqlserver 重置自增Id
  7. 23种设计模式C++源码与UML实现--命令模式
  8. Java对象容器——集合Set
  9. 如何提升网站在移动端的打开速度(转)
  10. 一个历史遗留问题,引发的linux内存管理的‘血案’