Medium
Given a position of a knight on the standard chessboard, find the number of different moves the knight can perform.
The knight can move to a square that is two squares horizontally and one square vertically, or two squares vertically and one square horizontally away from it. The complete move therefore looks like the letter L. Check out the image below to see all valid moves for a knight piece that is placed on one of the central squares.
Example
-
For cell = "a1", the output should be
chessKnight(cell) = 2. -
For cell = "c2", the output should be
chessKnight(cell) = 6.
Input/Output
-
[execution time limit] 0.5 seconds (c)
-
[input] string cell
String consisting of 2 letters - coordinates of the knight on an 8 × 8 chessboard in chess notation.
Guaranteed constraints:
cell.length = 2,
'a' ≤ cell[0] ≤ 'h',
1 ≤ cell[1] ≤ 8. -
[output] integer
[C] Syntax Tips
// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
char * answer = malloc(strlen(name) + 8);
printf("This prints to the console when you Run Tests");
strcpy(answer, "Hello, ");
strcat(answer, name);
return answer;
}
Solution
int chessKnight(char *cell)
{
return cell[0]=='a'||cell[0]=='h'?(cell[1]=='1'||cell[1]=='8'?2:cell[1]=='2'||cell[1]=='7'?3:4):cell[0]=='b'||cell[0]=='g'?(cell[1]=='1'||cell[1]=='8'?3:cell[1]=='2'||cell[1]=='7'?4:6):(cell[1]=='1'||cell[1]=='8'?4:cell[1]=='2'||cell[1]=='7'?6:8);
}
'Codesignal' 카테고리의 다른 글
<Codesignal> longestWord (0) | 2020.04.06 |
---|---|
<Codesignal> deleteDigit (0) | 2020.04.06 |
<Codesignal> lineEncoding (0) | 2020.04.06 |
<Codesignal> isDigit (0) | 2020.04.06 |
<Codesignal> Is MAC48 Address? (0) | 2020.04.06 |