Problem statement is here: http://acm.timus.ru/problem.aspx?space=1&num=1197
One of the simplest problem of Timus. I would encourage you to try first before looking at the solution.
package codes; import FastIO.InputReader; import FastIO.OutputWriter; public class Task1197 { private static final int SIZE = 8; private static final int[] ROW_DELTA = new int[]{ 2, 2, 1, -1, -2, -2, 1, -1 }; private static final int[] COL_DELTA = new int[]{ 1, -1, 2, 2, 1, -1, -2, -2 }; public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); while (n-- > 0) { int row = in.nextCharacter() - 'a'; int col = in.nextInt() - 1; int numStepsPossible = 0; // This is the most straightforward way to solve this problem. // This is a classic simulation problem so just code the same way // you arrived at the solution in your mind. No tricks whatsoever. // // Up two steps then one right. // if (row + 2 < SIZE && col + 1 < SIZE) { // numStepsPossible++; // } // // Up two steps then one left. // if (row + 2 < SIZE && col - 1 >= 0) { // numStepsPossible++; // } // // Right two steps and then one step up. // if (row + 1 < SIZE && col + 2 < SIZE) { // numStepsPossible++; // } // // Right two steps and then one step down. // if (row - 1 >= 0 && col + 2 < SIZE) { // numStepsPossible++; // } // // Down two steps and then one step right. // if (row - 2 >= 0 && col + 1 < SIZE) { // numStepsPossible++; // } // // Down two steps and then one step left. // if (row - 2 >= 0 && col - 1 >= 0) { // numStepsPossible++; // } // // Two steps left and then one step up. // if (row + 1 < SIZE && col - 2 >= 0) { // numStepsPossible++; // } // // Two steps left and then one step down. // if (row - 1 >= 0 && col - 2 >= 0) { // numStepsPossible++; // } // We can make our programs concise by removing the if statements, // Note that row and col only changes by +/- 2 or +/- 1 so we store // the changes in an array and simply iterate over the array // counting the valid moves. for (int i = 0; i < ROW_DELTA.length; i++) { numStepsPossible += (row + ROW_DELTA[i] >= 0 && row + ROW_DELTA[i] < SIZE && col + COL_DELTA[i] >= 0 && col + COL_DELTA[i] < SIZE) ? 1 : 0; } out.println(numStepsPossible); } } }