骑士的移动

题目链接: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. MathType 常用快捷键

    MathType 数学公式编辑器是广大理科生电脑上必不可少的软件!然而在大量公式时,不会巧妙使用快捷键真的是心累身累.巧妙使用MathType数学公式编辑器可加快数学符号的录入速度和效率,这将节约大量 ...

  2. 查询sqlserver 正在执行的sql语句的详细信息

    SELECT [Spid] = session_Id, ecid, [Database] = DB_NAME(sp.dbid), [User] = nt_username, [Status] = er ...

  3. this和super

    1.this * 每个类的每个非静态方法(没有被static修饰)都会隐含一个this引用名称,它指向调用这个方法的对象. * 当在方法中使用本类的属性时,都会隐含地使用this名称,当然也可以明确指 ...

  4. SQL详解(下)

    约束 *约束是添加在列上的,用来约束列的! 1. 主键约束(唯一标识)  特点:非空,唯一,被引用  创建表时指定主键的两种方式,分别为:    CREATE TABLE stu(     sid   ...

  5. Android Manifest 权限描述大全

    权限 名称 描述 android.permission.ACCESS_CHECKIN_PROPERTIES 访问登记属性 读取或写入登记check-in数据库属性表的权限 android.permis ...

  6. 数据库分库分表sharding1

    sharding Vertical Sharding 把数据分散到多台物理机(我们称之为Shard) 实现Sharding需要解决一系列关键的技术问题,这些问题主要包括:切分策略.节点路由.全局主键生 ...

  7. 廖雪峰js教程笔记 2

    arguments JavaScript还有一个免费赠送的关键字arguments,它只在函数内部起作用,并且永远指向当前函数的调用者传入的所有参数.arguments类似Array但它不是一个Arr ...

  8. shell-bash学习02运算、拼接、重定向

    运算 let操作 可以直接执行基本的算术操作;使用时,变量名之前不需要再添加$; #!/bin/bash no1=4; no2=5; let result=no1+no2 echo $result 自 ...

  9. express-1 从Node开始

    hello world var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, { ...

  10. 理解是最好的记忆方法 之 CSS中a链接的④个伪类为何有顺序

    理解是最好的记忆方法 之 CSS中a链接的④个伪类为何有顺序 在CSS中,a标签有4种伪类,分别为: a:link, a:visited, a:hover, a:active 对其稍有了解的前端er都 ...