You are viewing a single comment's thread. Return to all comments →
int queensAttack(int n, int k, int r_q, int c_q, vector> obstacles) {
int counter = 0; //Tower walk int leftCount = c_q - 1; int rightCount = n - c_q; int upCount = n - r_q; int downCount = r_q - 1; for (const auto& obstacle : obstacles) { if (obstacle[0] == r_q && obstacle[1] == c_q) { return 0; } else if (obstacle[0] == r_q && obstacle[1] < c_q) { leftCount = min(leftCount, c_q - obstacle[1] - 1); } else if (obstacle[0] == r_q && obstacle[1] > c_q) { rightCount = min(rightCount, obstacle[1] - c_q - 1); } else if (obstacle[1] == c_q && obstacle[0] > r_q) { upCount = min(upCount, obstacle[0] - r_q - 1); } else if (obstacle[1] == c_q && obstacle[0] < r_q) { downCount = min(downCount, r_q - obstacle[0] - 1); } } counter += (leftCount + rightCount + upCount + downCount); //bishop walk int rowDiff = n - r_q; int colDiff = c_q - 1; int leftTopDiag = min(rowDiff, colDiff); rowDiff = r_q - 1; colDiff = c_q - 1; int leftBottomDiag = min(rowDiff, colDiff); rowDiff = n - r_q; colDiff = n - c_q; int rightTopDiag = min(rowDiff, colDiff); rowDiff = r_q - 1; colDiff = n - c_q; int rightBottomDiag = min(rowDiff, colDiff); for (const auto& obstacle : obstacles) { if (abs(obstacle[0] - r_q) == abs(obstacle[1] - c_q)) { if (obstacle[0] > r_q && obstacle[1] < c_q) { leftTopDiag = min(abs(obstacle[0] - r_q) - 1, leftTopDiag) ; } else if (obstacle[0] < r_q && obstacle[1] < c_q) { leftBottomDiag = min(abs(obstacle[0] - r_q) - 1, leftBottomDiag); } else if (obstacle[0] > r_q && obstacle[1] > c_q) { rightTopDiag = min(rightTopDiag, abs(obstacle[0] - r_q) - 1); } else if (obstacle[0] < r_q && obstacle[1] > c_q) { rightBottomDiag = min(rightBottomDiag, abs(obstacle[0] - r_q) - 1); } } } counter += (leftTopDiag + leftBottomDiag + rightTopDiag + rightBottomDiag); return counter;
}
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Queen's Attack II
You are viewing a single comment's thread. Return to all comments →
int queensAttack(int n, int k, int r_q, int c_q, vector> obstacles) {
}