跳马(Knight Moves), ZOJ1091, POJ2243

题目描述:

给定象棋棋盘上两个位置 a 和 b,编写程序,计算马从位置 a 跳到位置 b 所需步数的最小值。

输入描述:

输入文件包含多个测试数据。每个测试数据占一行,为棋盘中的两个位置,用空格隔开。棋盘位置为两个字符组成的串,第 1 个字符为字母 a~h,代表棋盘中的列;第 2 个字符为数字字符1~8,代表棋盘中的行。

输出描述:

对输入文件中的每个测试数据,输出一行"To get from xx to yy takes n knight moves.", xx 和yy 分别为输入数据中的两个位置, n 为求得的最少步数。

样例输入:

样例输出:

e2 e4

a1 b2

b2 c3

a1 h8

a1 h7

h8 a1

b1 c3

f6 f6

To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.

这道题其实就是裸的bfs

不过需要注意的是:输入输出的形式!

代码如下:

1)注释版:

 #include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
char s[];
bool v[][];
int ex,ey,sx,sy,ans;
int dx[]={,,-,-,-,-,,};
int dy[]={-,-,-,-,,,,};
struct node
{
int x,y,step;
}cur,nxt; queue<node>q; void bfs()
{
if(ex==sx&&ey==sy) //特判起点等于终点
{
printf("To get from %c%d to %c%d takes %d knight moves.\n",char(ex+'a'-),ey,char(sx+'a'-),sy,);
return;
}
while(!q.empty()) q.pop(); // 多组数据初始化
memset(v,,sizeof(v)); // 同上
cur.x=ex,cur.y=ey; cur.step=; //起点
v[ex][ey]=true; //不要漏了标记起点
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop(); //不要漏了当前出队
//v[cur.x][cur.y]=false; 出队,清楚标记,是否需要? 答案当然是否定的
for(int i=;i<;i++) //八方位搜索
{
int xx=cur.x+dx[i],yy=cur.y+dy[i];
if(xx>&&xx<=&&yy>&&yy<=&&!v[xx][yy])
{
if(xx==sx&&yy==sy) //如果找到了,第一个找到的一定就是最近的
{
printf("To get from %c%d to %c%d takes %d knight moves.\n",char(ex+'a'-),ey,char(sx+'a'-),sy,cur.step+);
return ;//必须用return,或者也可以用多次break
}
nxt.x=xx, nxt.y=yy; nxt.step=cur.step+;
v[nxt.x][nxt.y]=true;//标记
q.push(nxt); //将扩展出的状态入队
}
}
}
}
int main()
{
while(scanf("%s",s)!=EOF)
{
ex=s[]-'a'+; ey=s[]-'';
scanf("%s",s);
sx=s[]-'a'+; sy=s[]-'';
bfs();
}
}

2)非注释版:(另一种方法,表示方向的换为一个二维数组)

 #include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm> using namespace std; struct node
{
int x,y;
};
int dir[][]={-,-,-,,-,-,-,,,-,,,,-,,};
int dis[][];
int ex,ey;
bool OK(node &b)
{
if(b.x<||b.x>||b.y<||b.y>||dis[b.x][b.y])
return false;
return true;
}
void BFS(int x,int y)
{
node a,b;
queue<node> Q;
a.x=x;a.y=y;
Q.push(a);
int i;
while(!Q.empty())
{
a=Q.front();Q.pop();
for(i=;i<;i++)
{
b.x=a.x+dir[i][];
b.y=a.y+dir[i][];
if(b.x==x&&b.y==y) continue;
if(OK(b))
{
dis[b.x][b.y]=dis[a.x][a.y]+;
Q.push(b);
}
if(b.x==ex&&b.y==ey) return;
}
} }
int main()
{
char op1[],op2[];
while(scanf("%s %s",op1,op2)!=EOF)
{
memset(dis,,sizeof(dis));
int x=op1[]-'a'+,y=op1[]-'';
ex=op2[]-'a'+;ey=op2[]-'';
// printf("%d %d==\n",ex,ey);
if(x!=ex||y!=ey)
BFS(x,y);
printf("To get from %s to %s takes %d knight moves.\n",op1,op2,dis[ex][ey]); }
}

跳马(Knight Moves), ZOJ1091, POJ2243 x的更多相关文章

  1. poj2243 Knight Moves(BFS)

    题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...

  2. poj2243 &amp;&amp; hdu1372 Knight Moves(BFS)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...

  3. POJ---2243 Knight Moves 使用A*算法的广度优先搜索

    题目链接:http://poj.org/problem?id=2243 启发式搜索:启发式搜索就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标.这样可以省 ...

  4. POJ2243 Knight Moves —— A*算法

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

  5. Knight Moves

    Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...

  6. HDU 1372 Knight Moves (广搜)

    题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...

  7. Knight Moves

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  8. HDU 1372 Knight Moves

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

  9. [宽度优先搜索] HDU 1372 Knight Moves

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

随机推荐

  1. 【Qt开发】事件循环与线程 一

    事件循环与线程 一 初次读到这篇文章,译者感觉如沐春风,深刻体会到原文作者是花了很大功夫来写这篇文章的,文章深入浅出,相信仔细读完原文或下面译文的读者一定会有收获. 由于原文很长,原文作者的行文思路是 ...

  2. editText内容从hint右输入

    如何让editText内容从hint右输入呢: <EditText android:id="@+id/et_password" android:textColor=" ...

  3. Mybatis-学习笔记(7)缓存机制

    1.一级缓存 SqlSession级别的缓存,使用HashMap存储缓存数据,不同的SqlSession之间的缓存数据区域(HashMap)互不影响. 一级缓存的作用域是SqlSession范围(强调 ...

  4. 剑指Offer编程题(Java实现)——反转链表

    题目描述 输入一个链表,反转链表后,输出新链表的表头. 思路一 使用头插法迭代进行反转 实现 /* public class ListNode { int val; ListNode next = n ...

  5. Javascript原型介绍

    原型及原型链 原型基础概念 function Person () { this.name = 'John'; } var person = new Person(); Person.prototype ...

  6. C#打印条码BarTender SDK打印之路和离开之路(web平凡之路)(转)

    C#打印条码BarTender SDK打印之路和离开之路(web平凡之路) 从来没想过自己会写一篇博客,鉴于这次从未知的探索到一个个难点的攻破再到顺利打印,很想记录这些点滴,让后人少走弯路. 下面走进 ...

  7. C++ static、const和static const类型成员变量声明以及初始化

    C++ static.const和static const 以及它们的初始化 const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间. sta ...

  8. PHP 如何实现页面静态化

    页面静态化分为两种 一种伪静态,即url重写,一种纯静态化. 一.静态化的优点: 1有利于搜索引擎收录网站页面的信息:搜索引擎更喜欢静态的,更变于抓取,搜索引擎SEO排名会更容易提高. 2静态网页化网 ...

  9. asp,net 传值方式 优缺点比较

    .net C#中页面之间传值传参的六种方法 1.QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中.如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用 ...

  10. Linux安装Jenkins并部署springboot项目

    安装步骤: 1.跳转连接https://pkg.jenkins.io/redhat-stable/下载安装包 2.安装命令: sudo rpm -ih jenkins-2.73.2-1.1.noarc ...