Queen's Attack II Discussions | Algorithms | HackerRank

Sort by

recency

|

987 Discussions

|

  • + 0 comments

    You are combining 2 numbers into single long digit. I wanted to know, how these tricks do people come to know? xxbrits

  • + 0 comments

    def queensAttack(n, k, r_q, c_q, obstacles):

    obs_map = {}
    for obs in obstacles:
        obs_map[obs[0], obs[1]] = 1
    
    deltas = [0, 1, -1]
    directions = []
    for i in deltas:
        for j in deltas:
            if i != 0 or j != 0:
                directions.append((i, j))
    
    
    count = 0
    for direction in directions:
        i, j = r_q + direction[0], c_q + direction[1]
        while obs_map.get((i, j)) is None and i > 0 and j > 0 and i < n+1 and j < n+1:
    
            count += 1
            i += direction[0]
            j += direction[1]
    return count
    
  • + 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;
    }
    
  • + 0 comments

    include

    include

    include

    include

    include

    include

    int main() {

    int n, k, rq, cq;
    
    scanf("%d%d", &n, &k);
    scanf("%d%d", &rq, &cq);
    
    int right = n - cq;
    int left = cq - 1;
    int up = n - rq;
    int down = rq - 1;
    
    int leftup, leftdown, rightup, rightdown;
    
    if (left < up) {
        leftup = left;
    } 
    else {
        leftup = up;
    }
    
    if (left < down) {
        leftdown = left;
    } 
    else {
        leftdown = down;
    }
    
    if (right < up) {
        rightup = right;
    } 
    else {
        rightup = up;
    }
    
    if (right < down) {
        rightdown = right;
    } 
    else {
        rightdown = down;
    }
    
    for (int i = 0; i < k; ++i) {
    
        int ro, co;
        scanf("%d%d", &ro, &co);
    
        if (rq == ro && cq > co) {
            if (cq - co - 1 < left) left = cq - co - 1;
        } 
        else if (rq == ro && cq < co) {
            if (co - cq - 1 < right) right = co - cq - 1;
        } 
        else if (cq == co && rq < ro) {
            if (ro - rq - 1 < up) up = ro - rq - 1;
        } 
        else if (cq == co && rq > ro) {
            if (rq - ro - 1 < down) down = rq - ro - 1;
        } 
        else if (co < cq && ro > rq && (cq - co) == (ro - rq)) {
            if (ro - rq - 1 < leftup) leftup = ro - rq - 1;
        } 
        else if (co < cq && rq > ro && (cq - co) == (rq - ro)) {
            if (cq - co - 1 < leftdown) leftdown = cq - co - 1;
        } 
        else if (co > cq && rq > ro && (co - cq) == (rq - ro)) {
            if (co - cq - 1 < rightdown) rightdown = co - cq - 1;
        } 
        else if (co > cq && rq < ro && (co - cq) == (ro - rq)) {
            if (ro - rq - 1 < rightup) rightup = ro - rq - 1;
        }
    }
    
    printf("%d", right + left + up + down + leftup + leftdown + rightup + rightdown);
    return 0;
    

    }

  • + 0 comments
    def queensAttack(n, k, r_q, c_q, obstacles):
        print(obstacles)
        obstacles = set(tuple(map(tuple, obstacles)))
        directions = [(1,-1), (1,0), (1,1), (0,-1), (0,1), (-1,-1), (-1,0), (-1,1)]
        print(dir)
        
        ans = 0
        for dy, dx in directions:
            y, x = r_q, c_q
            while True:
                y += dy
                x += dx
                if not (1 <= y <= n):
                    break
                if not (1 <= x <= n):
                    break
                if (y,x) in obstacles:
                    break
                ans +=1
        
        return ans