题目描述

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. docker数据持久化

    转载/参考: https://www.jianshu.com/p/ef0f24fd0674 Docker的数据持久化主要有两种方式: bind mount docker managed volume ...

  2. 谷歌浏览器试调网页 多出font标签

    突然发现一些按钮的点击功能失效,在控制台发现该a标签中多出个font标签,导致文字区域不能触发到a标签 就算a标签宽高设置百分百 也没用. 经测试不同的浏览器情况不一样 safari就不会出现这种情况 ...

  3. python将py文件转换为pyc

    python -m py_compile lib/ylpy.py python -m py_compile lib/ylpy.py python 一个.py文件如何调用另一个.py文件中的类和函数 A ...

  4. 多位IT专家分享他们离不开的实用工具

    本文的 PDF版本可供下载. #1: John Bartow,顾问 John Bartow的工作领域是网络和PC安全,他提供了自己从事的咨询公司, WinHaven Computer Consulti ...

  5. 014-操作系统下验证下载文件的 MD5/SHA1/SHA256

    一.mac 1.md5 openssl md5 /path/to/file 新的macOS默认支持:md5 filename 2.sha256 openssl dgst -sha256 /path/t ...

  6. Oracle关联删除的几种方式

    不多说了,我们来做实验吧. 创建如下表数据 select * from t1 ; select * from t2; 现需求:参照T2表,修改T1表,修改条件为两表的fname列内容一致. 方式1,u ...

  7. 独立的js文件中不能使用EL表达式取值

    在独立的js文件中写了一个EL表达式取值,发现没有取到值,原因在于不能在独立的js文件中使用EL表达式,可以在jsp页面定义全局变量,然后在js文件中引用

  8. Java操作excel_导出与读取(导入)

    我自己的jar包下载 一.介绍 常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际的开发中,很多时候需要实现导入.导 ...

  9. checklist和基础安全知识

    checklist和基础安全知识 https://book.yunzhan365.com/umta/rtnp/mobile/index.html 网络安全科普小册子 http://sec.cuc.ed ...

  10. 如何在mac上安装gitlab

    安装docker 下载地址:https://docs.docker.com/docker-for-mac/install/ 下载下来是一个dmg的安装包,直接安装就可以了. 网络问题 嗯,在国内做开发 ...