POJ 1915 Knight Moves

来源:互联网 发布:健身会所软件 编辑:程序博客网 时间:2024/06/05 22:11
Knight Moves
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 22794 Accepted: 10654
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


这道题套了一下上一道广搜题的模板,嘻嘻,都是朝8个方向走的,而且都是马在走日字的格,不同点是,这题是第一行是有0点的,不是从1开始的,所以我就强行把每一个输入为0的点转化为从1 开始,第二个不同点是,给了棋盘的大小不是一定的,会变,所以在循环次数那个地方得变变随棋盘大小改变,还有边界条件也是不一样的。

想起了高一时候的班主任了,每次开班会的逻辑都挺好,“今天要说的第一点balabala .......,第二点balabala .....第。。。。。。。。。最后说一下最后一点也是最重要的一点balabala ......再说一下.最后一点balabala......再说一下.最后一点balabalabala...........最后一点.................”,我也是醉了。

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;struct point{    int x,y;}point;int a[1005][1005],b[1005][1005];int main(void){   // freopen("D.txt","r",stdin);    int n;    scanf("%d",&n);    while(n--)    {        queue<struct point> p;        int l;        int endx,endy;        scanf("%d",&l);        scanf("%d%d%d%d",&point.x,&point.y,&endx,&endy);        if(point.x==0||point.y==0||endx==0||endy==0)        {            point.x++;            point.y++;            endx++;            endy++;        }      //  printf("%d%d%d%d\n",point.x,point.y,endx,endy);        p.push(point);        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        b[point.x][point.y]=1;        while(!p.empty())        {            struct point tmp;            tmp.x=p.front().x;tmp.y=p.front().y;            p.pop();            if(tmp.x==endx&&tmp.y==endy) break;            if(a[tmp.x-2][tmp.y+1]<=l*l&&b[tmp.x-2][tmp.y+1]==0&&tmp.x-2>=1&&tmp.x-2<=l&&tmp.y+1>=1&&tmp.y+1<=l)            {                a[tmp.x-2][tmp.y+1]=a[tmp.x][tmp.y]+1;                b[tmp.x-2][tmp.y+1]=1;                struct point t;                t.x=tmp.x-2;                t.y=tmp.y+1;                p.push(t);            }            if(a[tmp.x-1][tmp.y+2]<=l*l&&b[tmp.x-1][tmp.y+2]==0&&tmp.x-1>=1&&tmp.x-1<=l&&tmp.y+2>=1&&tmp.y+2<=l)            {                a[tmp.x-1][tmp.y+2]=a[tmp.x][tmp.y]+1;                b[tmp.x-1][tmp.y+2]=1;                struct point t;                t.x=tmp.x-1;                t.y=tmp.y+2;                p.push(t);            }            if(a[tmp.x+1][tmp.y+2]<=l*l&&b[tmp.x+1][tmp.y+2]==0&&tmp.x+1>=1&&tmp.x+1<=l&&tmp.y+2>=1&&tmp.y+2<=l)            {                a[tmp.x+1][tmp.y+2]=a[tmp.x][tmp.y]+1;                b[tmp.x+1][tmp.y+2]=1;                struct point t;                t.x=tmp.x+1;                t.y=tmp.y+2;                p.push(t);            }            if(a[tmp.x+2][tmp.y+1]<=l*l&&b[tmp.x+2][tmp.y+1]==0&&tmp.x+2>=1&&tmp.x+2<=l&&tmp.y+1>=1&&tmp.y+1<=l)            {                a[tmp.x+2][tmp.y+1]=a[tmp.x][tmp.y]+1;                b[tmp.x+2][tmp.y+1]=1;                struct point t;                t.x=tmp.x+2;                t.y=tmp.y+1;                p.push(t);            }            if(a[tmp.x+2][tmp.y-1]<=l*l&&b[tmp.x+2][tmp.y-1]==0&&tmp.x+2>=1&&tmp.x+2<=l&&tmp.y-1>=1&&tmp.y-1<=l)            {                a[tmp.x+2][tmp.y-1]=a[tmp.x][tmp.y]+1;                b[tmp.x+2][tmp.y-1]=1;                struct point t;                t.x=tmp.x+2;                t.y=tmp.y-1;                p.push(t);            }            if(a[tmp.x+1][tmp.y-2]<=l*l&&b[tmp.x+1][tmp.y-2]==0&&tmp.x+1>=1&&tmp.x+1<=l&&tmp.y-2>=1&&tmp.y-2<=l)            {                a[tmp.x+1][tmp.y-2]=a[tmp.x][tmp.y]+1;                b[tmp.x+1][tmp.y-2]=1;                struct point t;                t.x=tmp.x+1;                t.y=tmp.y-2;                p.push(t);            }            if(a[tmp.x-1][tmp.y-2]<=l*l&&b[tmp.x-1][tmp.y-2]==0&&tmp.x-1>=1&&tmp.x-1<=l&&tmp.y-2>=1&&tmp.y-2<=l)            {                a[tmp.x-1][tmp.y-2]=a[tmp.x][tmp.y]+1;                b[tmp.x-1][tmp.y-2]=1;                struct point t;                t.x=tmp.x-1;                t.y=tmp.y-2;                p.push(t);            }            if(a[tmp.x-2][tmp.y-1]<=l*l&&b[tmp.x-2][tmp.y-1]==0&&tmp.x-2>=1&&tmp.x-2<=l&&tmp.y-1>=1&&tmp.y-1<=l)            {                a[tmp.x-2][tmp.y-1]=a[tmp.x][tmp.y]+1;                b[tmp.x-2][tmp.y-1]=1;                struct point t;                t.x=tmp.x-2;                t.y=tmp.y-1;                p.push(t);            }        }        printf("%d\n",a[endx][endy]);    }    return 0;}

0 0
原创粉丝点击