【广搜】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 ...
随机推荐
- mongodb高可用部署linux
准备三台服务器,部署方案如下: 1.安装mongodb,详细不用说: 2.创建实例分别创建目录config.configServer.shard1.shard2.shard3.logs,分别存放实例配 ...
- 理解了这些异常现象才敢说真正懂了TCP协议
很多人总觉得学习TCP/IP协议没什么用,觉得日常编程开发只需要知道socket接口怎么用就可以了.如果大家定位过线上问题就会知道,实际上并非如此.如果应用在局域网内,且设备一切正常的情况下可能确实如 ...
- CentOS6.8安装Docker
在CentOS6.8上安装Docker 1.Docker使用EPEL发布,RHEL系的OS首先要确保已经持有EPEL仓库,否则先检查OS的版本,然后安装相应的EOEL包:如下命令: yum insta ...
- powerdesigner导出rtf
报告--reports--新建reports--reports template选择None 从左侧的Available items 拖动 需要导出的item到右侧 右侧的每一个item右键 可以设置 ...
- Flutter移动电商实战 --(25)列表页_使用Provide控制子类-1
主要是二级分类的UI布局 生成我们的右侧动态类 定义list变量 开始写里面的子项,把每一个小的写了 再拼成一个大的 这样我们的小类就写完了 开始写我的大类别:是一个横向的ListView.写横向的L ...
- 记录linux 生成crash dump文件步骤
执行文件编译时加入-g 命令 例如 g++ -g test.cpp 查看当前系统限制情况 ulimit -a 设置crash dump 文件大小 ulimit -c unlimited unlimit ...
- 使用pycharm发布python程序到ubuntu中运行
前提条件: 1.ubuntu安装了vsftpd,可以参考:https://www.cnblogs.com/xienb/p/9322805.html 2.安装专业版pycharm 步骤: 1.新建一个P ...
- C++ STL partial_sort_copy greater
#include <iostream>#include <deque>#include <algorithm>#include <vector>#inc ...
- 动手生成 Delphi xe DBTreeview
tProductType表结构如下 object FDConnection1: TFDConnection Params.Strings = ( 'Database=ClothingT ...
- JavaScript 异步和单线程
JavaScript语言本身是单线程的,所以它自身不可能是异步.所谓单线程,就必然意味着:所有任务需要排队,前一个任务结束,才会执行后一个任务. 但js的宿主环境(比如浏览器,Node)是多线程的.宿 ...