骑士的移动

题目链接: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. mysql_multi启动数据库

    1.初始化数据库 在$mysql_base目录下,新增加存放data的文件夹,用mysql_install_db命令执行初始化 [root@ora11g scripts]# ./mysql_insta ...

  2. 从官方ROM中提取原生APK

    背景:由于自己手机总出现android.process.acore问题,最后发现是被自己精简掉了日历相关应用,故寻找提取原生apk. 注:解决方案主要是在机锋论坛上看到的. 环境要求:需要电脑安卓ja ...

  3. Javascript 学习之路:鼠标长按事件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. 使用 json_in_java

    // */ // ]]> java_in_json Table of Contents 1. Java 使用 Json 1.1. 下载地址: 1.2. 构造 json 字符串 1.3. 解析 j ...

  5. Android 实现ListView中Item被单击后背景色保持高亮

    今天为了解决一个需求,就是我有一个slidingDrawer,里面是一个ListView.然后,单击其中的Item,默认只是显示一个橙色背景后就恢复了.客户便有着个需求,需要单击这个Item的背景高亮 ...

  6. .NET运用AJAX 总结及其实例

    1.AJAX简介 (1.没有AJAX会怎么样?普通的ASP.Net每次执行服务端方法的时候都要刷新当前页面,比如实现显示服务器的时间.每次都要刷新页面的坏处:页面刷新打断用户操作.速度慢.增加服务器的 ...

  7. 【JavaScript基础学习】关于正则表达式的完整内容

    w3cJavaScript RegExp对象  这个如果第一次看的话应该会很莫名其妙,但可以看一遍留个印象. 正则表达式30分钟入门教程 这个教程非常完整,走一遍大概能够明白怎么回事了. 正则表达式在 ...

  8. js 的一点用法

     js 中的json对象,ajax返回数据dataType为json否则无法将数据转换成json对象 也就无法通过json字符串转换成对象object,那么他将始终是个字符串,也就无法进行 对象操作. ...

  9. 20145223《Java程序程序设计》第10周学习总结

    20145223<Java网络编程> 一.Java的网络编程 ·网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来. ·java.net包中J2SE的API包含有类 ...

  10. node http 服务

    var http = require("http") http.createServer(function(req,res){ res.writeHead(200,{"C ...