BFS PKU 1915

渣渣的我做算法只能做水题啊。

又是一道BFS水题

代码虐我千百遍,我待代码如初恋。我对代码是真爱啊

Knight Moves

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 22041   Accepted: 10294

Description

Background

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

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

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

For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,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
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <math.h>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <iostream>
#include <deque>
using namespace std;
#define MAXN 300 + 5

struct node{
    int x;
    int y;
    int step;
};
int vis[MAXN][MAXN];
int n;
int fx,fy;
int xx[8] = {1,2,2,1,-1,-2,-2,-1};
int yy[8] = {2,1,-1,-2,-2,-1,1,2};

void BFS(int a,int b){
    queue<node>q;
    int i,j;
    int dx,dy;
    node front,rear;
    front.x = a;
    front.y = b;
    front.step = 0;
    vis[a][b] = 1;
    q.push(front);
    while(!q.empty()){
        front = q.front();
        q.pop();
        if(front.x==fx && front.y==fy){
            printf("%d\n",front.step);
        }
        for(i=0;i<8;i++){
            dx = front.x + xx[i];
            dy = front.y + yy[i];
            if(dx>=0&&dx<n&&dy>=0&&dy<n){
                if(vis[dx][dy] == 0){
                    rear.x = dx;
                    rear.y = dy;
                    rear.step = front.step+1;
                    vis[dx][dy] = 1;
                    q.push(rear);
                }
            }
        }
    }
}

int main(){
    int x1,y1;
    //int y1,y2;
    int T;

    while(~scanf("%d",&T)){
        while(T--){
            memset(vis,0,sizeof(vis));
            scanf("%d",&n);
            scanf("%d%d",&x1,&y1);
            scanf("%d%d",&fx,&fy);
            BFS(x1,y1);
        }
    }

    return 0;
}

时间: 2024-07-29 06:18:29

BFS PKU 1915的相关文章

BFS PKU 3278

一道很简单的,典型的BFS. 代码虐我千百遍,我待代码如初恋 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 47366   Accepted: 14869 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He star

poj 1915 双向 BFS 利用数组 a[x][y] = a[cp.x][cp.y] + 1; b[x][y] = b[cp.x][cp.y] + 1;保留步数

#include<iostream>#include<queue> using namespace std; struct point{    int x, y;};point bufa[8] ={    {-2, 1}, {-1, 2}, {1, 2}, {2, 1},    {2, -1}, {1, -2}, {-1, -2}, {-2, -1}}; int n, a[305][305], b[305][305]; int rule(int x,int y)//判断是否符合棋盘

poj 1915 BFS 利用 pre 计算步数------------------路径

// BFS #include <stdio.h> #include <string.h> int visited[301][301]; // visited 已经访问过了 int dic[8][2]={{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}}; int head,end,n,ex,ey,sx,sy; struct quene { int x,y,pre; // pre 前一个结点 }q[100000]; vo

POJ 1915 Knight Moves(BFS+STL)

 Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fa

PKU 1562/HDU 1241 Oil Deposits(原油有多少块区域---BFS,DFS)

Oil Deposits Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region o

poj 1915 Knight Moves (bfs搜索)

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21919   Accepted: 10223 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast

POJ 1915 Knight Moves [BFS]

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26844   Accepted: 12663 Description Background  Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fas

poj 1915 Knight Moves 【双向bfs】

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22121   Accepted: 10332 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast

poj 1915 KnightMoves(bfs)

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24094   Accepted: 11364 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast