题目描述

Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him? 
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.

输入

The input begins with the number n of scenarios on a single line by itself. 
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.

输出

For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The distance must be written on a single line.

样例输入

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的更多相关文章

  1. Knight Moves(广搜BFS)

    Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to ...

  2. HDU 1372 Knight Moves (广搜)

    题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...

  3. 一道简单的广搜题:Knight Moves

    这本来是要用双向宽度搜索的,但是我用简单的广搜也成功了,L<=300,也不会超时?? 另外一个问题就是,我本来想用原来的代码交,结果80分??将边界条件从小于L改成小于等于L,就对了.我可能不会 ...

  4. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

  5. [宽度优先搜索] HDU 1372 Knight Moves

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  6. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  7. 【POJ 2243】Knight Moves

    题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...

  8. POJ 2243 Knight Moves

    Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13222   Accepted: 7418 Des ...

  9. HDOJ/HDU 1372 Knight Moves(经典BFS)

    Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...

随机推荐

  1. CF1208D

    CF1208D 题意: 给你一个数组,要求支持单点修改和单点查询 解法: 直接线段树搞一搞就没了. CODE: #include<iostream> #include<cstdio& ...

  2. hystrix(一) 简单使用, 以及动态配置更新

    本文转载自https://my.oschina.net/u/1169457/blog/1787414 hystrix 简单使用, 以及动态配置更新 概述 只介绍同步模式下简单的使用, 有助于快速接入, ...

  3. GPS模块编程之NMEA0183协议

    NMEA 0183是美国国家海洋电子协会(National Marine Electronics Association)为海用电子设备制定的标准格式.现在已经成为GPS导航设备统一的RTCM(Rad ...

  4. 大数据(2)---HDFS集群搭建

    一.准备工作 1.准备几台机器,我这里使用VMware准备了四台机器,一个name node,三个data node. VMware安装虚拟机:https://www.cnblogs.com/niju ...

  5. 本地文件上传到Linux服务器

    1.从服务器上下载文件scp username@servername:/path/filename /var/www/local_dir(本地目录) 例如scp root@192.168.0.101: ...

  6. selenium+python自动化测试-环境搭建

    firefox浏览器打不开的解决办法: 1.确认将geckodriver拷贝到Firefox安装目录 2.将安装目录添加到Windows的path里 3.重启IDE  

  7. android之Framework问题总结:

    移动开发知识体系总章(Java基础.Android.Flutter) Android Handler消息机制 . Android中为什么主线程不会因为Looper.loop里的无限循环ANR? 1.1 ...

  8. Hadoop 3.1.3伪分布式环境安装Hive 3.1.2的异常总结

    背景:hadoop版本为3.1.3, 且以伪分布式形式安装,hive版本为3.1.2,hive为hadoop的一个客户端. 1. 安装简要步骤 (1) 官网下载apache-hive-3.1.2-bi ...

  9. Vulnhub-XXE靶机学习

    ------------恢复内容开始------------ 前两天在微信公众号上看见了这个XXE靶场,就想试一试,虽然网上关于这个的文章已经写了太多太多了,但还是要写出来划划水,233333333, ...

  10. leetcode907 Sum of Subarray Minimums

    思路: 对于每个数字A[i],使用单调栈找到A[i]作为最小值的所有区间数量,相乘并累加结果.时间复杂度O(n). 实现: class Solution { public: int sumSubarr ...