Algorithm


Problem Name: beecrowd | 2808

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

Knights Again

 

By Francisco Elio Parente Arcos Filho, UEA BR Brazil

Timelimit: 1

Given the initial position of a Knight on a chessboard and the target position, it must be said if, with exactly one movement, the Knight can reach the target position. If this is possible, the move is classified as valid, otherwise the move is said invalid..

On a chessboard numbers are used, from 1 to 8, to specify the board line and letters, from 'a' to 'h,' to specify the column.

 

Input

 

The entry consists of a single line containing the initial position of the Knight and the target position, separated by a space. A position in the board is specified by a character, which represents the column, followed by an integer representing the line.

 

Output

 

The output consists of a line containing the message "VALIDO" if the move is a valid movement of a Knight in the game of chess or "INVALIDO" otherwise.

 

 

 

Input Samples Output Samples

d4 b5

VALIDO

 

 

 

a1 g6

INVALIDO

 

 

 

h8 f7

VALIDO

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

int main()
{
    char x[3], y[3];
    int a, b, fin;
    while(scanf("%s %s", x, y)!=EOF)
    {

        a = x[0] - y[0];
        b = x[1] - y[1];

        fin=abs(a*a)+abs(b*b);
        if(fin==5)printf("VALIDO\n");
        else
            printf("INVALIDO\n");
        fin=0;
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
d4 b5

Output

x
+
cmd
VALIDO

#2 Code Example with Java Programming

Code - Java Programming


const { readFileSync } = require("node:fs")
const [from, to] = readFileSync("/dev/stdin", "utf-8").split(" ", 2)

/**
 * @param {string} posA
 * @param {string} posB
 */
function validateChessKnightMovements(posA, posB) {
	const [colA, lnA] = posA.toUpperCase().split("", 2)
	const [colB, lnB] = posB.toUpperCase().split("", 2)

	const dx = Math.abs(colA.charCodeAt(0) - colB.charCodeAt(0))
	const dy = Math.abs(lnA.charCodeAt(0) - lnB.charCodeAt(0))

	return ((dx == 1 && dy == 2) || (dx == 2 && dy == 1))
}

console.log(validateChessKnightMovements(from, to) ? "VALIDO" : "INVALIDO")

Copy The Code & Try With Live Editor

Input

x
+
cmd
d4 b5

Output

x
+
cmd
VALIDO

#3 Code Example with Javascript Programming

Code - Javascript Programming


i, d = input().split()
col = abs(ord(i[0]) - ord(d[0]))
lin = abs(int(i[1]) - int(d[1]))

if (col == 1 and lin == 2) or (col == 2 and lin == 1):
    print('VALIDO')
else:
    print('INVALIDO')

Copy The Code & Try With Live Editor

Input

x
+
cmd
d4 b5

Output

x
+
cmd
VALIDO
Advertisements

Demonstration


Previous
#2807 Beecrowd Online Judge Solution 2807 Iccanobif Solution in C, C++, Java, Js and Python
Next
#2812 Beecrowd Online Judge Solution 2812 Laércio Solution in C, C++, Java, Js and Python