Knight's Tour骑士游历问题(C语言实现)
2015-01-30 10:41
666 查看
#include<stdio.h> int chessboard[8][8]; bool FindPath(int startI,int startJ) { //走法的序号 int queueNumber; //开始位置标注为1 chessboard[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和nowJ nowJ=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"); } } else printf("未找到遍历棋盘所有节点的路径!"); }
尽自己所能把注释写得详细了,但愿以后还能看得懂╮( ̄▽ ̄")╭
写在语句后面的注释,表明该语句是导致我的程序出现问题的原因所在……两个地方都是因为复制粘贴保留了一个错误值,我也算是为我的懒惰付出了惨痛的代价……
相关文章推荐
- MySQL一次执行多条语句的实现及常见问题(C API Commands out of sync; you can't run this command now)
- GEF,EMF,RCP,Eclipse's plugin的几个问题(3) 让eclipse的properies view实现disabled效果
- dojo grid遇到的问题 点击按钮“全选”'删除"无法实现
- c语言:模拟实现printf,要求功能:print(&quot;ccc\ts!&quot;,&#39;b&#39;,&#39;i&#39;,&#39;t&#39;,&quot;welcome to you&quot;);
- [算法]图算法之骑士遍历问题(象棋中马的遍历问题)分析,C语言实现
- what's in string? c语言string类函数实现汇总 觉得都是学习使用指针的好例子(算是读书摘抄和笔记吧)
- 【C语言】 缓冲区问题 循环使用getchar scanf会受到换行符'\n'的干扰
- C语言中以十六进制输出字符型变量会出现'ffffff"的问题
- what's in string? c语言string类函数实现汇总 都是学习使用指针的好例子啊(算是读书摘抄和笔记吧)
- C语言中以十六进制输出字符型变量会出现'ffffff"的问题
- C语言主函数返回值问题:return type of 'main' is not 'int' [-Wmain]
- Orange's 一个操作系统的实现--Bochs遇到的问题
- POJ2488 A Knight's Journey(深搜DFS,字典序,骑士游历问题)
- C语言:模拟实现printf,要求功能:print(&quot;ccc\ts!&quot;,&#39;b&#39;,&#39;i&#39;,&#39;t&#39;,&quot;welcome to you&quot;);
- 北航11年机试--从键盘输入包含扩展符'-'的字符串,将其扩展为等价的完整字符(C语言实现)
- Knight's Trip 马在无线大棋盘上跳到指定点最小步数问题
- 骑士游历问题(C语言代码)
- A Chinese Knight's Tour
- 递归算法——Hanoi(汉诺)问题(Java实现&C语言实现)
- bochs 2.4.2 ubuntu 安装运行问题《orange's 一个操作系统的实现》第二章 调试篇