Easy
Given the positions of a white bishop and a black pawn on the standard chess board, determine whether the bishop can capture the pawn in one move.
The bishop has no restrictions in distance for each move, but is limited to diagonal movement. Check out the example below to see how it can move:
Example
-
For bishop = "a1" and pawn = "c3", the output should be
bishopAndPawn(bishop, pawn) = true. -
For bishop = "h1" and pawn = "h3", the output should be
bishopAndPawn(bishop, pawn) = false.
Input/Output
-
[execution time limit] 0.5 seconds (c)
-
[input] string bishop
Coordinates of the white bishop in the chess notation.
Guaranteed constraints:
bishop.length = 2,
'a' ≤ bishop[0] ≤ 'h',
1 ≤ bishop[1] ≤ 8. -
[input] string pawn
Coordinates of the black pawn in the same notation.
Guaranteed constraints:
pawn.length = 2,
'a' ≤ pawn[0] ≤ 'h',
1 ≤ pawn[1] ≤ 8. -
[output] boolean
- true if the bishop can capture the pawn, false otherwise.
[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
bool bishopAndPawn(char *bishop,char *pawn)
{
return abs(pawn[0]-bishop[0])==abs(pawn[1]-bishop[1]);
}
'Codesignal' 카테고리의 다른 글
<Codesignal> buildPalindrome (0) | 2020.04.05 |
---|---|
<Codesignal> isBeautifulString (0) | 2020.04.05 |
<Codesignal> digitDegree (0) | 2020.04.05 |
<Codesignal> longestDigitsPrefix (0) | 2020.04.05 |
<Codesignal> Knapsack Light (0) | 2020.04.05 |