【广搜】Knight Moves
题目描述
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.

输入
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.
输出
样例输入
3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1
样例输出
5
28
0
【题目】
在[0,300]范围的棋盘上,给定起点,终点和8中行走方式,求从起点到终点的最少步数。
【思路】
给定了起始状态和结束状态,求最少步数。显然是用BFS,为了节省时间,我选择了双向BFS。双向BFS即从起点向终点搜,从终点向起点搜,扩展各自的状态,直到出现两者扩展的状态重合。一个优化,每次选择结点少的扩展。
#include<bits/stdc++.h>
using namespace std;
int dir[][] = {
{-,-} , {-,},
{-,-}, {-,},
{,-}, {,},
{,-}, {,}
};
struct qtp{
int x,y;
}q[][];
int test,ans,n,L[],R[];
int dis[][][];
int vis[][][];
int expand(int k){
int t,x,y,d,tx,ty;
x = q[k][L[k]].x;
y = q[k][L[k]].y;
d = dis[k][x][y];
for(int i=;i<;i++){
tx = x + dir[i][];
ty = y + dir[i][];
if( <=tx && tx<=n && <=ty && ty<=n && !vis[k][tx][ty] ){
vis[k][tx][ty] = ;
R[k] ++ ;
q[k][R[k]].x = tx ;
q[k][R[k]].y = ty ; dis[k][tx][ty] = d+;
if ( vis[-k][tx][ty] ){
ans = dis[k][tx][ty] + dis[-k][tx][ty];
return ;
}
}
}
return ;
}
void BFS(){
if( q[][].x == q[][].x && q[][].y == q[][].y ){
ans = ;
return ;
}
vis[][q[][].x][q[][].y] = ;
vis[][q[][].x][q[][].y] = ;
L[] = R[] = ;
L[] = R[] = ;
while ( L[] <= R[] && L[] <= R[] ){
if( R[] - L[] < R[] - L[] ){
if ( expand() ) return ;
L[] ++ ;
}else{
if ( expand() ) return ;
L[] ++ ;
}
}
}
int main()
{ for( cin>>test ; test ; test-- ){
memset(vis,,sizeof vis);
memset(q,,sizeof q );
memset(dis,,sizeof dis) ;
cin >> n ;
n--;
cin >> q[][].x >> q[][].y ;
cin >> q[][].x >> q[][].y ;
BFS();
cout << ans << endl;
}
return ;
}
Knight Moves
【广搜】Knight Moves的更多相关文章
- Knight Moves(广搜BFS)
Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to ...
- HDU 1372 Knight Moves (广搜)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
- 一道简单的广搜题:Knight Moves
这本来是要用双向宽度搜索的,但是我用简单的广搜也成功了,L<=300,也不会超时?? 另外一个问题就是,我本来想用原来的代码交,结果80分??将边界条件从小于L改成小于等于L,就对了.我可能不会 ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- 【POJ 2243】Knight Moves
题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...
- POJ 2243 Knight Moves
Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13222 Accepted: 7418 Des ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
随机推荐
- redhat7.4安装gitlab
1.参考官方安装指南 https://about.gitlab.com/install/#centos-7 2.遇到的问题 2.1.启动postfix出错 错误内容 Job for postfix.s ...
- tomcat+myeclipse+mysql环境搭建
毕业设计要做的题目,搭环境我就搭了两天...网上的教程都好碎啊.. 我的需求是这样,我有一个Android app需要与电脑上的数据库交换信息,实现增删查改,这样不管用什么方法,电脑上都要有一个服务器 ...
- 分享一个自己做的SpringMVC的PPT,由于比较忙只写了一些重要的部分
- 如何把Java的double类型变量保留两位小数
已知 双精度标量 f, 如果想以字符串形式输出,小数点后保留2位,可直接通过C语言的输出格式,System.out.printf("%.2f", f), 达到目的. 如果想要先转 ...
- Redis | 一文轻松搞懂redis集群原理及搭建与使用
转载:https://juejin.im/post/5ad54d76f265da23970759d3 作者:SnailClimb 这里总结一下redis集群的搭建以便日后所需同时也希望能对你有所帮助. ...
- linux下的什么工具能将DVI文件转换成PostScript文件?
答: dvips,此工具能将由Latex或Tex生成的DVI文件转换成PostScript文件,官网在此
- 自己动手写Android插件化框架
自己动手写Android插件化框架 转 http://www.imooc.com/article/details/id/252238 最近在工作中接触到了Android插件内的开发,发现自己这种技 ...
- shared pointer
#include <string>#include <fstream>#include <memory>#include <cstdio> class ...
- Windows10 64位 安装 Docker
本文主要介绍Docker在Windows下的安装.关于Docker的介绍和文档在其官网中可以找到:http://www.docker.com .安装环境:Windows10 64位. Docker引擎 ...
- Spark中的CombineKey()详解
CombineKey()是最常用的基于键进行聚合的函数,大多数基于键聚合的函数都是用它实现的.和aggregate()一样,CombineKey()可以让用户返回与输入数据的类型不同的返回值.要理解C ...