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 ...
随机推荐
- Libevent使用样例,从简单到复杂
转载请注明出处:http://blog.csdn.net/luotuo44/article/details/39670221 本文从简单到复杂.展示怎样使用libevent.网上的很多 ...
- [Unity3D]Unity3D游戏开发之ACT游戏三连击效果实现综述
各位朋友,大家好,我是秦元培,欢迎大家关注我的博客,我的博客地址是blog.csdn.net/qinyuanpei.在研究了Unity3D Mecanim动画系统的重定向特性后,今天我们继续来探索Me ...
- android关于实现滑动界面
首先要说的是,滑动界面,我们需要一个以上的view切换,实际上可以使用ArrayList<View> pageViews要保存view信息,然后切换 LayoutInflater infl ...
- Wix打包系列 (六)制作升级和补丁包
原文:Wix打包系列 (六)制作升级和补丁包 前面我们已经知道怎么制作一个完整安装包了,但我们的软件往往不能一次性就满足客户的需要,当客户需要我们给软件进行升级的时候,我们应该怎么做呢? 在这之前,我 ...
- poj3613(恰经过N条边的最短路)
题目连接:http://poj.org/problem?id=3613 题意:从S 到 T 经过边得个数恰为k的最短路是多少. 分析:01邻接矩阵A的K次方C=A^K,C[i][j]表示i点到j点正好 ...
- Struts2 拦截器具体配置过程
拦截器差点儿遍布每个程序中,所以贴出拦截器配置的具体过程,希望可以帮到大家. Struts2 拦截器具体配置过程 <interceptors> <!-- 先定义拦截器 --> ...
- js:进一步关闭(范围:下一个)
function fn1(){ //创建一个数组 var fns = new Array(); //i这个变量是保存在fn1这个作用域中 for(var i=0;i<10;i++ ...
- GString及IntelliJIdea中调试Groovy的操作步骤
今天是学习Groovy的第一天,首先我觉得学习任何一种语言都要先弄清楚这种语言的特性,因为只有了解了特性之后学习才能达到好的效果,那么groovy的特点是什么的.我觉得groovy是一种动态语言,动态 ...
- eclipse安装ADT后在windows菜单下找不到android SDK and AVD Manager选项的解决办法
在eclipse中点击window→Customize Perspective→Command Groups availability→Available command groups下勾选Andro ...
- Operand should contain 1 column(s)
今天sql当测试发现错误:Operand should contain 1 column(s). 因为in背后有多种条件字段,in只有有背后场.