POJ1915(Knight Moves)_NENUOJ(移动的骑士)_C++版@FDDLC

POJ1915:http://poj.org/problem?id=1915

NENUOJ:http://47.106.114.75/contest/21/problem/A

 

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. 

《POJ1915(Knight Moves)_NENUOJ(移动的骑士)_C++版@FDDLC》

 

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 <queue> //用到了队列
#include<string.h> //用到了memset
using namespace std; //C++才有队列,C没有。C++要使用命名空间

#define EDGE_MAX 300 //最大边长
int is_visited[EDGE_MAX][EDGE_MAX];

//用结构体来记录当前位置的坐标以及从起点移动到当前位置所用的步数
struct x_y_step
{
    int x, y, step;
};

//从当前位置到下一个位置共有8种走法,这里用x和y的增量来表示
int delta_x_delta_y[8][2] =
{
    {-2, -1},
    {-1, -2},
    { 1, -2},
    { 2, -1},
    { 2,  1},
    { 1,  2},
    {-1,  2},
    {-2,  1}
};

int out_bound_check(x_y_step next_postion, int edge) //检查是否越界
{
    if(next_postion.x < 0 || next_postion.x >= edge || next_postion.y < 0 || next_postion.y >= edge)
    {
        return 1;
    }
    return 0;
}

int is_visited_check(x_y_step postion) //检查是否已被访问
{
    return is_visited[postion.x][postion.y];
}

//检查是否到达终点
int has_arrived_check(int current_position_x, int current_position_y, int target_x, int target_y)
{
    if(current_position_x == target_x && current_position_y == target_y)
        return 1;
    return 0;
}


void is_visited_set(x_y_step position) //将该位置标记为已访问状态
{
    is_visited[position.x][position.y] = 1;
}

//该算法主要使用了宽度优先的思想,故使用队列
//宽度优先搜索有这么一个特点:若找到了一个解,则该解一定是最小的
int search_step(int edge, int source_x, int source_y, int target_x, int target_y)
{
    memset(is_visited, 0, sizeof(is_visited)); //所有元素初始化为0,表示未被访问,每次都得初始化!!!

    queue<x_y_step> position_queue;
    x_y_step source = {source_x, source_y, 0}; //起点
    position_queue.push(source); //将起点入队

    while(!position_queue.empty())
    {
        x_y_step current_position = position_queue.front(); //将队首元素设为当前位置

        //检查是否到达终点
        if(has_arrived_check(current_position.x, current_position.y, target_x, target_y))
            return current_position.step;

        //若没被访问过,才有必要扩展子结点
        if(is_visited_check(current_position) == 0)
            for(int delta_index = 0; delta_index < 8; delta_index++) //向8个方向扩展子结点
            {
                x_y_step next_position = current_position;
                next_position.x += delta_x_delta_y[delta_index][0];
                next_position.y += delta_x_delta_y[delta_index][1];
                next_position.step++;

                //若子结点越界,则舍去(即不入队)
                //若子结点不越界,则检查是否已被访问过;若被访问过,亦舍去
                if(out_bound_check(next_position, edge) == 1 || is_visited_check(next_position) == 1 )
                    continue;  //跳过本次循环,不执行下面的入队代码


                position_queue.push(next_position);
            }

        is_visited_set(current_position); //当前结点已被扩展完,将其标记为已访问
        position_queue.pop(); //当前结点已被扩展完,出列,以便扩展下一个结点
    }


}

int main()
{
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int case_number;
    scanf("%d", &case_number);

    while(case_number--)
    {
        int edge, source_x, source_y, target_x, target_y;
        scanf("%d%d%d%d%d", &edge, &source_x, &source_y, &target_x, &target_y);
        printf("%d\n", search_step(edge, source_x, source_y, target_x, target_y));
    }


    return 0;
}

 

    原文作者:骑士周游问题
    原文地址: https://blog.csdn.net/liuxc324/article/details/85132878
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞