Knight Moves(BFS)


リンク:http://poj.org/problem?id=1915
Problem:
Knight Moves
Time Limit: 1000 MS
 
メモリLimit: 30000 K
Total Submissions: 22061
 
Acceepted: 10344
Description
Background 
Mr Somurlov,fabulous chees-gamer indeed,asterts that no one else but him can move knights from one position to another so fast.Can beat him? 
The Problem 
Your task is to write a program to calculate the minimum number of moves need for a knight to reach one point from another、so that you have the change to be faster than Somurlov. 
For people not familyr with chess、the possible knight moves are shown in Figure 1. 
Knight Moves(BFS)_第1张图片
Input
The input begins with the number n of scenaros on a single line by itself. 
Next follow n scenaros.Each scenaro consists of three lineas containing integer numbers.The first line specifies the length l of a side of the chess board(4==l==300).The entire board harid size.The.The.conirl.Thespecifying the starting and ending position of the knight on the board.The integers are separated by a single blank.You can assitions the positions ars ars the chess board of the scenalo.
Output
For each scenaro of the input you have to calculate the minimal amount of knight moves which ar necessary to move from the starting point.If starting point and ending point are e e e e e e e,distance zeris.Themure stance ine ine ine infone.
Sample Input
3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1
Sample Output
5
28
0
コード:
ヒドイincludeヒドイincludeヒドイincludeusing namespace std;int vis[301][301],Step[301][301],nint d[8]={-2、-1}、{-1、-2}、{1、-2}、{2、-1}、{2、-1}、{1,2}struct node{int x;int y;}node st,edqueueQbfs(){node now、next;inti;Q.pussh(st);vis[st.x][st. y]==1;step[st. x][st. x][st. y]=0;while(!Q.emptyy(){now=Q.frontt();Q.pop.();.vip.vip.if(now.if=now=now. ew. ew. ewx=ewx=ewx=edyyyyyyyyyyyyyyyyyyyyy= edededededededededededewwwwwwwwwwwww= edp=edy=ewfor(i=0;i<8;i+){next.x=d[i]+now.x;next.y=d[i]+n[i]+now.y;if(next.x<0|next.y<0|next.y>next.y.{Q.pussh(next);vis[next.x][next.y]=1;step[next.x][next.x]、[next.y]=step[now.x][now.y]+1;if(next.x==ed.x&next.y=edt.y====ed. y=ed.y=ed. y=ed. y=ed. y=ed.y=ed. y)retututurnint.y)returnstststststep.exttttttttttttttttttttttttttxt.[[[[]]]]]{while}{scanf('%d]、&n);scanf('%d'、&st.x、&s.y);scanf('%d%d'、&ed.x、&ed.y);memset(vis,0,sizef(vis);memset(step,zep 0,zepe of);(!Q.empty().Q.pop();printf(“%d”、bfs();}return 0;