【广搜】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 ...
随机推荐
- Hadoop环境搭建|第二篇:hadoop环境搭建
硬件配置:1台NameNode节点.2台DataNode节点 一.Linux环境配置 这里我只配置NameNode节点,DataNode节点的操作相同. 1.1.修改主机名 命令:vi /etc/sy ...
- puppeteer注入cookie然后访问页面
var puppeteer = require('puppeteer'); const devices = require('puppeteer/DeviceDescriptors'); const ...
- Java synchronized和Lock
Synchronized 1. 将synchronized加在方法上, 即可实现对此方法的同步 public synchronized void deposit(float amt) { float ...
- 以太坊Geth通过私钥导入新地址到钱包步骤
Open TextEdit Paste key into TextEdit without any extra characters or quotations Save the file as pk ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
- [Java复习] Spring 常见面试问题
1. 什么是 Spring 框架?Spring 框架有哪些主要模块? 轻量级实现IoC和AOP的JavaEE框架. Core模块: bean(bean定义创建解析), context(环境, IoC容 ...
- mysql 调优 (转)
原文:https://mp.weixin.qq.com/s__biz=MzI4NTA1MDEwNg==&mid=2650763421&idx=1&sn=2515421f09c1 ...
- java+断点续传
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...
- nginx rewrite正则子组最多匹配到$9
nginx rewrite正则匹配()匹配子组最多匹配到$9,就是从$0到$9 当需要匹配更多子组时,可通过变量来实现 if ($uri ~ ^/forum-15/sortid-74/(.*?)(la ...
- golang的下载与安装
golang的官网可能由于政策原因登陆不上. 所以可以到Go语言中文网下载:https://studygolang.com/dl 我下载的是go1.10.3.windows-amd64.msi安装包, ...