骑士的移动

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83498#problem/E

题目:

Description

A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.

Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.

Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.

Input Specification

The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.

Output Specification

For each test case, print one line saying "To get from xx to yy takes n knight moves.".

Sample Input

e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6

Sample Output

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. 题意:
 输入标准8*8国际象棋棋盘上的两个格子(列用a~h表示,行用1~8表示),
求马最少需要多少步从起点跳到终点。
分析:
马每次有八个方向可以走动,直接用BFS进行搜索即可;
不过要注意当马在棋盘的边境时有的方向不能走(不能越境),
还有不能重复走(走过的地方进行标记)。
 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int c[][];
int dir[][] = {{-,-},{-,},{-,},{,},{,},{,-},{,-},{-,-}};
typedef struct
{
int x,y,count;
}node;
node start,finish;
int main()
{
char row,end;
int col,ed;
int min;
while(scanf("%c",&row)!=EOF)
{
scanf("%d",&col);
getchar();
scanf("%c%d",&end,&ed);
getchar();
start.x = row-'a'+;
start.y = col;
finish.x = end-'a'+;
finish.y = ed;
if(start.x==finish.x&&start.y==finish.y)
min = ;
else
{
memset(c,,sizeof(c));
node pre,cur;
start.count = ;
queue<node> q;
q.push(start);
c[start.x][start.y] = ;
while(!q.empty())
{
pre = q.front();
q.pop();
if(pre.x == finish.x&&pre.y == finish.y)
min=pre.count;
for(int i = ; i < ; i++)
{
cur.x = pre.x + dir[i][];
cur.y = pre.y + dir[i][];
if(cur.x<||cur.x>||cur.y<||cur.y>)continue;
if(c[cur.x][cur.y]==)continue;
c[cur.x][cur.y] = ;
cur.count = pre.count + ;
q.push(cur);
}
}
}
printf("To get from %c%d to %c%d takes %d knight moves.\n",row,col,end,ed,min);
}
return ;
}

												

BFS 骑士的移动的更多相关文章

  1. [BFS]骑士旅行

    骑士旅行 Description 在一个n m 格子的棋盘上,有一只国际象棋的骑士在棋盘的左下角 (1;1)(如图1),骑士只能根据象棋的规则进行移动,要么横向跳动一格纵向跳动两格,要么纵向跳动一格横 ...

  2. BFS-基础简单的算法

    前言 有时候,当你并不了解很多高级算法的时候,搜索不失为一种解决问题的好方法,而且很多高级算法有或多或少的会用到搜索或者搜索的思想.可见,搜索是一个基础并且必须要掌握的算法. 在这篇文章中,会对BFS ...

  3. 【数据结构与算法Python版学习笔记】目录索引

    引言 算法分析 基本数据结构 概览 栈 stack 队列 Queue 双端队列 Deque 列表 List,链表实现 递归(Recursion) 定义及应用:分形树.谢尔宾斯基三角.汉诺塔.迷宫 优化 ...

  4. 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS

    [Usaco2005 Dec]Knights of Ni 骑士 Description  贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...

  5. 骑士问题(knight) (BFS)

    题目描述 在一个标准8×8的国际象棋棋盘上,棋盘中有些格子可能是有障碍物的.已知骑士的初始位置和目标位置,你的任务是计算出骑士最少需要多少步可以从初始位置到达目标位置.有障碍物的格子当然不可以到达. ...

  6. BZOJ 1671: [Usaco2005 Dec]Knights of Ni 骑士 (bfs)

    题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1671 题解: 按题意分别从贝茜和骑士bfs然后meet_in_middle.. 把一个逗号 ...

  7. hdu 1372 骑士从起点走到终点的步数 (BFS)

    给出起点和终点 求骑士从起点走到终点所需要的步数 Sample Inpute2 e4 //起点 终点a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 f6 Sample OutputT ...

  8. 【BZOJ】1671: [Usaco2005 Dec]Knights of Ni 骑士(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1671 从骑士bfs一次,然后从人bfs一次即可. #include <cstdio> # ...

  9. bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士【bfs】

    bfs预处理出每个点s和t的距离d1和d2(无法到达标为inf),然后在若干灌木丛格子(x,y)里取min(d1[x][y]+d2[x][y]) /* 0:贝茜可以通过的空地 1:由于各种原因而不可通 ...

随机推荐

  1. C++中单例模式

    //C++单例模式:指一个类只生成一个对象 #include <iostream> using namespace std; class A{ public: static A* getA ...

  2. 退出Activity(转)

    退出Activity 如何退出Activity?如何安全退出已调用多个Activity的Application? 退出activity 直接调用 finish () 方法 . //用户点击back键  ...

  3. WEB前端知识体系脑图

    说在开始的话: 我上大学那会,虽说主要是学Java语言,但是web前端也稍微学了一些,那时候对前端也没多在意,因为涉入的不深,可以搞一个差不多可以看的界面就可以了,其他也没过多在意. 因为稍微了解一点 ...

  4. Redis学习笔记(2) Redis基础类型及命令之一

    1. 基础命令 (1) 获取符合规则的键名列表 格式为:KEYS pattern 其中pattern表示支持通配符 # 建立一个名为bar的键 > SET bar OK # 获取Redis所有键 ...

  5. 去除字符串中的html标记及标记中的内容

    去除字符串中的html标记及标记中的内容 --1.创建函数 create function [dbo].[clearhtml] (@maco varchar(8000)) returns varcha ...

  6. Junit的简单使用

    Junit是一个很好用的单元测试工具,下面是使用Junit来测试方法的简单案例: import java.util.ArrayList; import java.util.Iterator; impo ...

  7. 本人经过测试认为最简单最好的popupwindow样式

    <shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- solid 设置 ...

  8. CSS3 calc()的使用

    前言: 平时在制作页面的时候,总会碰到有的元素是100%的宽度.众所周知,如果元素宽度为100%时,其自身不带其他盒模型属性设置还好,要是有别的,那将导致盒子撑破.比如说,有一个边框,或者说有marg ...

  9. 《DSP using MATLAB》示例Example4.14

    代码: b = [1]; a = [1, -1.5, 0.5]; % [R, p, C] = residuez(b,a) Mp = (abs(p))' Ap = (angle(p))'/pi % ch ...

  10. mvc-10部署

    性能 提高性能最简单的办法就是减少HTTP的请求数量,每个HTTP请求除了有TCP开销外,还包含大量的头信息: 让页面和其资源文件保持较小的体积将减少网络用时,对于互联网上的应用而言,这才是真正的瓶颈 ...