题目链接:http://noi.openjudge.cn/ch0205/917/

原题应该是hdu 1372

总时间限制: 1000ms  内存限制: 65536kB
描述
Background
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?
The Problem
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

来源TUD Programming Contest 2001, Darmstadt, Germany

题目大意:

输入n表示有一个n*n的棋盘,输入开始坐标和结束坐标,问一个骑士朝着棋盘的8个方向走马字步,从起点到终点最少需要多少步。

首先输入T表示有T个测试样例,然后输入n表示棋盘规模,然后输出起点和终点的坐标(坐标从0开始到n-1)

输出马移动的最少步数,起点和终点相同则输出0.

算法分析:

这个题明显用广搜效率比较高。深搜的话,要搜索完所有路径然后才知道最优解。

 #include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std; struct obj
{
int xx,yy,step;
}; int T,n;
queue<struct obj> q;
struct obj S,E;
int used[][]; int dx[]={-,-,,,,,-,-};//???????????????????8????
int dy[]={,,,,-,-,-,-};
void BFS(); int main(int argc, char *argv[])
{
freopen("917.in","r",stdin);
scanf("%d",&T);
while(T)
{
T--;
scanf("%d",&n);
scanf("%d%d%d%d",&S.xx,&S.yy,&E.xx,&E.yy);
S.step=;
E.step=-; if(S.xx==E.xx&&S.yy==E.yy) printf("0\n");
else
{
memset(used,,sizeof(used));
BFS();
if(E.step==-) printf("no way!\n");
else printf("%d\n",E.step);
}
}
return ;
} void BFS()
{
int i,txx,tyy;
struct obj temp; while(!q.empty()) q.pop(); used[S.xx][S.yy]=;
q.push(S);
while(!q.empty())
{
for(i=;i<;i++)
{
txx=q.front().xx+dx[i];
tyy=q.front().yy+dy[i];
if(txx>=&&txx<n&&tyy>=&&tyy<n&&used[txx][tyy]==)
{
temp.xx=txx;
temp.yy=tyy;
temp.step=q.front().step+;
q.push(temp);
used[txx][tyy]=;
if(temp.xx==E.xx&&temp.yy==E.yy)
{
E.step=temp.step;
return;
}
}
}
q.pop();
}
}

917:Knight Moves的更多相关文章

  1. Knight Moves

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  2. HDU 1372 Knight Moves

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

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

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

  4. HDU 1372 Knight Moves (bfs)

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

  5. UVA 439 Knight Moves --DFS or BFS

    简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...

  6. 【POJ 2243】Knight Moves

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

  7. hdu Knight Moves

    这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕. #include <iostream> #include <stdio.h> #incl ...

  8. HDU 1372 (搜索方向稍有改变) Knight Moves

    其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...

  9. HDU 1372 Knight Moves【BFS】

    题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...

随机推荐

  1. Netty Associated -- ChannelPipeline

    A list of ChannelHandlers which handles or intercepts inbound events and outbound operations of a Ch ...

  2. Chapter 4 -- Throwables

    TODO: rewrite with more examples Guava's Throwables utility can frequently simplify dealing with exc ...

  3. Asp.Net MVC3 简单教程(三)详解Controller之Filter 【转】

    前言 前面两篇写的比较简单,刚开始写这个系列的时候我面向的对象是刚开始接触Asp.Net MVC的朋友,所以写的尽量简单.所以写的没多少技术含量.把这些技术总结出来,然后一简单的方式让更多的人很好的接 ...

  4. matlib常用知识

    把文件装入矩阵 x = load('ex4x.dat'); y = load('ex4y.dat'); [m, n] = size(x); %得到矩阵x的行数和列数 ex4x.dat共80行,2列,通 ...

  5. %date~0,4%和 %time~0,2%等用法详解(转)

    比如在windowscmd命令行窗口执行date命令后这个环境变量的值为当前日期:2014-09-01 星期六 或2014/09/01 周六那么如下的各个操作的意义如下:%date:~0,4% 表示从 ...

  6. macOS Sierra10.12.5 显示允许任何来源

    在终端输入:sudo spctl --master-disable即可.

  7. 前端Js框架汇总【转】

    概述: 有些日子没有正襟危坐写博客了,互联网飞速发展的时代,技术更新迭代的速度也在加快.看着Java.Js.Swift在各领域心花路放,也是煞是羡慕.寻了寻.net的消息,也是振奋人心,.net co ...

  8. Triangle leetcode java

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  9. centos 下 mysql+keepalived实现双主自由切换

    目录 ip规划 mysql双主配置 keepalived配置 mysql1中keepalived的配置 mysql2中keepalived的配置 VIP漂移检测 本文的目的是搭建一个互为主从的mysq ...

  10. DNS预解析dns-prefetch提升页面载入速度优化前端性能

    当浏览器请求一个URL的时候,通过firebug我们可以发现大概有以下几个过程:阻挡.域名解析.建立连接.发送请求.等待响应.接收数据.后面四个跟用户的网络情况和你的服务器处理速度有关,本文重点说说前 ...