为了账号安全,请及时绑定邮箱和手机立即绑定

骑士巡游问题,为什么N的值大于7以后程序就无法运行了

骑士巡游问题,为什么N的值大于7以后程序就无法运行了

#include<iostream> #include<array> using namespace std; const int N = 8, STEPS = N*N-1; int border[N][N]; bool move(int currentRow, int currentColumn, int steps) { if (currentRow<0 || currentRow > N - 1 || currentColumn<0 || currentColumn > N - 1) return false; if (steps == -1) return true; if (border[currentRow][currentColumn] != 0) return false; steps--; border[currentRow][currentColumn] = STEPS - steps; if (move(currentRow + 1, currentColumn + 2, steps) == true)return true; if (move(currentRow + 1, currentColumn - 2, steps) == true)return true; if (move(currentRow - 1, currentColumn + 2, steps) == true)return true; if (move(currentRow - 1, currentColumn - 2, steps) == true)return true; if (move(currentRow + 2, currentColumn + 1, steps) == true)return true; if (move(currentRow + 2, currentColumn - 1, steps) == true)return true; if (move(currentRow - 2, currentColumn + 1, steps) == true)return true; if (move(currentRow - 2, currentColumn - 1, steps) == true)return true; border[currentRow][currentColumn] = 0; return false; } int main() { int CurrentRow = 0, CurrentColumn = 0; for(CurrentRow=0;CurrentRow < N;CurrentRow++) for (CurrentColumn = 0; CurrentColumn < N; CurrentColumn++) { cout << "start from " << CurrentRow << " " << CurrentColumn << "\n"; if (move(CurrentRow, CurrentColumn, STEPS) == true) { for (int i = 0; i < N; i++)  { for (int j = 0; j < N; j++)  { cout.fill('0');  cout.width(2); cout << border[i][j] << " "; } cout << endl; } } else  { cout << "No path found!" << endl; } } }编写程序求解骑士巡游问题:在n行n列的棋盘上(如n=5),假设一位骑士(按象棋中“马走日”的行走法)从初始坐标位置(x1,y1)出发,要遍访(巡游)棋盘中的每一个位置一次。请编一个程序,为骑士求解巡游“路线图”(或告诉骑士,从某位置出发时,无法遍访整个棋盘 — 问题无解)。
查看完整描述

1 回答

?
tanhouyusheng

TA贡献94条经验 获得超59个赞

算法需要优化吧,正常的递归回溯问题没有优化的话就是很慢的

查看完整回答
反对 回复 2017-07-28
  • 1 回答
  • 0 关注
  • 1877 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信