Queen's Attack II Discussions | Algorithms | HackerRank
  • + 0 comments

    JS/Javascript solution:-

    function queensAttack(n, k, r_q, c_q, obstacles) {
        let canQueenGo = 0;
        const obs = new Set();
        obstacles.forEach(v => obs.add(`${v[0]},${v[1]}`));
        function doTheAttack(dirX,dirY) {
            let posX = r_q + dirX;
            let posY = c_q + dirY;
            while (posX <= n && posY <= n && posX > 0 && posY > 0) {
                if (obs.has(`${posX},${posY}`)) return;
                canQueenGo++;
                posX += dirX;
                posY += dirY;
            }
        }
        for (let directionX = -1; directionX <= 1; directionX++) {
            for (let directionY = -1; directionY <= 1; directionY++) {
                if (directionY === 0 && directionX === 0) {   
                    continue;
                } else {
                    doTheAttack(directionX, directionY)
                }
            }
        }
        return canQueenGo;
    }