반응형

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.



난 백트랙킹으로 구현 했는데...


한줄로도 되네? 오진다 오져


code>>


bool check(int b_x, int b_y, int p_x, int p_y, int x, int y)

{

if (b_x < 0 || b_x > 7) return false;

if (b_y < 0 || b_y > 7) return false;

if (b_x == p_x && b_y == p_y) return true;

bool result = false;

result = check(b_x + x, b_y + y, p_x, p_y, x, y);

return result;

}


bool bishopAndPawn(std::string bishop, std::string pawn) {

int x[4] = { -1, 1, -1, 1 };

int y[4] = { 1, 1, -1, -1 };

int b_x = bishop[0] - 'a';

int b_y = bishop[1] - '1';

int p_x = pawn[0] - 'a';

int p_y = pawn[1] - '1';

bool result = false;

for (int i = 0; i< 4; i++)

{

result = check(b_x, b_y, p_x, p_y, x[i], y[i]);

if (result)

break;

}

return result;

}


다른 사람 코드>>


bool bishopAndPawn(std::string b, std::string p) {

return abs(p[0] - b[0]) == abs(p[1] - b[1]);

}



반응형

'알고리즘 > codefights' 카테고리의 다른 글

44>findEmailDomain  (0) 2017.09.12
43>isBeautifulString  (0) 2017.08.11
41>digitDegree  (0) 2017.08.08
40>longestDigitsPrefix  (0) 2017.08.08
39>knapsackLight  (0) 2017.08.08

+ Recent posts