WOJ1028-Injured Bishop Problem

来源:互联网 发布:linux 内存查询 编辑:程序博客网 时间:2024/06/05 19:23

Given a chessboard made up of N squares by N squares. An injured bishop is a bishop who can move only one step in diagonal

direction from current position. You will have to find the minimal distance for the injured bishop to move from an initial position to the final
position. We denotes the position of an injured bishop by (x, y), which represents the bishop in the x-th row, the y-th column. The following
figure illustrates that:

Fig: Injured Bishop at (3, 1) can reach the adjacent grey squares. Bishop at (5, 5) can reach

adjacent grey squares. The injured bishop positions are black and the reachable places are grey.

输入格式

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test
cases.

For each test case, the first line contains an integer N (1 <= N <= 10000), which denotes the chessboard dimension. The second line contains (x, y) (1 <= x, y <= N) denoting the initial position, while the third line has the same form and it denotes the final position.

输出格式

Results should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from

  1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.
    For each test case, output a single line containing the minimal distance for the injured bishop to move from the initial position to the final position. If the injured bishop can?t move to the final position, just output -1 instead.

样例输入

241 12 283 33 3

样例输出

Case 1:1Case 2:0

两个点横纵坐标和奇偶不同肯定无解,其余有解,最优解是先把一个棋子移到另一个的同一行(或者列),然后交叉着走到那个点。

#include<stdio.h>#include<stdlib.h>#include<math.h>int main(void){    int result, start[2], end[2];    int t,N,ti;    scanf("%d", &t);    for(ti=1;ti<=t;ti++){        scanf("%d", &N);        scanf("%d %d", &start[0], &start[1]);        scanf("%d %d", &end[0], &end[1]);        if(abs(start[0]-end[0])%2 == abs(start[1]-end[1])%2){            if(abs(start[0]-end[0]) >= abs(start[1]-end[1])){                result = abs(start[0]-end[0]);            }            else{                result = abs(start[1]-end[1]);            }        }        else{            result = -1;        }        if(ti!=1)printf("\n");        printf("Case %d:\n%d\n",ti, result);        }    return 0;}


原创粉丝点击