HackerRank Queen’s Attack II problem solution

In this HackerRank Queen’s Attack II problem You will be given a square chessboard with one queen and a number of obstacles placed on it. Determine how many squares the queen can attack.

HackerRank Queen's Attack II problem solution

Problem solution in Python programming.

n, k = map(int, input().split())
rq, cq = map(int, input().split())

moves = {'n': n - rq, 'nw': min(n - rq, cq-1), 'ne': min(n - rq, n - cq),
         's': rq-1, 'sw': min(rq-1, cq-1), 'se': min(rq-1, n - cq), 'w': cq-1, 'e': n - cq}
for _ in range(k):
    r, c = map(int, input().split())
    if c == cq:
        if r > rq:
            moves['n'] = min(r-rq-1, moves['n'])
        else:
            moves['s'] = min(rq-r-1, moves['s'])
    elif r == rq:
        if c > cq:
            moves['e'] = min(c-cq-1, moves['e'])
        else:
            moves['w'] = min(cq-c-1, moves['w'])
    elif c - r == cq - rq:
        if c < cq and r < rq:
            moves['sw'] = min(min(cq-c-1, rq-r-1), moves['sw'])
        elif c > cq and r > rq:
            moves['ne'] = min(min(c-cq-1, r-rq-1), moves['ne'])
    elif c + r == cq + rq:
        if c < cq and r > rq:
            moves['nw'] = min(min(r-rq-1, cq-c-1), moves['nw'])
        elif c > cq and r < rq:
            moves['se'] = min(min(rq-r-1, c-cq-1), moves['se'])
            
print(sum(moves.values()))

Problem solution in Java Programming.

import java.util.*;

public final class Solution {
  public static final void main(String[] args) {
    int n, rq, cq;
    Set<Long> o;
    try (Scanner in = new Scanner(System.in)) {
      n = in.nextInt();
      int k = in.nextInt();
      rq = in.nextInt();
      cq = in.nextInt();
      o = new HashSet<>(k);
      while (k --> 0) {
        int ro = in.nextInt(), co = in.nextInt();
        o.add((long)ro << 32 | co);
      }
    }
    int t = 0;
    for (int d[] : new int[][] {{-1, -1}, {-1,  0}, {-1, +1},
                                { 0, -1},           { 0, +1},
                                {+1, -1}, {+1,  0}, {+1, +1}}) {
      for (int r = rq + d[0], c = cq + d[1];
           1 <= r && r <= n && 1 <= c && c <= n && !o.contains((long)r << 32 | c);
           r += d[0], c += d[1]) {
        t++;
      }
    }
    System.out.println(t);
  }
}

Problem solution in C++ programming.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
int n;
using namespace std;
map<pair<int,int>,int> q;


int dl(int i1,int i2){
    int ret=0;
    while(i1<n && i2>1){
        i1++;
        i2--;
        if(q[{i1,i2}]) break;
        ret++;
    }
    return ret;
}
int up(int i1,int i2){
    int ret=0;
    while(i1>1){
        i1--;
        if(q[{i1,i2}]) break;
        ret++;
    }
    return ret;
}

int dw(int i1,int i2){
    int ret=0;
    while(i1<n){
        i1++;
        if(q[{i1,i2}]) break;
        ret++;
    }
    return ret;
}

int lf(int i1,int i2){
    int ret=0;
    while(i2>1){
        i2--;
        if(q[{i1,i2}]) break;
        ret++;
    }
    return ret;
}

int rg(int i1,int i2){
    int ret=0;
    while(i2<n){
        i2++;
        if(q[{i1,i2}]) break;
        ret++;
    }
    return ret;
}

int ur(int i1,int i2){
    int ret=0;
    while(i1>1 && i2<n){
        i1--;
        i2++;
        if(q[{i1,i2}]) break;
        ret++;
    }
    return ret;
}

int ul(int i1,int i2){
    int ret=0;
    while(i1>1 && i2>1){
        i1--;
        i2--;
        if(q[{i1,i2}]) break;
        ret++;
    }
    return ret;
}

int dr(int i1,int i2){
    int ret=0;
    while(i1<n && i2<n){
        i1++;
        i2++;
        if(q[{i1,i2}]) break;
        ret++;
    }
    return ret;
}
int main(){

    int k;
    cin >> n >> k;
    int f;
    int g;
    cin >> f >> g;
    for(int a0 = 0; a0 < k; a0++){
        int rObstacle;
        int cObstacle;
        cin >> rObstacle >> cObstacle;
        q[{rObstacle,cObstacle}]=1;
    }
    
    cout<<up(f,g)+dw(f,g)+lf(f,g)+rg(f,g)+ur(f,g)+ul(f,g)+dl(f,g)+dr(f,g);
    return 0;
}

Problem solution in C programming.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))

int rQueen; 
int cQueen;

int B[8] = {0, 0, 0, 0, 0, 0, 0 , 0};

enum { U, D, L, R, UR, DR, DL, UL };

int blocks(int r, int c) {
    if (r == rQueen) return c > cQueen ? R : L;
    if (c == cQueen) return r > rQueen ? U : D;
    if (abs(r - rQueen) == abs(c - cQueen)) {
        if (r < rQueen && c < cQueen) return DL;
        if (r < rQueen && c > cQueen) return DR;
        if (r > rQueen && c < cQueen) return UL;
        return UR;
    }
    return -1;
}

int main(){
    int n; 
    int k; 
    scanf("%d %d",&n,&k);
    scanf("%d %d",&rQueen,&cQueen);
    
    B[D]  = rQueen - 1;
    B[U]  = n - rQueen;
    B[L]  = cQueen - 1;
    B[R]  = n - cQueen;
    B[UL] = min(B[U], B[L]);
    B[UR] = min(B[U], B[R]);
    B[DL] = min(B[D], B[L]);
    B[DR] = min(B[D], B[R]);
    
    for(int a0 = 0; a0 < k; a0++){
        int rObstacle; 
        int cObstacle; 
        scanf("%d %d",&rObstacle,&cObstacle);
        int b = blocks(rObstacle, cObstacle);
        if (b != -1) {
            int nv = max(abs(rObstacle-rQueen), abs(cObstacle-cQueen)) - 1;
            if (nv < B[b]) B[b] = nv;
        }
    }
    int s = 0;
    for (int i = 0; i < 8; s += B[i++]) {}
    printf("%dn", s);
    return 0;
}

Problem solution in JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var n_temp = readLine().split(' ');
    var n = parseInt(n_temp[0]);
    var k = parseInt(n_temp[1]);
    var rQueen_temp = readLine().split(' ');
    var rQueen = parseInt(rQueen_temp[0]);
    var cQueen = parseInt(rQueen_temp[1]);
    
    var left = cQueen - 1;
    var right = n - cQueen;
    var above = n - rQueen;
    var below = rQueen - 1;
    var above_left = Math.min(above, left);
    var below_left = Math.min(below, left);
    var above_right = Math.min(above, right);
    var below_right = Math.min(below, right);

    
    for(var a0 = 0; a0 < k; a0++){
        var rObstacle_temp = readLine().split(' ');
        var rObstacle = parseInt(rObstacle_temp[0]);
        var cObstacle = parseInt(rObstacle_temp[1]);
        
        if (rObstacle === rQueen) {
            if (cObstacle < cQueen) {            
                left = Math.min(left, cQueen - cObstacle - 1);
            } else {
                right = Math.min(right, cObstacle - cQueen - 1);
            }
        } else if (cObstacle === cQueen) {
            if (rObstacle < rQueen) {
                below = Math.min(below, rQueen - rObstacle - 1);
            } else {
                above = Math.min(above, rObstacle - rQueen - 1);
            }
        } else if (rObstacle - cObstacle === rQueen - cQueen) {
            if (cObstacle < cQueen) {
                below_left = Math.min(below_left, cQueen - cObstacle - 1);
            } else {
                above_right = Math.min(above_right, cObstacle - cQueen - 1);
            }
        } else if (rObstacle + cObstacle === rQueen + cQueen) {
            if (cObstacle < cQueen) {
                above_left = Math.min(above_left, cQueen - cObstacle - 1);
            } else {
                below_right = Math.min(below_right, cObstacle - cQueen - 1);
            }
        }
        
        
    }

    console.log(left + right + above + below + above_left + below_left + above_right + below_right);
}