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 <- 这个题深搜广搜都是可以的,迷宫已经给出了,就看怎么做了:一般起点终点确定用广搜 ...
随机推荐
- Swfit中视图跳转
.跳转到任一UIViewController var sb = UIStoryboard(name: "Main", bundle:nil) var vc = sb.instant ...
- asp.net多图片上传同时保存对每张图片的描述
前台aspx //图片预览和描述 function previewImage(file) { var div = document.getElementById('preview'); div.inn ...
- SqlServer2008 数据库同步的两种方式(Sql JOB)
尊重原著作:本文转载自http://www.cnblogs.com/tyb1222/archive/2011/05/27/2060075.html 数据库同步是一种比较常用的功能.下面介绍的就是数据库 ...
- BZOJ 3875: [Ahoi2014]骑士游戏
d[i]表示消灭i所需的最小体力值, d[i] = min(S[i], K[i]+Σd[x]), Σd[x]表示普通攻击而产生的其他怪兽. 因为不是DAG, 所以用个队列类似SPFA来更新答案. -- ...
- Linux学习之sed命令详解
概述 sed是stream editor的简称,也就是流编辑器.它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区 ...
- Asp.net MVC学习
一.mvc项目的创建并运行 1.启动vs2010 2.新建项目 3.选择Asp.net mvc应用程序 4.不创建测试用例 5.创建之后的效果 6.运行后的mvc程序
- Linux SSH 互信
第一步: 创建用于身份认证的两个密钥文件 ssh-keygen #注明.想省事的话打完这个命令后一直回车就行了. 第二步: 把公钥上传到目标主机上去 ssh-copy-id -i id_rsa.pub ...
- 6. java.lang.IllegalArgumentException
方法的参数错误 比如g.setColor(int red,int green,int blue)这个方法中的三个值,如果有超过255的也会出现这个异常,因此一旦发现这个异常,我们要做的,就是赶紧去检查 ...
- php命名空间使用
对于命名空间,官方文档已经说得很详细[查看],我在这里做了一下实践和总结. 命名空间一个最明确的目的就是解决重名问题,PHP中不允许两个函数或者类出现相同的名字,否则会产生一个致命的错误.这种情况下只 ...
- arcEngine添加标注(上)
arcEngine添加标注有3个技术点:1,获得图层的属性字段:2,初始化符号显示控件:3,添加标注. 获得图层的属性字段: //每个图层都是一个表的图形化显示,ILayer跟ITale可以互相转 ...