BFS 骑士的移动
骑士的移动
题目链接: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 骑士的移动的更多相关文章
- [BFS]骑士旅行
骑士旅行 Description 在一个n m 格子的棋盘上,有一只国际象棋的骑士在棋盘的左下角 (1;1)(如图1),骑士只能根据象棋的规则进行移动,要么横向跳动一格纵向跳动两格,要么纵向跳动一格横 ...
- BFS-基础简单的算法
前言 有时候,当你并不了解很多高级算法的时候,搜索不失为一种解决问题的好方法,而且很多高级算法有或多或少的会用到搜索或者搜索的思想.可见,搜索是一个基础并且必须要掌握的算法. 在这篇文章中,会对BFS ...
- 【数据结构与算法Python版学习笔记】目录索引
引言 算法分析 基本数据结构 概览 栈 stack 队列 Queue 双端队列 Deque 列表 List,链表实现 递归(Recursion) 定义及应用:分形树.谢尔宾斯基三角.汉诺塔.迷宫 优化 ...
- 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS
[Usaco2005 Dec]Knights of Ni 骑士 Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...
- 骑士问题(knight) (BFS)
题目描述 在一个标准8×8的国际象棋棋盘上,棋盘中有些格子可能是有障碍物的.已知骑士的初始位置和目标位置,你的任务是计算出骑士最少需要多少步可以从初始位置到达目标位置.有障碍物的格子当然不可以到达. ...
- BZOJ 1671: [Usaco2005 Dec]Knights of Ni 骑士 (bfs)
题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1671 题解: 按题意分别从贝茜和骑士bfs然后meet_in_middle.. 把一个逗号 ...
- hdu 1372 骑士从起点走到终点的步数 (BFS)
给出起点和终点 求骑士从起点走到终点所需要的步数 Sample Inpute2 e4 //起点 终点a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 f6 Sample OutputT ...
- 【BZOJ】1671: [Usaco2005 Dec]Knights of Ni 骑士(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1671 从骑士bfs一次,然后从人bfs一次即可. #include <cstdio> # ...
- 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:由于各种原因而不可通 ...
随机推荐
- jquery获取、改变元素属性值
//标签的属性称作元素属性,在JS里对应的DOM对象的对应属性叫DOM属性.JS里的DOM属性名有时和原元素属性名不同. //==================================操作元 ...
- hdu 4023 2011上海赛区网络赛C 贪心+模拟
以为是贪心,结果不是,2333 贪心最后对自己绝对有利的情况 点我 #include<cstdio> #include<iostream> #include<algori ...
- git checkout 命令详解
转自:http://www.cnblogs.com/hutaoer/archive/2013/05/07/git_checkout.html?utm_source=tuicool&utm_me ...
- Web开发基本准则-55实录-Web访问安全
Web开发工程师请阅读下面的前端开发准则,这是第一部分,强调了过去几年里我们注意到的Web工程师务须处理的Web访问安全基础点.尤其是一些从传统软件开发转入互联网开发的工程师,请仔细阅读,不要因为忽视 ...
- loj 1038(dp求期望)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25915 题意:求一个数不断地除以他的因子,直到变成1的时候 除的次 ...
- OpenGL大作业
GLfloat light0_position[] = { 15.0,15.0,15.0,10.0 };//定义光源位置 103glLightfv(GL_LIGHT0, GL_POSITION, li ...
- ASP.Net MVC开发基础学习笔记(5):区域、模板页与WebAPI初步
一.区域—麻雀虽小,五脏俱全的迷你MVC项目 1.1 Area的兴起 为了方便大规模网站中的管理大量文件,在ASP.NET MVC 2.0版本中引入了一个新概念—区域(Area). 在项目上右击创建新 ...
- 改变图片尺寸(python)
for name in /图片路径; do convert -resize 256x256! $name $namedone
- 如何查看经过编码的cookie?
方法1.去在线工具网站(http://tool.oschina.net/encode?type=2)手动复制编码的cookie,转码后查看. 方法2.用火狐浏览器打开网页,如果有历史记录(存在cook ...
- hdu 1078 FatMouse and Cheese
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...