POJ 1915 Knight Moves (廣度搜索)

Knight Moves
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 19803   Accepted: 9150

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

Source

TUD Programming Contest 2001, Darmstadt, Germany


題意:
給你棋盤的邊寬以及騎士的起始位置和終點位置,需要求出按照騎士的下法,從起始位置最快走幾步能到達終點位置。
思路:
經典的廣度搜索題

/*************************************************************************
	> File Name: poj1915.cpp
	> Author: BSlin
	> Mail:  
	> Created Time: 2013年09月25日 星期三 19時09分03秒
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iterator>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#define MP make_pair
#define INF (1<<30)
#define PI acos(-1.0)
#define esp 1e-8
const int dx[4]={0,0,0,0};
using namespace std;
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#if defined (_WIN32) || defined (__WIN32) || defined (WIN32) || defined (__WIN32__)
#define LL __int64
#define LLS "%" "I" "6" "4" "d"
#else
#define LL long long
#define LLS "%" "l" "l" "d"
#endif


struct node {
    int x,y,step;
}p1,p2;

int to[8][2] = {1,2,2,1,2,-1,1,-2,-1,-2,-2,-1,-2,1,-1,2};
int stx,sty,enx,eny,vis[320][320],l;

bool inmap(int x, int y) {
    if(x >= 0 && x < l && y >= 0 && y < l) return true;
    return false;
}

int bfs() {
    queue<node>qu;
    memset(vis,0,sizeof(vis));
    p1.x = stx;
    p1.y = sty;
    p1.step = 0;
    qu.push(p1);
    while(!qu.empty()) {
        p2 = qu.front();
        qu.pop();
        if(p2.x == enx && p2.y == eny) return p2.step;
        for(int i=0; i<8; i++) {
            p1.x = p2.x + to[i][0];
            p1.y = p2.y + to[i][1];
            if(!inmap(p1.x,p1.y) || vis[p1.x][p1.y]) continue ;
            p1.step = p2.step + 1;
            vis[p1.x][p1.y] = true;
            qu.push(p1);
        }
    }
}

int main(int argc, char** argv) {
    //read;
    int n,cnt;
    scanf("%d",&n);
    while(n--) {
        scanf("%d%d%d%d%d",&l,&stx,&sty,&enx,&eny);
        printf("%d\n",bfs());
    }
    return 0;
}




發佈了45 篇原創文章 · 獲贊 6 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章