Queen's Attack II Discussions | Algorithms | HackerRank
  • + 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;
    

    }