骑士周游
来源:互联网 发布:架子鼓调音软件 编辑:程序博客网 时间:2024/04/28 21:02
pascal写成的骑士周游,仍有运行时间长(约为10秒左右)的问题,求赐教,以作进一步优化。
uses crt;constN=8;typechessarray=array [1..N,1..N] of integer;steparray=array[1..8,1..2] of integer;varchess:chessarray;step:steparray;procedure init(var _chess:chessarray;var _step:steparray);var i,j:integer;begin for i:=1 to N do begin for j:=1 to N do begin _chess[i][j]:=0; end; end; _step[1][1]:=-2;_step[1][2]:=-1; _step[2][1]:=-1;_step[2][2]:=-2; _step[3][1]:=1;_step[3][2]:=-2; _step[4][1]:=2;_step[4][2]:=-1; _step[5][1]:=2;_step[5][2]:=1; _step[6][1]:=1;_step[6][2]:=2; _step[7][1]:=-1;_step[7][2]:=2; _step[8][1]:=-2;_step[8][2]:=1;end;procedure print(_chess:chessarray);var i,j:integer;begin randomize; for i:=1 to N do begin for j:=1 to N do begin //1-->14 textcolor(random(14)+1); write(_chess[i][j]:5); end; writeln; end; writeln;end;procedure test(_step:steparray);var i:integer;begin for i:=1 to 8 do begin writeln(_step[i][1]:5,_step[i][2]:5); end;end;function canJump(x,y:integer):boolean;begin if (x>=1) and (x<=N) and (y>=1) and (y<=N) and (chess[x][y]=0) then begin canJump:=true; exit; end; canJump:=false;end;procedure backtrack(t,x,y:integer);var i:integer;begin if t>=N*N then begin print(chess); readln; end else begin for i:=1 to 8 do begin if canJump(x+step[i][1],y+step[i][2])=true then begin x:=x+step[i][1]; y:=y+step[i][2]; chess[x][y]:=t+1; backtrack(t+1,x,y); chess[x][y]:=0; x:=x-step[i][1]; y:=y-step[i][2]; end; end; end;end;beginwriteln('waiting...');writeln;init(chess,step);//print(chess);chess[1][1]:=1;backtrack(1,1,1);readln;end.
图为其中的2组解。
- 骑士周游
- 骑士周游问题
- 骑士周游 贪心
- 骑士周游 回溯法
- 骑士周游 探索访问
- 骑士周游,Knight Tour
- 骑士周游问题解决方案
- poj2488 骑士周游(深搜)
- 跳马问题(骑士周游问题)初探
- 跳马问题(骑士周游问题)初探
- 马踏棋盘算法(骑士周游问题)
- 马踏棋盘算法(骑士周游问题)
- 马踏棋盘算法(骑士周游问题)
- 马踏棋盘算法(骑士周游问题)
- C++ 马踏棋盘(骑士周游)
- 再探跳马问题(骑士周游问题)
- poj 2488 A Knight's Journey 骑士周游
- 数据结构学习之启发式搜索求解骑士周游问题
- ibatis接收多个参数
- Ubuntu 12.04 下 OpenStack Folsom 最精简安装
- ios::app与ios::ate打开方式的不同
- 免费的数据可视化分析工具推荐
- 与角色权限相关的视图
- 骑士周游
- 自考 计算机信息管理之信息资源管理 (课本总结)
- 区段减肥,去除垃圾区段,重建PE。
- 《高效程序员的45个习惯》读书笔记【思维导图】
- 图像方面的科研代码分类
- vb中的资源文件
- hdu 1102 Constructing Roads
- 学会搜索,享受乐趣
- 天勤OJ 题目1410: 比较字符串