304 North Cardinal St.
Dorchester Center, MA 02124

# Correctness of Knight Move CodeChef Solution

## Correctness of Knight Move CodeChef Solution in C++14

``````#include <bits/stdc++.h>
#define ll long long
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(s)	(ll)(s.size())
#define e "\n"
#define lp(i,n)  for(int i=0;i<n;i++)
const ll mod = 1e9 + 7;
using namespace std;
void hitham() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
string s;
bool valid(char a, char b) {
return ('a' <= a && a <= 'h' && '1' <= b && b <= '8');
}
bool knight(char a, char b, char c, char d) {
return (abs(a - c) == 2 && abs(b - d) == 1) || (abs(a - c) == 1 && abs(b - d) == 2);
}
int main()
{
//freopen("popcorn.in","r",stdin);
//freopen("output.txt","w",stdout);
hitham();
int t; cin >> t;
cin.ignore();
lp(w, t) {
//cin >> s;
getline(cin, s);
if (sz(s) == 5 && valid(s[0], s[1]) && valid(s[3], s[4]) && s[2] == '-') {
if (knight(s[0], s[1], s[3], s[4])) {
cout << "Yes\n";
}
else cout << "No\n";
}
else cout << "Error\n";
}
}``````

## Correctness of Knight Move CodeChef Solution in PYTH 3

``````ch, dg = 'abcdefgh', '12345678'

ans = []
In = [input() for _ in range(int(input()))]
for A in In:
if len(A) != 5: an = 'Error'
elif (A[0] not in ch) or (A[3] not in ch) or (A[1] not in dg) or (A[4] not in dg) or (A[2] != '-'): an = 'Error'
else:
c, d = abs(ord(A[0])-ord(A[3])), abs(int(A[1])-int(A[4]))
if c!=0 and d!=0 and ((c+d) == 3): an = 'Yes'
else: an = 'No'
ans.append(an)
print(*ans, sep = '\n')``````

## Correctness of Knight Move CodeChef Solution in C

``````# include<stdio.h>
int main( )
{
int o;

scanf("%d",&o);

getchar();

while(o--)
{
int n,k=0,a,b;

char c[12];

gets(c);

n=strlen(c);

if(n==5)
{
if((c[0]>=97&&c[0]<=104)&&(c[1]>=49&&c[1]<=56)&&(c[2]==45)&&(c[3]>=97&&c[3]<=104)&&(c[4]>=49&&c[4]<=56))
k=1;

if(k==1)
{
a=abs(c[0]-c[3]);

b=abs(c[1]-c[4]);

if((a==2&&b==1)||(a==1&&b==2))

puts("Yes");

else

puts("No");

}

else

puts("Error");

}

else

puts("Error");

}
}``````

## Correctness of Knight Move CodeChef Solution in JAVA

``````/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
String w = sc.nextLine();
for (int ind=0; ind<t; ind++)
{
String s = sc.nextLine();
int l = s.length();
if (l!=5 || s.charAt(2)!='-')
{
System.out.println("Error");
continue;
}
char ch1 = s.charAt(0);
char ch2 = s.charAt(1);
char ch3 = s.charAt(3);
char ch4 = s.charAt(4);
if ((ch1>'h' || ch1<'a') || (ch2>'8' || ch2<'1') || (ch3>'h' || ch3<'a') ||
(ch4>'8' || ch4<'1'))
{
System.out.println("Error");
continue;
}
int d1 = ch1>ch3? ch1-ch3 : ch3-ch1;
int d2 = ch2>ch4? ch2-ch4 : ch4-ch2;
if ((d1==2 && d2==1) || (d1==1 && d2==2))
{
System.out.println("Yes");

}
else
System.out.println("No");
}
}
}``````

## Correctness of Knight Move CodeChef Solution in PYPY 3

``````for x in range(int(input())):
a=['a','b','c','d','e','f','g','h']
b=['1','2','3','4','5','6','7','8']
s=input()
l=len(s)
if l!=5:
print("Error")
elif s[0] in a and s[1] in b and s[2]=='-' and s[3] in a and s[4] in b:
if abs(ord(s[0])-ord(s[3]))==1 and abs(ord(s[1])-ord(s[4]))==2 or abs(ord(s[0])-ord(s[3]))==2 and abs(ord(s[1])-ord(s[4]))==1:
print("Yes")
else:
print("No")
else:
print("Error")``````

## Correctness of Knight Move CodeChef Solution in PYTH

``````t = int(raw_input())
for i in range(t):
st = raw_input()
if len(st) == 5:
rst = 'No'
if (st[0] < 'a') or (st[0] > 'h'):
rst = 'Error'
if (st[3] < 'a') or (st[3] > 'h'):
rst = 'Error'
if (st[1] < '1') or (st[1] > '8'):
rst = 'Error'
if (st[4] < '1') or (st[4] > '8'):
rst = 'Error'
if st[2] <> '-':
rst = 'Error'
if rst == 'No':
n = abs(ord(st[0])-ord(st[3]))
m = abs(ord(st[1])-ord(st[4]))
n = n*m
if n == 2:
rst = 'Yes'
else:
rst = 'Error'
print rst``````

## Correctness of Knight Move CodeChef Solution in C#

``````using System;

namespace CC_E_CorrectnessKnightMove
{
class MainClass
{
public static void Main(string[] args)
{
string[] moveValid = new string[moves];

for (int i = 0; i < moves; i++)
{

moveValid[i] = CheckCorrectness(line);
}

foreach (string validity in moveValid)
{
Console.WriteLine(validity);
}

}

static string CheckCorrectness(string line)
{
if (line.Length == 5)
{
if (line[2] == '-')
{
if (CheckValidPosition(line[0], line[1]))
{
if (CheckValidPosition(line[3], line[4]))
return CheckValidMove(line);
}
}
}

return "Error";
}

static bool CheckValidPosition(char c, char n)
{
if (c >= 'a' && c <= 'h')
{
if (n >= '1' && n <= '8')
return true;
}

return false;
}

static string CheckValidMove(string line)
{
if (IsVerticalMovement(line))
return "Yes";
if (IsHorizontalMovement(line))
return "Yes";
return "No";
}

static bool IsVerticalMovement(string line)
{
if (Math.Abs(line[0] - line[3]) == 2)
{
if (Math.Abs(line[1] - line[4]) == 1)
return true;
}
return false;
}

static bool IsHorizontalMovement(string line)
{
if (Math.Abs(line[1] - line[4]) == 2)
{
if (Math.Abs(line[0] - line[3]) == 1)
return true;
}
return false;
}
}
}``````

## Correctness of Knight Move CodeChef Solution in NODEJS

``````var input = '';

function cacheInput(data) {
input += data;
}

function callMain() {
input = input.split(/\r?\n/);
main();
}

var i = -1;
return function() {
return input[++i];
};
}();

}

//Is the value in the range [start, end]?
function isInRange(value, start, end) {
if (value >= start && value <= end) {
return true;
}
return false;
}

function validateKnightMove(line) {
var codeA = 'a'.charCodeAt();
var codeH = 'h'.charCodeAt();
var code1 = '1'.charCodeAt();
var code8 = '8'.charCodeAt();
var codeMinus = '-'.charCodeAt(0);
if (line.length == 5
&& isInRange(line.charCodeAt(0),codeA, codeH)
&& isInRange(line.charCodeAt(1), code1, code8)
&& line.charCodeAt(2) == codeMinus
&& isInRange(line.charCodeAt(3),codeA, codeH)
&& isInRange(line.charCodeAt(4), code1, code8)) {
if (Math.abs(line.charCodeAt(0) - line.charCodeAt(3))
* Math.abs(line.charCodeAt(1) - line.charCodeAt(4)) == 2) {
return 'Yes';
}
else {
return 'No';
}
}
else {
return 'Error';
}
}

function main() {
var output = '';
for (var t = 0; t < T; ++t) {
output += validateKnightMove(line) + '\n';
}
process.stdout.write(output);
process.exit();
}

process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', cacheInput).on('end', callMain);``````
##### Correctness of Knight Move CodeChef Solution Review:

In our experience, we suggest you solve this Correctness of Knight Move CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Correctness of Knight Move CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this Correctness of Knight Move CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!