Sunday, January 26, 2014

codechef KNIGHTMV - "correctness of knight move" solution

codechef KNIGHTMV - "correctness of knight move": http://www.codechef.com/problems/KNIGHTMV
codechef KNIGHTMV - "correctness of knight move" editorial :  http://discuss.codechef.com/questions/4132/knightmv-editorial

codechef KNIGHTMV - "correctness of knight move" solution :  http://ideone.com/hPy4KW

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int main() {
int t, flag;
char s[15];
scanf("%d", &t);
getchar();
while(t--) {
flag=0;
gets(s);
if(s[0]<'a' || s[0]>'h' || s[1]<'1' || s[1]>'8' || s[2]!='-' || s[3]<'a' || s[3]>'h' || s[4]<'1' || s[4]>'8' || strlen(s)!=5) {
cout<<"Error"<<endl;
}
else {
if(s[0]>s[3]) {
if(s[1]>s[4]) {
if(s[0]-s[3]==1 && s[1]-s[4]==2) flag=1;
else if(s[0]-s[3]==2 && s[1]-s[4]==1) flag=1;
}
else {
if(s[0]-s[3]==1 && s[4]-s[1]==2) flag=1;
else if(s[0]-s[3]==2 && s[4]-s[1]==1) flag=1;
}
}
else {
if(s[1]>s[4]) {
if(s[3]-s[0]==1 && s[1]-s[4]==2) flag=1;
else if(s[3]-s[0]==2 && s[1]-s[4]==1) flag=1;
}
else {
if(s[3]-s[0]==1 && s[4]-s[1]==2) flag=1;
else if(s[3]-s[0]==2 && s[4]-s[1]==1) flag=1;
}
}
if(flag==1) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
return 0;
}

note: pay attn to difference between <cstdio> and <stdio.h>, i damn used and tried many times gets(), etc with <cstdio>, verdict was always WA, but i changed it to <stdio.h>, AC :)

also, imho many use of && in if condition brings prob may be, i dont know :( my code did not work on that

No comments:

Post a Comment