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 ...
随机推荐
- Indiegogo: An International Crowdfunding Platform to Raise Money
Indiegogo: An International Crowdfunding Platform to Raise Money The world's funding platform. Fund ...
- SVNKIT操作SVN版本库的完整例子
Model: package com.wjy.model; public class RepositoryInfo { public static String storeUrl="http ...
- hdu1535(最短路)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1535 题意:给你一个源点,让你从这里派发n个学生去其余的n-1个站点去邀请人们去CSS,然后再返回CS ...
- hdu4151(二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4151 题意:找出比n小的没有重复数字的总个数,例如12以内11不符合,1~10都符合. 分析:直接利用 ...
- hdu-4833-Best-Financing(DP)
Best Financing Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- linux下golang
linux下golang的配置 linux下golang的配置 之前开发golang一直在windows下,今天在linux下试了一下 ,遇到一些梗,比如go 找不到 sync包.花了一小时全部解决, ...
- 安装了VS2010 sp1 后再安装ASP.NET MVC 3.0的问题(Final Result: Installation failed with error code: (0x80070643), "安装时发生严重错误 " (Ela)
原文:安装了VS2010 sp1 后再安装ASP.NET MVC 3.0的问题(Final Result: Installation failed with error code: (0x800706 ...
- Groovy常见语法汇总
一.groovy是什么 简单地说,Groovy 是下一代的java语言,跟java一样,它也运行在 JVM 中. 作为跑在JVM中的另一种语言,groovy语法与 Java 语言的语法很相似.同时,G ...
- HUST 1017(DLX)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=65998#problem/A 题意:求01矩阵的精确覆盖. DLX学习资料:ht ...
- css3进行截取
在css3出现之前,一般采用substring来进行截取,现在 不用js,纯css3也能进行截取了: text-overflow:clip | ellipsis 1.clip: 要在一定的高度内,配合 ...