题目见zoj 1091

使用宽度搜索优先来求解,这个算法已经忘记的差不多了,所以写出来的代码很罗嗦,看起来很不清晰。

好像还可以直接用公式或者神经网络算法求解,详见Knight's Tour

/* zoj 1091 Knight Moves */
#include <stdio.h>
#include <string.h>
#define MAX 100
#define BOARDSIZE (8+1)
struct queueStruct{int x;int y;int step;
}queue[MAX],tempQueue;
int front;
int  rear;
int minStep;
int isVisited[BOARDSIZE][BOARDSIZE];
const int searchTable[8][2] = {{-2,-1},{-1,-2},{-2,1},{-1,2},{1,2},{2,1},{1,-2},{2,-1}};
int bfs(int startx, int starty, int endx, int endy, int step);int main(void)
{char src[3],des[3];int startx, starty, endx, endy;while(scanf("%s %s",src,des) == 2){memset(isVisited,0,sizeof(isVisited));memset(queue,0,sizeof(queue));front = rear = 0;startx = src[0] - 'a' + 1;starty = src[1] - '0';endx = des[0] - 'a' + 1;endy = des[1] - '0';bfs(startx,starty,endx,endy,0);printf("To get from %s to %s takes %d knight moves.\n",src,des,minStep);}return 0;
}
int bfs(int startx, int starty, int endx, int endy, int step)
{int i;struct queueStruct q;if(startx == endx && starty == endy){minStep = step;return 0;}isVisited[startx][starty] = 1;tempQueue.x = startx;tempQueue.y = starty;tempQueue.step = step;queue[rear++] = tempQueue;while(front < rear){tempQueue = queue[front++];for(i = 0; i < BOARDSIZE; i++){/*    q = tempQueue;*/q.x = tempQueue.x + searchTable[i][0];q.y = tempQueue.y + searchTable[i][1];q.step = tempQueue.step + 1;if(q.x < 1 || q.x > 8 || q.y < 1 || q.y > 8 ||isVisited[q.x][q.y])continue;if(q.x == endx && q.y == endy){minStep = q.step;return 0;}queue[rear++] = q;isVisited[q.x][q.y] = 1;}}return 0;
}

zoj 1091 Knight Moves相关推荐

  1. UVa 439 - Knight Moves

    给一个8*8的棋盘,给出骑士的起点和终点,求出从起点到终点的走的最小步数.这个也是ZOJ 的1091题. 用bfs解决,一个vis数组保存是否访问过,dis数组保存从起点到该点的移动次数.代码如下: ...

  2. poj - 2243 Knight Moves

    这题和poj 1915一样,用bfs做走马步.现在再看当时的代码,真是好幼稚啊. 1 #include <stdio.h> 2 #include <string.h> 3 in ...

  3. 1 Knight Moves

    Problem F- Knight Moves 题目来源:https://vjudge.net/contest/207868#problem/F Problem description: 题意概括:中 ...

  4. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

  5. OpenJudge/Poj 1915 Knight Moves

    1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...

  6. hdu1372 Knight Moves BFS 搜索

    简单BFS题目 主要是读懂题意 和中国的象棋中马的走法一样,走日字型,共八个方向 我最初wa在初始化上了....以后多注意... 代码: 1 #include <iostream> 2 # ...

  7. Knight Moves

    Knight Moves poj-1915 题目大意:在国际象棋中有一个骑士,想从一个点到达另一个点.骑士可以从2*3的矩阵的一角跳到对角线的另一角.问:骑士最少需要跳多少次才能从一个点跳到另一个点. ...

  8. POJ2243 Knight Moves —— A*算法

    题目链接:http://poj.org/problem?id=2243 Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  9. (BFS)Knight Moves(hdu1372)

    题目: 在象棋王国,尼古拉斯.火山是一匹英俊的马,他非常幸运迎娶了白马王国的公主,他们将度蜜月,你现在是他们的女仆,火山会问你去一些地方最少需要多少步,这么简单的事当然难不倒你.由于火山是一匹马,他的 ...

最新文章

  1. 33个神经网络「炼丹」技巧
  2. Maven学习——安装与修改Maven的本地仓库路径
  3. 【更名通知】将以个人名义继续更新维护
  4. RPC调用链通信方法
  5. 超 10000 名开发者在追的技术栏目,你绝不能错过!
  6. 怎样才算是一个好的老师???
  7. Centos5.8升级SSH到5.8p2
  8. LINUX虚拟机安装增强功能时报错:/sbin/mount.vboxsf: mounting failed with the error: No such device
  9. 2022年最新《谷粒学院开发教程》:2 - 前后端交互篇
  10. 在线Latex公式编辑器
  11. 学习笔记(01):10小时掌握区块链开发教程-2小时构建以太坊智能合约-1
  12. BCD码中的8421码、2421码、5421码和余3码
  13. Siebel 数学运算
  14. 游戏开发核心技术之-存档与读档(1)
  15. A Morden C++ —— C++11新特性指南
  16. 基于网络分析和文本挖掘的意见领袖影响力研究
  17. ArcToolbox的运行结果发布为GP服务
  18. 性能优化之YUICompressor压缩JS、CSS
  19. 成功实施SOA:从物理排列到化学反应
  20. IDEA中Lam表达式爆红问题jdk8

热门文章

  1. 15-多容器复杂应用的部署
  2. centos6设置静态IP
  3. squid代理服务器在企业网中的应用
  4. 【转载】给程序员的圣诞大礼★★春夏
  5. git 上传代码到码云
  6. 一篇文章解释struts常用功能
  7. c语言单字符输入和输出函数分别为,第03章单元总练习-实训-知识拓展.doc
  8. hexeditor 复制二进制值_MySQL复制全解析 Part 6 MySQL GTID 生命周期
  9. php ci框架 实例化类,php框架CI(codeigniter)自动加载与自主创建对象操作实例分析...
  10. [转载] JAVA数组实现学生成绩统计