POJ 1915 Knight Moves(BFS+STL)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 20913 | Accepted: 9702 |
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
distance must be written on a single line.
Sample Input
3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1
Sample Output
5
28
0
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define maxn 350
using namespace std; int n;
int x1,x2,y1,y2; struct node{
int x;
int y;
int sum;
node(int a,int b,int c)
{
x=a;
y=b;
sum=c;
} }; int go[8][2]={{1,2},{1,-2},{2,1},{2,-1},{-1,2},{-1,-2},{-2,1},{-2,-1}};
int vis[310][310]; int BFS()
{
queue<node> que;
memset(vis,0,sizeof vis);
que.push(node(x1,y1,0));
vis[x1][y1]=1;
while(!que.empty())
{
node temp=que.front();
for(int i=0;i<8;i++)
{
int x0=temp.x+go[i][0];
int y0=temp.y+go[i][1];
int sum0=temp.sum+1;
if(x0>=0&&x0<n&&y0>=0&&y0<n)
{
if(x0==x2&&y0==y2) return sum0;
else if(!vis[x0][y0])
{
que.push(node(x0,y0,sum0));
vis[x0][y0]=1;
}
}
}
que.pop();
} return 0; } int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d%d",&n,&x1,&y1,&x2,&y2); if(x1==x2&&y1==y2)
printf("0\n");
else
printf("%d\n",BFS());
} return 0; }
版权声明:本文博客原创文章,博客,未经同意,不得转载。
POJ 1915 Knight Moves(BFS+STL)的更多相关文章
- 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 ...
随机推荐
- 实现ListView A~Z快速索引
ListView A~Z快速索引这种效果在通信录和城市列表中经常看到,方便用户查找,是一种增加用户体验的好方法. 实现步骤: 1.自定义一个名叫SlideBar 的View. 2.在布局文件中加入这个 ...
- [LeetCode]Pascal's Triangle II
题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...
- hdu1166(线段树)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 线段树功能:update:单点增减 query:区间求和 #pragma comment(lin ...
- 登录oracle时,scott is locked (帐户被锁定) 的解决方法
登录Oracle时,用scott/tiger 通常此时会报一个错误: scott is locked (帐户被锁定) 现在就要用超级用户system将scott帐户进行解锁. cmd->sql ...
- POJ1470 Closest Common Ancestors 【Tarjan的LCA】
非常裸的模版题,只是Tarjan要好好多拿出来玩味几次 非常有点巧妙呢,tarjan,大概就是当前结点和它儿子结点的羁绊 WA了俩小时,,,原因是,这个题是多数据的(还没告诉你T,用scanf!=EO ...
- Thinkphp中的volist标签(查询数据集(select方法)的结果输出)用法简介
参考网址:http://camnpr.com/archives/1515.html 通常volist标签多用于查询数据集(select方法)的结果输出,通常模型的select方法返回的结果是一个二维数 ...
- SVN的CheckOut操作和Export操作的区别
- vmware无法链接U盘:vm-->removeable devices.
vmware无法链接U盘:vm-->removeable devices.
- Google Earth数据存储、管理、表现及开发机制
Google Earth数据存储.管理.表现及开发机制 一. Google Earth(Map)介绍 1.1 Google Earth介绍 在众多的地理信息服务提供商中,Google是较早 ...
- mini2440裸试验—计算器(LCD显示,触摸屏突破)
关于Pait_Bmp(x0, y0, x, y, BMPaddr);函数 像素图在屏幕左上角为(0,0),Pait_Bmp中的x0.y0分别像素点初始位置,x,y为BMP图片的X,Y的大小.BMPad ...