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 <- 这个题深搜广搜都是可以的,迷宫已经给出了,就看怎么做了:一般起点终点确定用广搜 ...
随机推荐
- Fibonacci Tree(最小生成树,最大生成树)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- ASP.NET MVC 中将FormCollection与实体间转换方法【转】
将Action动作中传递的FormCollection转变成对应的实体,可以使用Controller的TryUpdateModel()方法. 示例如下: [HttpPost] public Actio ...
- OC中最难的一部分内容:内存管理
OC中最难的一部分内容:内存管理为什么说他难呢?因为内存如果需要我们程序员去管理的话,那个难度肯定是很大的,如果是Java,垃圾回收器会把这份工作给做了,我们不需要关心,但是就是因为如此,Androi ...
- Android Spinner 下拉列表
private Spinner spinner ; private List<String> list ; private ArrayAdapter< ...
- SQL复杂查询(子查询)
USE STUDY SELECT * from EMP SELECT * FROM SALGRADE --1.查询雇员姓名,所在部门编号和名称 SELECT ename,EMP.deptno,DEPT ...
- erlang学习笔记(2)
函数%###geometry.erl###-module(geometry). 定义-export([area/1, function1/2, function2/0, ...]).area({rec ...
- 不显示BOM清单的版本
应用 Oracle Bill Of Materiel 层 Level Function 函数名 Funcgtion Name BOM_BOMFDBOM 表单名 Form Name BOMFDBOM ...
- 解密电子书之四:MCU(freescale)
谈完国产的君正,让我们再看看呛了君正财路的freescale iMX51. 这是freescale近期的主打产品,用的是ARM Cortex A8架构,主频在消费电子领域最高可达800MHz,在工业领 ...
- 减小Delphi的Exe文件大小(11种方法)
一般来说,由Delphi生成的EXE文件,要比其由它编程语言生成的体积大一些.这主要是由于使用VCL的原因(当然,VCL是有许多优点的!) 以下是减小EXE文件大小的几种途径: 01) 使用加壳工具( ...
- XDocument读取xml的所有元素以及XPath语法
原文 http://www.cnblogs.com/xxyishutong/p/3326375.html <?xml version="1.0" encoding=&quo ...