Knight's Tour骑士游历问题(C语言实现)
来源:互联网 发布:lte网络优化很垃圾 编辑:程序博客网 时间:2024/05/16 01:37
#include<stdio.h>int chessboard[8][8];bool FindPath(int startI,int startJ){//走法的序号int queueNumber;//开始位置标注为1chessboard[startI][startJ]=1;//规则走法const int posI[8]={1,1,2,2,-1,-1,-2,-2};const int posJ[8]={2,-2,1,-1,2,-2,1,-1};//当前位置可走方位的计数器int posCount=0;//考察可走方位最少的那个方位int minPosCount;//暂时记录各个方位的可走方位数int posCountTemp;//当前位置的可走方位的坐标int nexI[8]={0},nexJ[8]={0};//当前位置int nowI=startI,nowJ=startJ;//待考察的可走方位位置int nextI,nextJ;//各个方位的可走方位位置int nextnextI,nextnextJ;//寻找路径for (queueNumber=2;queueNumber<=64;++queueNumber){posCount=0;//考察新的位置,需归零for(int k=0;k<8;++k){nextI=nowI+posI[k];nextJ=nowJ+posJ[k];//如果超出棋盘范围if (nextI>=8||nextI<0||nextJ>=8||nextJ<0){continue;}//如果还没走过该位置if (chessboard[nextI][nextJ]==0){//记录下当前位置的八个方位的可走的方位坐标nexI[posCount] = nextI;nexJ[posCount] = nextJ;++posCount;}}//如果还没走到第64个位置,但是已经没有地方可走,则返回“没有可遍历路径”if (posCount==0&&queueNumber<63){return false;}minPosCount=8;//用一个最大值初始化,才能取到所需的最小值//对各个方位进行考察for (int posNum=0;posNum<posCount;++posNum){posCountTemp=0;//考察新的位置,需归零//对各个方位上各自的方位进行考察for (int k=0;k<8;++k){nextnextI=nexI[posNum]+posI[k];nextnextJ=nexJ[posNum]+posJ[k];if (nextnextI>=8||nextnextI<0||nextnextJ>=8||nextnextJ<0)//如果超出棋盘范围,注意是nextnextI。复制粘贴保留成nextI是错误的{continue;}//如果还没走过该位置if (chessboard[nextnextI][nextnextJ]==0){++posCountTemp;}}if (minPosCount>posCountTemp){minPosCount=posCountTemp;nowI=nexI[posNum];//选定好位置之后需更新的是nowI和nowJnowJ=nexJ[posNum];//之前写成了nextI和nextJ,也是复制粘贴保留的错误}}chessboard[nowI][nowJ]=queueNumber;//nowI和nowJ}return true;}void main(){int startI,startJ;for (int i=0;i<8;++i){for (int j=0;j<8;++j){chessboard[i][j]=0;}}printf("请输入开始节点的坐标,并用空格分隔(如2 4):");scanf("%d %d",&startI,&startJ);if(FindPath(startI,startJ)){printf("路径如下:\n");for (int i=0;i<8;++i){for (int j=0;j<8;++j){printf("%4d",chessboard[i][j]);}printf("\n");}}elseprintf("未找到遍历棋盘所有节点的路径!");}
尽自己所能把注释写得详细了,但愿以后还能看得懂╮( ̄▽ ̄")╭
写在语句后面的注释,表明该语句是导致我的程序出现问题的原因所在……两个地方都是因为复制粘贴保留了一个错误值,我也算是为我的懒惰付出了惨痛的代价……
0 0
- Knight's Tour骑士游历问题(C语言实现)
- 骑士游历问题(C语言代码)
- POJ2488 A Knight's Journey(深搜DFS,字典序,骑士游历问题)
- POJ2488 骑士游历 A Knight's Journey 解题报告
- poj2243 Knight Moves 骑士游历问题的纯数学方法!!!
- 骑士周游,Knight Tour
- wikioi骑士游历c
- 骑士游历问题
- 骑士游历问题
- 骑士游历问题
- The knight\'s tour(马周游问题)
- Knight Tour 骑士走棋盘算法(附代码)
- Algorithm Gossip (7) 骑士走棋盘(Knight tour)
- poj 1915 骑士游历问题
- poj2488--A Knight's Journey(dfs,骑士问题)
- 骑士游历
- 骑士游历
- 骑士游历
- 添加库存
- Fibonacci again and again(Nim博弈)
- keil 新建stm32工程
- 图像坐标:我想和世界坐标谈谈(A) 【计算机视觉学习笔记--双目视觉几何框架系列】
- Android内存分析和调优(下)
- Knight's Tour骑士游历问题(C语言实现)
- Linux命令学习:mkdir
- 验证图是否相似
- linux上删除所有的无效文件链接
- HDU 4694 Important Sisters
- pdns 错误解决[备忘]
- ArrayList与数组
- 修改服务器3389远程登录端口
- Spring事务配置的五种方式及事务传播