WOJ1028-Injured Bishop Problem

xiaoxiao2021-02-28  15

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

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.


2 4 1 1 2 2 8 3 3 3 3


Case 1: 1 Case 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; }

转载请注明原文地址: https://www.6miu.com/read-1250309.html