P
P
PRIYD2020-12-20 15:49:11
Algorithms
PRIYD, 2020-12-20 15:49:11

How to fail a knight's move?

Task: https://acmp.ru/index.asp?main=task&id_task=6
Problem: it crashes on the 10th test, I break my head, I don't understand why.
The code:

string n;
cin >> n;

string a = "ABCDEFGH";

int i1 = n[1] - '0';
int i2 = n[4] - '0';
char c1 = n[0];
char c2 = n[3];

int iN = abs(i1-i2);
int cN = abs(find(a.begin(), a.end(), c1) - find(a.begin(), a.end(), c2));

bool isCharactersWrong = find(a.begin(), a.end(), c1) == a.end() || find(a.begin(), a.end(), c2) == a.end(); 
bool isNumbersWrong = i1>8 || i2>8 || i1<=0 || i2<=0;

if (isNumbersWrong || isCharactersWrong) {
        cout << "ERROR" << endl;
} 
else if ((iN==2 && cN==1) || (iN==1 && cN==2)) {
        cout << "YES" << endl;
}
else {
        cout << "NO" << endl;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2020-12-20
@PRIYD

Try checking that the middle character is "-".
You also need to check that the length of the string is exactly 5 characters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question