Poj 1915 knight moves [bidirectional BFS]

Source: Internet
Author: User

Knight moves
Time limit:1000 ms   Memory limit:30000 K
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. 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

380 07 01000 030 50101 11 1

Sample output

5280

Analysis: the most basic BFs, no one-way wide search, learned how to use two-way wide search.

Here is my opinion...

For questions that can be searched in two ways, you must first have the start point and the target point, and then start from the start and the target at the same time to perform one-way search separately, you know that the search in one direction hits another ..

# Include <stdio. h> # include <queue> # include <string. h> # define M 305 using namespace STD; int dis1 [m] [m], dis2 [m] [m], Len; const int DX [] = {1, 1, -1,-1, 2, 2,-2,-2}; // direction const int dy [] = {2,-2, 2,-2, 1, -1, 1,-1}; struct node {int X, Y;} St, en; int limit (node A) {return (. x> = 0 &. x <Len &. y> = 0 &. Y <Len);} void dobfs () {int I, size; dis1 [st. x] [st. y] = dis2 [en. x] [en. y] = 0; queue <node> p, q; q. push (S T); p. Push (en); While (! Q. Empty ()&&! P. empty () {size = Q. size (); While (size --) {node cur = Q. front (); q. pop (); If (limit (cur) & dis2 [cur. x] [cur. y]! =-1) {// printf ("% d .. 1 \ n ", dis1 [cur. x] [cur. y], dis2 [cur. x] [cur. y]); printf ("% d \ n", dis1 [cur. x] [cur. y] + dis2 [cur. x] [cur. y]); Return ;}for (I = 0; I <8; I ++) {node temp = cur; temp. X + = DX [I], temp. Y + = Dy [I]; If (limit (temp) & dis2 [temp. x] [temp. y]! =-1) {// printf ("% d .... 1 \ n ", dis1 [cur. x] [cur. y], dis2 [temp. x] [temp. y]); printf ("% d \ n", dis1 [cur. x] [cur. y] + dis2 [temp. x] [temp. y] + 1); return;} If (limit (temp) & dis1 [temp. x] [temp. y] =-1) {q. push (temp); dis1 [temp. x] [temp. y] = dis1 [cur. x] [cur. y] + 1 ;}}size = P. size (); While (size --) {node cur = P. front (); p. pop (); If (limit (cur) & dis1 [cur. x] [cur. y]! =-1) {// printf ("% d .. 2 \ n ", dis1 [cur. x] [cur. y], dis2 [cur. x] [cur. y]); printf ("% d \ n", dis1 [cur. x] [cur. x] + dis2 [cur. x] [cur. y]); Return ;}for (I = 0; I <8; I ++) {node temp = cur; temp. X + = DX [I]; temp. Y + = Dy [I]; If (limit (temp) & dis1 [temp. x] [temp. y]! =-1) {// printf ("% d .... 2 \ n ", dis2 [cur. x] [cur. y], dis1 [temp. x] [temp. y]); printf ("% d \ n", dis1 [temp. x] [temp. y] + dis2 [cur. x] [cur. y] + 1); return;} If (limit (temp) & dis2 [temp. x] [temp. y] =-1) {P. push (temp); dis2 [temp. x] [temp. y] = dis2 [cur. x] [cur. y] + 1 ;}}}} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & Len); scanf ("% d", & St. x, & St. y, & en. x, & en. y); memset (dis1,-1, sizeof (dis1); memset (dis2,-1, sizeof (dis2); If (MATCH (St, en )) {printf ("0 \ n"); continue;} else dobfs ();} return 0 ;}



Poj 1915 knight moves [bidirectional BFS]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.