poj1915 BFS
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u
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
代码;
/*
简单BFS,寻找最短路径长度
*/
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
const double eps=1e-8;
const double pi=acos(-1.0);
int m[305][305];//用来标记,避免重复,bfs常见剪枝
int ax,ay,bx,by;
struct nod
{
int x,y;
int step;
};
int f[8][2]={{-2,1},{-2,-1},{-1,2},{-1,-2},{1,2},{1,-2},{2,1},{2,-1}};
queue<nod> q;
int ans;
int l;
void bfs()
{
nod t;
while(!q.empty())
{
t=q.front();
q.pop();
if(t.x==bx&&t.y==by)
{
ans=t.step;
return;
}
for(int i=0;i<8;i++)
{
int xx=t.x+f[i][0];
int yy=t.y+f[i][1];
nod n;
n.x=xx,n.y=yy,n.step=t.step+1;
if(xx>=0&&xx<l&&yy>=0&&yy<l&&m[n.x][n.y]==0)
{q.push(n);m[n.x][n.y]=1;}//在放入队列时就要标记,而不是吧每一次对队列头部进行标记,我又sb了
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&l);
memset(m,0,sizeof(m));
scanf("%d%d%d%d",&ax,&ay,&bx,&by);
nod k;
k.x=ax,k.y=ay,k.step=0;
q.push(k);
m[ax][ay]=1;
bfs();
cout<<ans<<endl;
while(!q.empty())
q.pop();//对于有多组数据情况,一定要记得清空队列
}
return 0;
}
poj1915 BFS的更多相关文章
- POJ1915 BFS&双向BFS
俩月前写的普通BFS #include <cstdio> #include <iostream> #include <cstring> #include <q ...
- poj1915 Knight Moves(BFS)
题目链接 http://poj.org/problem?id=1915 题意 输入正方形棋盘的边长.起点和终点的位置,给定棋子的走法,输出最少经过多少步可以从起点走到终点. 思路 经典bfs题目. 代 ...
- poj1915(双向bfs)
题目链接:https://vjudge.net/problem/POJ-1915 题意:求棋盘上起点到终点最少的步数. 思路:双向广搜模板题,但玄学的是我的代码G++会wa,C++过了,没找到原因QA ...
- POJ-1915 Knight Moves (BFS)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 26952 Accepted: 12721 De ...
- BFS广度优先搜索 poj1915
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25909 Accepted: 12244 Descri ...
- 超超超简单的bfs——POJ-1915
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 26102 Accepted: 12305 De ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
随机推荐
- ZOJ3558 How Many Sets III(公式题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud How Many Sets III Time Limit: 2 Seconds ...
- Implement Insert and Delete of Tri-nay Tree
问题 Implement insert and delete in a tri-nary tree. A tri-nary tree is much like a binary tree but wi ...
- Bootstrap3 入门实战
因为公司选择了使用BootStrap3作为项目的前台展示框架,所以花了半天时间来学习Bootstrap, 如果你是第一次听说,或者说以前听说过,但没有使用过这个框架的话,希望这篇入门实战能够让你快速掌 ...
- php配置文件php.ini 中文版
;;;;;;;;;;;;;;;; 简介 ;;;;;;;;;;;;;;;;; 本文并非是对英文版 php.ini 的简单翻译,而是参考了众多资料以后,结合自己的理解,增加了许多内容,; 包括在原有 ph ...
- 【Android纳米学位】project 0 - 问题汇总
1.页面布局 参考:http://www.xuebuyuan.com/1100763.html 从不知道如何下手到开始布局出想要的样子,使用线性布局及属性 margin,padding 2.添加点击事 ...
- Qt程序Windows部署前打包方法
1.需求 很多公司都在使用Qt作为GUI库,在开发者的计算机上通常是利用配置PATH环境变量来实现调用Qt的DLL.然而当把开发后的软件直接部署在顾客方,顾客的计算机上并没有Qt的bin目录,所以并不 ...
- directive 指令
参考文章 : http://www.zouyesheng.com/angular.html#toc20 18. 自定义指令directive http://blog.jobbole.com/62 ...
- Qt跨线程信号和槽的连接(默认方式是直连和队列的折中)
Qt支持三种类型的信号-槽连接:1,直接连接,当signal发射时,slot立即调用.此slot在发射signal的那个线程中被执行(不一定是接收对象生存的那个线程)2,队列连接,当控制权回到对象属于 ...
- 必须用C模拟OS?
ASM基本必要,至于高级语言就很难说了.去osdev wiki上一翻一堆各种语言实现的玩意. 一个模拟OS其实不太容易完整搭出来,反倒是直接构造内核的后顾之忧少(如果还有真的想在SIGALRM里耍什么 ...
- BZOJ1123: [POI2008]BLO
1123: [POI2008]BLO Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 614 Solved: 235[Submit][Status] ...