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 <- 这个题深搜广搜都是可以的,迷宫已经给出了,就看怎么做了:一般起点终点确定用广搜 ...
随机推荐
- Magento - get Attribute Options of the dropdown type attribute
$attribute_code = "color"; $attribute_details = Mage::getSingleton("eav/config" ...
- 畅通工程续(dijskra+SPFA)
畅通工程续 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submiss ...
- url的4种访问方式
1.PATHINFO 模式 -- 重点!!!!!! http://域名/项目名/入口文件/模块名/方法名/键1/值1/键2/值2 (可以修改 'URL_PATHINFO_DEPR'=>'-',/ ...
- JavaScript、JSP、Java及javaEE
对JavaScript.JSP.Java及javaEE之间区别的理解 JavaScript和Java名字极为类似,相信不少的初学者或者准备学这些知识的人对于JavaScript.JSP.Java及Ja ...
- LeakCanary,检测安卓,java内存泄漏
官方中文API地址:http://www.liaohuqiu.net/cn/posts/leak-canary-read-me/
- Facebook React Native 配置小结
2015 年 9 月 15 号,React Native for Android 发布.至此,React 基本完成了对多端的支持.基于 React / React Native 可以: H5, And ...
- Android 图片平铺效果实现的3种方法
Html中平铺的效果,那么我们都是怎么样才能实现的那,我们其实主要用到的就是api,我们一开始new一个bitmap,就可以了,那么我们就来说说第二种方法,那就在用到了xml,上面我们说了两个方法,但 ...
- Nginx 拒绝指定IP访问
来源 : http://www.ttlsa.com/nginx/nginx-deny-ip-access/ 闲来无事,登陆服务器,发现有个IP不断的猜测路径.试图往服务器上传文件(木马).于是查看 ...
- mysql函数操作(5)
<?php try{ $dbh = new PDO('mysql:dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd'); }catc ...
- Spring Boot的启动器Starter详解
Spring Boot的启动器Starter详解 作者:chszs,未经博主允许不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs Spring Boot ...