POJ-1915 Knight Moves (BFS)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 26952 | Accepted: 12721 | 
Description
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.

Input
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.
Output
Sample Input
3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1
Sample Output
5
28
0
Source
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std; const int MAX=;
int l,sx,sy,ex,ey;
int dx[]={-,-,-,-,,,,};
int dy[]={,-,,-,,-,,-};
bool vis[MAX][MAX];
struct Node
{
int x,y,step;
bool operator < (const Node &a) const{
return a.step<step;
}
};
priority_queue<Node> q;//利用优先队列,每次出队的为步数较小的。 void bfs()
{
Node now,next;
now.x=sx; now.y=sy; now.step=;
while(!q.empty()) //由于多组数据,每次bfs都要清空q
{
q.pop();
}
q.push(now);
while(!q.empty())
{
now = q.top();
q.pop();
if(now.x==ex&&now.y==ey) //bfs结束,找到出口
{
printf("%d\n",now.step);
break;
}
for(int i=;i<;i++)
{
next.x = now.x+dx[i];
next.y = now.y+dy[i];
if(next.x>=&&next.x<l &&next.y>=&&next.y<l && !vis[next.x][next.y])
{
vis[next.x][next.y]=true;
next.step = now.step+;
q.push(next);
}
}
}
} int main()
{
int Case;
scanf("%d",&Case);
while(Case--)
{
memset(vis,false,sizeof(vis));
scanf("%d",&l);
scanf("%d %d",&sx,&sy);
scanf("%d %d",&ex,&ey);
bfs();
//printf("bfs-end\n");
}
}
POJ-1915 Knight Moves (BFS)的更多相关文章
- POJ 1915 Knight Moves(BFS+STL)
		
 Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20913 Accepted: 9702 ...
 - POJ 1915  Knight Moves
		
POJ 1915 Knight Moves Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29 ...
 - POJ 2243 Knight Moves(BFS)
		
POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...
 - OpenJudge/Poj 1915 Knight Moves
		
1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...
 - POJ 2243 Knight Moves
		
Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13222 Accepted: 7418 Des ...
 - (step4.2.1) hdu 1372(Knight Moves——BFS)
		
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
 - UVA 439  Knight Moves(BFS)
		
Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...
 - HDU 1372 Knight Moves(BFS)
		
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
 - HDU1372:Knight Moves(BFS)
		
Knight Moves Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
 - hdu1372 Knight Moves BFS 搜索
		
简单BFS题目 主要是读懂题意 和中国的象棋中马的走法一样,走日字型,共八个方向 我最初wa在初始化上了....以后多注意... 代码: #include <iostream> #incl ...
 
随机推荐
- MySQL 5.7 在windows下修改max_allowed_packet变量
			
(一)执行sql遇到的错误如下: ### Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (387 ...
 - 14.什么是jsp动作
			
JSP动作元素(action elements),动作元素为请求处理阶段提供信息.动作元素遵循XML元素的语法,有一个包含元素名的开始标签,可以有属性,可选的内容,与开始标签匹配的结束标签. 包含的类 ...
 - mac下git配置和jenkins打包
			
今天自己配置了下jenkins,还算是比较顺利,把整个过程和大家分享下. 1. 查看秘钥是否存在 打开终端查看是否已经存在SSH密钥:cd ~/.ssh 如果没有密钥则不会有此文件夹,有则备份删除, ...
 - linux新手入门前知道的一些概念
			
前言: 这篇文章是结合自己从小白开始学linux到工作中运用linux系统,以新手怎么理解的角度来说说linux系统,希望能给想学习linux的新手带来一些帮助. 引子:随着互联网技术不断更新,企业对 ...
 - awvs 中文手册详细版
			
目录: 0×00.什么是Acunetix Web Vulnarability Scanner ( What is AWVS?) 0×01.AWVS安装过程.主要文件介绍.界面简介.主要操作区域简介(I ...
 - 【ESP8266】发送HTTP请求
			
一.ESP8266简介 ESP8266 是深圳安信可科技有限公司开发的基于乐鑫ESP8266的超低功耗的UART-WIFI模块的模组,可以方便进行二次元开发,接入云端服务,实现手机3/4G全球随时随地 ...
 - mysql启动关闭的批处理,感觉很好用在其他论坛帖子上找到的,感谢分享
			
最近用mysql的时间比较多,每次都在计算机管理工具下面去启动,感觉很麻烦,于是搜索了下果然有前辈已经做出了这些东西,今天收藏整理,mysql启动关闭的批处理感觉很好用在其他论坛帖子上找到的,感谢互联 ...
 - MyBatis源码解析【4】反射和动态代理
			
通过之前的介绍,我们了解了几个组件的生命周期. 它也是我们重要装备之一. 今天我们需要搞一件更加强的装备,叫做反射和动态代理. 如果没有这件装备的话,显然后面的源码boss是打不动的. 顺便说一下,下 ...
 - 浅析CQRS的应用部署
			
CQRS,中文翻译命令和查询职责分离,它是一种架构,不仅可以从数据库层面实现读写分离,在代码层面上也是推荐读写分离的.在接口上可以更为简单 命令端定义 ICommandResult Execute(I ...
 - Linux下mysql远程连接问题
			
[旧博客转移 - 发布于2015年9月6日 18:42] 在Linux上装好Mysql后 发现远程客户端连接不上 出现这种情况是因为mysql服务器出于安全考虑,默认只允许本地登录数据库服务器. ...