The problem link is correctness of knight move.
I have the following code and can’t find where i am going wrong.Any help would be appreciated.
#include <iostream>
using namespace std;
int valid(string s)
{
if(s.length()!=5)
return 0;
if(s[0]<97 || s[0]>104 || s[3]<97 || s[1]>104)
return 0;
if(s[1]<49 || s[1]>56 || s[4]<49 || s[4]>56)
return 0;
if(s[2]!='-')
return 0;
return 1;
}
int main()
{
int t;
string s;
cin>>t;
cin.ignore();
//ignoore a \n
while(t--)
{
getline(cin,s);
if(valid(s)==0)
{
cout<<"Error"<<endl;
continue;
}
if((abs(s[0]-s[3])==2 && abs(s[1]-s[4])==1) || (abs(s[0]-s[3])==1 && abs(s[1]-s[4])==2))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}