Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1087

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1087

Queen

 

By Fábio Dias Moreira  Brazil

Timelimit: 1

The game of Chess has several pieces with curious movements. One of them is the Queen, which can move any number of squares in any direction: in the same line, in the same column or in any of the diagonals, as illustrated by the figure below (black dots represent positions the queen may reach in one move):



The great Chess Master Kary Gasparov invented a new type of chess problem: given the position of a queen in an empty standard chess board (that is, an 8 x 8 board) how many moves are needed so that she reaches another given square in the board?

Kary found the solution for some of those problems, but is having a difficult time to solve some others, and therefore he has asked that you write a program to solve this type of problem.

 

Input

 

The input contains several test cases. The only line of each test case contains four integers X1, Y1, X2 and Y2 (1 ≤ X1, Y1, X2, Y2 ≤ 8). The queen starts in the square with coordinates (X1, Y1), and must finish at the square with coordinates (X2, Y2). In the chessboard, columns are numbered from 1 to 8, from left ro right; lines are also numbered from 1 to 8, from top to bottom. The coordinates of a square in line X and column Y are (X, Y).

The end of input is indicated by a line containing four zeros, separated by spaces.

 

Output

 

For each test case in the input your program must print a single line, containing an integer, indicating the smallest number of moves needed for the queen to reach the new position.

 

 

 

Input Sample Output Sample

4 4 6 2
3 5 3 5
5 5 4 3
0 0 0 0

1
0
2

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main ()
{

  unsigned short x1, y1, x2, y2;

  while (true)
  {

    scanf("%hd %hd %hd %hd", &x1, &y1, &x2, &y2);

    if (x1 == 0 && y1 == 0 && x2 == 0 && y2 == 0)
      break;

    if (x1 == x2 && y1 == y2)
      printf("0\n");
    else if (abs(x2 - x1) == abs(y2 - y1))
      printf("1\n");
    else if (x1 == x2 || y1 == y2)
      printf("1\n");
    else
      printf("2\n");

  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 4 6 2
3 5 3 5
5 5 4 3
0 0 0 0

Output

x
+
cmd
1
0
2

#2 Code Example with C++ Programming

Code - C++ Programming


#include <stdio.h>

int main(){
    int x1,y1,x2,y2;
    
    while(true){
                   scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
                   if(x1 == 0 && y1 == 0 && x2 == 0 && y2 == 0) break;
                   
                   if(x1 == x2 && y1 == y2) printf("0\n");
                   else if((x2-x1) == -(y2-y1) || -(x2-x1) == -(y2-y1) || -(x2-x1) == (y2-y1) || (x2-x1) == (y2-y1)) printf("1\n");
                   else if(x1 == x2 || y1 == y2) printf("1\n");
                   else printf("2\n");
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 4 6 2
3 5 3 5
5 5 4 3
0 0 0 0

Output

x
+
cmd
1
0
2

#3 Code Example with Java Programming

Code - Java Programming


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner entrada = new Scanner(System.in);
		int x1 , y1 , x2 , y2 , qtdMov;
		while(true) {
			x1 = entrada.nextInt();
			y1 = entrada.nextInt();
			x2 = entrada.nextInt();
			y2 = entrada.nextInt();
			if(x1 == 0 && y1 == 0 && x2 == 0 && y2 == 0)
				break;
			if(x1 == x2 && y1 == y2)
					System.out.printf("0\n");
			else if ((x2-x1) == -(y2-y1) || -(x2-x1) == -(y2-y1) || -(x2-x1) == (y2-y1) || (x2-x1) == (y2-y1))
					System.out.printf("1\n");
			else if(x1 == x2 || y1 == y2)
					System.out.printf("1\n");
			else
					System.out.printf("2\n");
		}
    }
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
4 4 6 2
3 5 3 5
5 5 4 3
0 0 0 0

Output

x
+
cmd
1
0
2

#4 Code Example with Javascript Programming

Code - Javascript Programming


const { readFileSync } = require("fs")
const input = readFileSync("/dev/stdin", "utf8")
	.split("\n")
	.map((line) => line.split(" ").map((value) => Number.parseInt(value, 10)))

function main() {
	const responses = []

	for (const [X1, Y1, X2, Y2] of input) {
		if ([X1, Y1, X2, Y2].every((crd) => crd == 0)) break

		if (X1 == X2 && Y1 == Y2) responses.push("0")
		else if ((X1 != X2 && Y1 == Y2) || (X1 == X2 && Y1 != Y2) || Math.abs(X1 - X2) == Math.abs(Y1 - Y2)) responses.push("1")
		else responses.push("2")
	}

	console.log(responses.join("\n"))
}

main()
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 4 6 2
3 5 3 5
5 5 4 3
0 0 0 0

Output

x
+
cmd
1
0
2

#5 Code Example with Python Programming

Code - Python Programming


from math import fabs
aux=2
while True:
    x1,y1,x2,y2=map(int,input().split())
    if x1==y1==x2==y2==0:break
    if x1==x2 and y1==y2:print(0)
    elif((x2-x1) == -(y2-y1) or -(x2-x1) == -(y2-y1) or -(x2-x1) == (y2-y1) or (x2-x1) == (y2-y1)):print("1")
    elif x1==x2 or y1==y2:print(1)
    else:print(2)
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 4 6 2
3 5 3 5
5 5 4 3
0 0 0 0

Output

x
+
cmd
1
0
2
Advertisements

Demonstration


Previous
#1086 Beecrowd Online Judge Solution 1086 The Club Ballroom Solution in C, C++, Java, Js and Python
Next
#1089 Beecrowd Online Judge Solution 1089 Musical Loop Solution in C, C++, Java, Js and Python