Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int queensAttack(int n, int k, int rQ, int cQ, int[][] obstacles) {
- int toTheRight = n + 1 - cQ;
- int toTheTop = n + 1 - rQ;
- int[] leftDiagonal = {rQ + Math.Min(cQ, toTheTop), cQ - Math.Min(cQ, toTheTop)};
- int[] topVertical = {n + 1, cQ};
- int[] rightAntidiagonal = {rQ + Math.Min(toTheRight, toTheTop), cQ + Math.Min(toTheRight, toTheTop)};
- int[] rightHorizontal = {rQ, n + 1};
- int[] rightDiagonal = {rQ - Math.Min(toTheRight, rQ), cQ + Math.Min(toTheRight, rQ)};
- int[] bottomVertical = {0, cQ};
- int[] leftAntidiagonal = {rQ - Math.Min(cQ, rQ), cQ - Math.Min(cQ, rQ)};
- int[] leftHorizontal = {rQ, 0};
- foreach (int[] obstacle in obstacles) {
- int rO = obstacle[0];
- int cO = obstacle[1];
- int[] newValue = new[] {rO, cO};
- if (cO < cQ) {
- if (rO > rQ && rO - rQ == cQ - cO) {
- if (cO > leftDiagonal[1]) {
- leftDiagonal = newValue;
- }
- }
- else if (rO == rQ) {
- if (cO > leftHorizontal[1]) {
- leftHorizontal = newValue;
- }
- }
- else if (rO < rQ && rQ - rO == cQ - cO) {
- if (cO > leftAntidiagonal[1]) {
- leftAntidiagonal = newValue;
- }
- }
- }
- else if (cO == cQ) {
- if (rO > rQ && rO < topVertical[0]) {
- topVertical = newValue;
- }
- else if (rO < rQ && rO > bottomVertical[0]) {
- bottomVertical = newValue;
- }
- }
- else if (cO > cQ) {
- if (rO > rQ && rO - rQ == cO - cQ) {
- if (cO < rightAntidiagonal[1]) {
- rightAntidiagonal = newValue;
- }
- }
- else if (rO == rQ) {
- if (cO < rightHorizontal[1]) {
- rightHorizontal = newValue;
- }
- }
- else if (rO < rQ && rQ - rO == cO - cQ) {
- if (cO < rightDiagonal[1]) {
- rightDiagonal = newValue;
- }
- }
- }
- }
- int leftDiagonalDistance = leftDiagonal[0] - rQ - 1;
- int topVerticalDistance = topVertical[0] - rQ - 1;
- int rightAntidiagonalDistance = rightAntidiagonal[0] - rQ - 1;
- int rightHorizontalDistance = rightHorizontal[1] - cQ - 1;
- int rightDiagonalDistance = rQ - rightDiagonal[0] - 1;
- int bottomVerticalDistance = rQ - bottomVertical[0] - 1;
- int leftAntidiagonalDistance = rQ - leftAntidiagonal[0] - 1;
- int leftHorizontalDistance = cQ - leftHorizontal[1] - 1;
- return leftDiagonalDistance +
- topVerticalDistance +
- rightAntidiagonalDistance +
- rightHorizontalDistance +
- rightDiagonalDistance +
- bottomVerticalDistance +
- leftAntidiagonalDistance +
- leftHorizontalDistance;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement