题意:8*8国际象棋棋盘,求马从起点到终点的最少步数。

编写时犯的错误:1、结构体内没构造。2、bfs函数里返回条件误写成起点。3、主函数里取行标时未注意书中的图。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<sstream>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<list>
using namespace std;
const int MAXN=+;
const int INF=0x7f7f7f7f;
const double PI=acos(1.0);
typedef long long ll;
typedef unsigned long long llu;
char s1[];
char s2[];
int vis[][];
struct KK
{
int x_,y_,bushu_;//x行标,y列标,bushu步数
KK(int x=,int y=,int bushu=):x_(x),y_(y),bushu_(bushu){}//构造!构造!构造!
};
int stax[]={-,-,-,-,,,,};//马的八种移动方式
int stay[]={-,,-,,-,,-,};//行x(上减下加),列y(左减右加)
int bfs(int a,int b,int c,int d)
{
memset(vis,,sizeof(vis));//!!!
queue<KK> q;
q.push(KK(a,b,));//将起点压入队列,此时步数为0
vis[a][b]=;//标记!!!
while(!q.empty())
{
KK tmp=q.front();
q.pop();
int tmpx=tmp.x_;
int tmpy=tmp.y_;
int tmpb=tmp.bushu_;
if(tmpx==c&&tmpy==d)//若此时位置等于终点坐标,则返回步数
return tmpb;
for(int i=;i<;i++)//从这一点遍历八个方向
{
int tx=tmpx+stax[i];
int ty=tmpy+stay[i];
if(tx<||tx>=||ty<||ty>=)//越界
continue;
if(!vis[tx][ty])
{
q.push(KK(tx,ty,tmpb+));//未被标记则压入,注意步数加1
vis[tx][ty]=;//标记!!!
}
}
}
}
int main()
{
while(scanf("%s%s",s1,s2)==)
{
int a=-(s1[]-'');//紫书上的图一到八行是8到1,这里取行标(从上到下----0~7)
int b=s1[]-'a';//列标
int c=-(s2[]-'');
int d=s2[]-'a';
int step=bfs(a,b,c,d);
printf("To get from %s to %s takes %d knight moves.\n",s1,s2,step);
}
return ;
}

uva439 - Knight Moves(BFS求最短路)的更多相关文章

  1. POJ 2243 Knight Moves(BFS)

    POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...

  2. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

  3. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  4. UVa 816 (BFS求最短路)

    /*816 - Abbott's Revenge ---代码完全参考刘汝佳算法入门经典 ---strchr() 用来查找某字符在字符串中首次出现的位置,其原型为:char * strchr (cons ...

  5. BFS求最短路

    假设有一个n行m列的迷宫,每个单位要么是空地(用1表示)要么是障碍物(用0表示).如和找到从起点到终点的最短路径?利用BFS搜索,逐步计算出每个节点到起点的最短距离,以及最短路径每个节点的前一个节点. ...

  6. UVA 816 -- Abbott's Revenge(BFS求最短路)

     UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...

  7. 6.4.2 用BFS求最短路

    前面的篇幅占了太多,再次新开一章,讲述BFS求最短路的问题 注意此时DFS就没有BFS好用了,因为DFS更适合求全部解,而BFS适合求最优解 这边再次提醒拓扑变换的思想在图形辨认中的重要作用,需要找寻 ...

  8. POJ-1915 Knight Moves (BFS)

    Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26952   Accepted: 12721 De ...

  9. (step4.2.1) hdu 1372(Knight Moves——BFS)

    解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...

随机推荐

  1. Codeforces Gym 100425A Luggage Distribution 二分 数学

    A - Luggage DistributionTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/c ...

  2. C++ AfxBeginThread

    计算从1+2+3...+100000=? 关键点 CWinThread* AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam, in ...

  3. 行规——::GetDC()和::ReleaseDC()配对

    1.今天向客户演示程序,不巧崩溃了.最后发现是使用了GetDC()后没有使用ReleaseDC()引起的. 2.最早听说GetDC()和ReleaseDC()是从前任带头码哥那里听说的,那时候仅仅用& ...

  4. 【BZOJ1486】【HNOI2009】最小圈 分数规划 dfs判负环。

    链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  5. C#_模拟webAp_POST-GET-PUT-DELETE

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  6. Maven学习小结(五 pom.xml详解[转])

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  7. B - Plane of Tanks: Pro

    Description Vasya has been playing Plane of Tanks with his friends the whole year. Now it is time to ...

  8. android源码编译过程

    1.下载好android源码包. 2.装好vm,ubuntu(如果能在实体机装linux更好). 3.安装所需要的deb包 在终端执行如下命令: sudo apt-get install flex b ...

  9. JS中获取table节点的tr或td的内容

    <table id="tb1" width="200" border="1" cellpadding="4" ce ...

  10. LeetCode 343

    Integer Break Given a positive integer n, break it into the sum of at least two positive integers an ...