알고리즘/codefights

50>chessKnight

Diademata 2017. 11. 4. 01:35
반응형

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.






code>>


int chessKnight(std::string cell) {

int x = cell[0] - 'a';

int y = cell[1] - '1';

int r = 0;

if (x - 2 >= 0 && y + 1 < 8) r++;

if (x - 2 >= 0 && y - 1 >= 0) r++;


if (y - 2 >= 0 && x + 1 < 8) r++;

if (y - 2 >= 0 && x - 1 >= 0) r++;


if (x + 2 < 8 && y + 1 < 8) r++;

if (x + 2 < 8 && y - 1 >= 0) r++;


if (y + 2 < 8 && x + 1 < 8) r++;

if (y + 2 < 8 && x - 1 >= 0) r++;

return r;

}


개 귀찮 ㅋㅋㅋㅋㅋ


번역기 돌리면서 문제 풀었는데 더 이상 이해가 안되서 포기한다.

반응형