Problem B: 最少步数
Description
A friend of you is doing research on theTraveling Knight Problem (TKP) where you are to find the shortest closed tourof knight moves that visits each square of a given set of n squares on achessboard exactly once. He thinks that the most difficult part of the problemis determining the smallest number of knight moves between two given squaresand 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 programthat solves the “difficult” part.
Your job is to write a program that takes two squares a and b as input and thendetermines the number of knight moves on a shortest route from a to b.
Input
The input will contain one or more testcases. Each test case consists of one line containing two squares separated byone space. A square is a string consisting of a letter (a-h) representing thecolumn and a digit (1-8) representing the row on the chessboard.
Output
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.
思路:这是一道比较简单的BFS,刚开始想着输入8*8,a-h,1-8;后来看了一点别人的
输入的控制,两个字符数组,a和b就可以啦,注意马走日
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int dx[]={-,-,,,,,-,-};
int dy[]={-,-,-,-,,,,};
struct dot
{
int x,y;
int time;
};
inline bool in(dot gx)
{
if(gx.x>=&&gx.x<&&gx.y>=&&gx.y<)
return true;
return false;
}
int main()
{
char a[],b[];
int vis[][];
while(scanf("%s%s",&a,&b)!=EOF)
{ dot gx;
int x1=a[]-'a';
int y1=a[]-'';
int x2=b[]-'a';
int y2=b[]-'';
gx.x=x1;
gx.y=y1;
gx.time=;
queue<dot>q;
while(!q.empty())
q.pop();
q.push(gx);
memset(vis,,sizeof(vis));
int step=;
if(strcmp(a,b))
{
while(!q.empty())
{
dot tmp,next;
tmp=q.front(),q.pop();
for(int i=;i<;i++)
{
next.x=tmp.x+dx[i];
next.y=tmp.y+dy[i];
next.time=tmp.time+;
if(in(next)&&!vis[next.x][next.y])
{
vis[next.x][next.y]=;
q.push(next);
if(next.x==x2&&next.y==y2)
{
step=next.time;
break;
}
}
}
if(step>)
break;
}
}
else
step=;
printf("To get from ");
cout<<a<<" "<<"to"<<" "<<b;
printf(" takes %d knight moves.\n",step); } }
Problem B: 最少步数的更多相关文章
- NYOJ 58 最少步数
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...
- ACM 最少步数
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...
- [ACM_搜索] ZOJ 1103 || POJ 2415 Hike on a Graph (带条件移动3盘子到同一位置的最少步数 广搜)
Description "Hike on a Graph" is a game that is played on a board on which an undirected g ...
- nyoj 1022 最少步数【优先队列+广搜】
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...
- 最少步数(bfs)
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...
- 最少步数(dfs + bfs +bfs优化)
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...
- 最少步数(bfs)
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...
- T1330 最少步数(#Ⅱ- 8)(广度优先搜索)
[题目描述] 在各种棋中,棋子的走法总是一定的,如中国象棋中马走“日”.有一位小学生就想如果马能有两种走法将增加其趣味性,因此,他规定马既能按“日”走,也能如象一样走“田”字.他的同桌平时喜欢下围棋, ...
- NYOJ-58最少步数,广搜思想!
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 -> Link <- 这个题深搜广搜都是可以的,迷宫已经给出了,就看怎么做了:一般起点终点确定用广搜 ...
随机推荐
- Yii的场景
先上代码 class User extends CActiveRecord{ public function rules() { return array( ...
- Cow Sorting(置换群)
Cow Sorting Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6664 Accepted: 2602 Descr ...
- JS函数作用域及作用域链理解
从事web开发工作,尤其主要是做服务器端开发的,难免会对客户端语言JavaScript一些概念有些似懂非懂的,甚至仅停留在实现功能的层面上,接下来的文章,是记录我对JavaScript的一些概念的理解 ...
- mvc 设置默认页技巧
打开网址:http://xxxx.com自动跳转==>http://xxx.com/home/index 设置route入口: routes.MapRoute( name: "Main ...
- 第四课 Grid Control实验 GC Agent安装(第一台机器部署) 及卸载
3.GC Agent安装(第一台机器部署) 安装Agent 拷贝agent,现在ocm2机器上查找agent.linux 查找文件的方法: find ./ -name agent*linux 把ag ...
- java.util.vector中的vector的详细用法
ArrayList会比Vector快,他是非同步的,如果设计涉及到多线程,还是用Vector比较好一些 import java.util.*; /** * 演示Vector的使用.包括Vector的创 ...
- php7 install memcached extension
#download source code package from git $ git clone https://github.com/php-memcached-dev/php-memcache ...
- 环境配置与JBoss安装-EJB3.0入门经典学习笔记(1)
目录 1. JDK的安装 2. JBoss的安装 3. JBoss安装目录说明 1. JDK的安装 1) 下载JDK 下载地址:http://www.oracle.com/technetwork/ja ...
- python 文本编辑基础记录
不熟悉编码方式,同时python的编码方式折磨我了很长时间,记录下,以免忘记,本文内容存在错误,是自己理解,看到仅当参考 Unicode 是字符集,有点像一本字典,utf-8是在unicode这本字典 ...
- 九度OJ 题目1534:数组中第K小的数字(二分解)
题目链接:点击打开链接 题目描述: 给定两个整型数组A和B.我们将A和B中的元素两两相加可以得到数组C. 譬如A为[1,2],B为[3,4].那么由A和B中的元素两两相加得到的数组C为[4,5,5,6 ...