poj1915 简单bfs

Background

Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him canmove 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 neededfor a knight to reach one point from another, so that you have the chance to befaster than Somurolov.

For people not familiar with chess, the possible knight moves are shown inFigure 1.

Input

The input begins with the number n of scenarios on asingle line by itself.

Next follow n scenarios. Each scenario consists of three lines containinginteger numbers. The first line specifies the length l of a side of the chessboard (4 <= l <= 300). The entire board has size l * l. The second andthird line contain pair of integers {0,
..., l-1}*{0, ..., l-1} specifying thestarting and ending position of the knight on the board. The integers areseparated by a single blank. You can assume that the positions are validpositions on the chess board of that scenario.

Output

For each scenario of the input you have to calculate theminimal amount of knight moves which are necessary to move from the startingpoint to the ending point. If starting point and ending point areequal,distance is zero. The 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

题目分析:求象棋中,“马”从一个点跳到另一个点的最少步数。用bfs求最短路径。

AC代码:

#include <iostream>

#include <cstring>

#include <cstdio>

#include <queue>

using namespace std;

bool vis[310][310];//标记数组

int n;

intdx[]={-1,-2,-2,-1,1,2,2,1},dy[]={2,1,-1,-2,-2,-1,1,2};//方向数组

struct P{//点类

int x,y;

};

struct Node{//队元素

Px;

int step;

};

int bfs(P a,P b){

memset(vis,false,sizeof(vis));

vis[a.x][a.y]=true;

Node now,next;

now.x=a;now.step=0;

queue<Node> q;//用队列实现bfs

q.push(now);

while(!q.empty()){

now=q.front();q.pop();

if(now.x.x==b.x && now.x.y==b.y){

return now.step;

}

for(int i=0;i<8;++i){

int xx=next.x.x=now.x.x+dx[i],yy=next.x.y=now.x.y+dy[i];

next.step=now.step+1;

if(xx>=0 && xx<n && yy>=0 && yy<n&& !vis[xx][yy]){

vis[xx][yy]=true;

q.push(next);

}

}

}

return 0;

}

int main()

{

int t;scanf("%d",&t);

while(t--){

scanf("%d",&n);

P a,b;

scanf("%d%d%d%d",&a.x,&a.y,&b.x,&b.y);

int ans=bfs(a,b);

printf("%d\n",ans);

}

return 0;

}

时间: 2024-10-13 20:55:27

poj1915 简单bfs的相关文章

POJ 3278 Catch That Cow --- 简单BFS

/* POJ 3278 Catch That Cow --- 简单BFS */ #include <cstdio> #include <queue> #include <cstring> using namespace std; const int maxn = 100005; bool visit[maxn]; int step[maxn]; int bfs(int n, int k){ if (n == k) return 0; memset(visit, 0, s

【POJ 3669 Meteor Shower】简单BFS

流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封锁).安全地带为永远不会被封锁的点. 简单bfs,开始WA在把平面空间上限当成300*300,但根据题目,这只是有流星雨撞击的范围.实际可走的空间理论上没上限,但分析可得,离原点最近的安全地带一定在(302,302)范围内,所以应可把数组至少开为303*303. 后来WA在把G[0][0]==1的情

LightOJ 1012 简单bfs,水

1.LightOJ 1012  Guilty Prince  简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define F(i,a,b) for (int i=a;i<=b;i++) using names

HDU 1548 A strange lift(Dijkstra,简单BFS)

题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数,然后是n个数分别代表第i层的移动范围.输出最少移动次数,若不可达,输出-1. 解题思路: 1.用Dijkstra算法,首先构建邻接矩阵,注意在构造时,要考虑i-k[i]<1和i+k[i]>n,i代表当前所在层. 1 #include<string.h> 2 #include<st

poj 1562 简单 bfs

// 简单 bfs #include <iostream>#include<fstream>using namespace std; char map[110][110];int flag[110][110];int qu[11000][2],qe,qs,m,n;int add[8][2]={-1,-1,  -1,0, -1,1,   0,-1,  0,1,  1,-1,    1,0,   1,1 }; void bfs(int r,int c){    int i,tr,tc;

POJ3185(简单BFS,主要做测试使用)

没事做水了一道POJ的简单BFS的题目 这道题的数据范围是20,所以状态总数就是(1<<20) 第一次提交使用STL的queue,并且是在队首判断是否达到终点,达到终点就退出,超时:(其实这里我是很不明白的,,TM状态总数就只有1e6怎么也不应该超时的,,,,只能说STL的queue的常数实在是太大,完全没法弄...) 1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <qu

逃脱 (简单BFS)

题目传送门 G逃脱  题目描述 这是mengxiang000和Tabris来到幼儿园的第四天,幼儿园老师在值班的时候突然发现幼儿园某处发生火灾,而且火势蔓延极快,老师在第一时间就发出了警报,位于幼儿园某处的mengxiang000和Tabris听到了火灾警报声的同时拔腿就跑,不知道两人是否能够逃脱险境? 幼儿园可以看成是一个N*M的图,在图中一共包含以下几种元素: “.”:表示这是一块空地,是可以随意穿梭的. “#”:表示这是一块墙,是不可以走到这上边来的,但是可以被火烧毁. “S”:表示men

POJ 1753 Flip Game 简单BFS

很简单的搜索题目,随便写. 题目链接 1 #include <stdio.h> 2 #include <string.h> 3 int st; 4 char s[10]; 5 int q[70000],vis[70000],front,tail; 6 const int dx[]={1,-1,0,0}; 7 const int dy[]={0,0,1,-1}; 8 int BFS() { 9 front=tail=0; 10 memset(vis,-1,sizeof(vis));

POJ - 2251 - Dungeon Master (简单BFS)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20450   Accepted: 7917 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled