题目链接: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. [转]PHP 汉字转拼音

    转自: https://git.oschina.net/wapznw/php-pinyin <?php /** * @package default * @copyright php-pinyi ...

  2. OpenWRT - WEB界面开发思路和基本方法

    想要对OpenWRT的WEB界面(*下称界面)进行修改.修改的目标是: 1.修改页面的样式,设计为企业的风格(stylesheet) 2.新建自己的功能,实现 访问页面后,用户就可以对配置文件(也就是 ...

  3. hdu4753 Fishhead’s Little Game 状态压缩,总和一定的博弈

    此题和UVA 10891 Game of Sum 总和一定的博弈,区间dp是一个道理,就是预处理麻烦 这是南京网络赛的一题,一直没做,今天做了,虽然时间有点长,但是1ac,这几乎是南京现场赛的最后一道 ...

  4. go语言之进阶篇多任务资源竞争问题

    1.多任务资源竞争问题 示例: package main import ( "fmt" "time" ) //定义一个打印机,参数为字符串,按每个字符打印 // ...

  5. 轻松搞定 easyui datagrid 二次加载的问题(转)

    对于使用url方式的初学者,经常碰到重复请求的问题,这个问题的根源是因为一旦设置了url参数,Datagrid组件在实例化的时候就会做请求,如何避免二次加载这样问题呢,个人觉得注意以下两点基本就可以防 ...

  6. fatal error LNK1104: 无法打开文件“libc.lib”的问题 (转)

    今天,编译程序的时候,意外遇到了一个错误,就是VS2008一直提示:fatal error LNK1104: 无法打开文件“libc.lib”,后来在网上查找了很多资料,终于知道原因了... 如果将用 ...

  7. [leetcode]Restore IP Addresses @ Python

    原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...

  8. iOS开发-消息通知机制(NSNotification和NSNotificationCenter)

    iOS中委托模式和消息机制基本上开发中用到的比较多,一般最开始页面传值通过委托实现的比较多,类之间的传值用到的比较多,不过委托相对来说只能是一对一,比如说页面A跳转到页面B,页面的B的值改变要映射到页 ...

  9. 浅谈Kmeans聚类

    http://www.cnblogs.com/easymind223/archive/2012/10/30/2747178.html 聚类分析是一种静态数据分析方法,常被用于机器学习,模式识别,数据挖 ...

  10. Eclipse里选一个变量后,这个类里的该变量不变色了

    使用“Alt+Shift+O”对该提示功能的开/关切换