algorithms/implementation/queens-attack-2.c
int queensAttack(int n, int k, int r_q, int c_q, int obstacles_rows, int obstacles_columns, int** obstacles)
{

    int r_q_lo = 1;
    int r_q_hi = n;
    int c_q_lo = 1;
    int c_q_hi = n;

    int r_qi = n - r_q + 1;
    int c_qi = n - c_q + 1;

    // TODO: real diagonal coordinates
    int d1_r_lo = r_q + 1 - min(r_q, c_q);
    int d1_c_lo = c_q + 1 - min(r_q, c_q);
    int d1_r_hi = r_q + n - max(r_q, c_q);
    int d1_c_hi = c_q + n - max(r_q, c_q);

    int d2_r_lo =  r_q  + 1 - min(r_q, c_qi);
    int d2_c_lo = -c_qi + n + min(r_q, c_qi);
    int d2_r_hi =  r_q  + n - max(r_q, c_qi);
    int d2_c_hi = c_q - (n - max(r_q, c_qi));

    printf("lo d1: %d %d\n", d1_r_lo, d1_c_lo);
    printf("hi d1: %d %d\n", d1_r_hi, d1_c_hi);
    printf("lo d2: %d %d\n", d2_r_lo, d2_c_lo);
    printf("hi d2: %d %d\n", d2_r_hi, d2_c_hi);

    for (int j = 0; j < k; j++)
    {
        int r_test = obstacles[j][0];
        int c_test = obstacles[j][1];

        // row blocks
        if (r_test == r_q)
        {
            if (c_test < c_q)
            {
                c_q_lo = max(c_q_lo, c_test + 1);
            }
            else
            {
                c_q_hi = min(c_q_hi, c_test - 1);
            }
        }

        // column blocks
        if (c_q == c_test)
        {
            if (r_test < r_q)
            {
                r_q_lo = max(r_q_lo, r_test + 1);
            }
            else
            {
                r_q_hi = min(r_q_hi, r_test - 1);
            }
        }

        // diagonal 1
        if (r_test - r_q == c_test - c_q)
        {
            if (r_test < r_q)
            {
                if (r_test + 1 > d1_r_lo)
                {
                    d1_r_lo = r_test + 1;
                    d1_c_lo = c_test + 1;
                }
            }
            else
            {
                if (r_test - 1 < d1_r_hi)
                {
                    d1_r_hi = r_test - 1;
                    d1_c_hi = c_test - 1;
                }
            }
        }

        // diagonal 2
        if (r_test - r_q == c_q - c_test)
        {
            if (r_test < r_q)
            {
                if (r_test + 1 > d2_r_lo)
                {
                    d2_r_lo = r_test + 1;
                    d2_c_lo = c_test - 1;
                }
            }
            else
            {
                if (r_test - 1 < d2_r_hi)
                {
                    d2_r_hi = r_test - 1;
                    d2_c_hi = c_test + 1;
                }
            }
        }

    }

    int total = 0;

    total += r_q_hi - r_q_lo;
    total += c_q_hi - c_q_lo;
    total += d1_r_hi - d1_r_lo;
    total += d2_r_hi - d2_r_lo;

    return total;
}
// Complete the queensAttack function below.
Complete the queensAttack function below.
Complete the queensAttack function below .
int queensAttack(int n, int k, int r_q, int c_q, int obstacles_rows, int obstacles_columns, int** obstacles)
{
    int r_q_lo = 1;
    int r_q_hi = n;
    int c_q_lo = 1;
    int c_q_hi = n;
    int r_qi = n - r_q + 1;
    int c_qi = n - c_q + 1;
    int d1_r_lo = r_q + 1 - min(r_q, c_q);
    int d1_c_lo = c_q + 1 - min(r_q, c_q);
    int d1_r_hi = r_q + n - max(r_q, c_q);
    int d1_c_hi = c_q + n - max(r_q, c_q);
    int d2_r_lo =  r_q  + 1 - min(r_q, c_qi);
    int d2_c_lo = -c_qi + n + min(r_q, c_qi);
    int d2_r_hi =  r_q  + n - max(r_q, c_qi);
    int d2_c_hi = c_q - (n - max(r_q, c_qi));
    printf("lo d1: %d %d\n", d1_r_lo, d1_c_lo);
    printf("hi d1: %d %d\n", d1_r_hi, d1_c_hi);
    printf("lo d2: %d %d\n", d2_r_lo, d2_c_lo);
    printf("hi d2: %d %d\n", d2_r_hi, d2_c_hi);
    for (int j = 0; j < k; j++)
    {
        int r_test = obstacles[j][0];
        int c_test = obstacles[j][1];
        if (r_test == r_q)
        {
            if (c_test < c_q)
            {
                c_q_lo = max(c_q_lo, c_test + 1);
            }
            else
            {
                c_q_hi = min(c_q_hi, c_test - 1);
            }
        }
        if (c_q == c_test)
        {
            if (r_test < r_q)
            {
                r_q_lo = max(r_q_lo, r_test + 1);
            }
            else
            {
                r_q_hi = min(r_q_hi, r_test - 1);
            }
        }
        if (r_test - r_q == c_test - c_q)
        {
            if (r_test < r_q)
            {
                if (r_test + 1 > d1_r_lo)
                {
                    d1_r_lo = r_test + 1;
                    d1_c_lo = c_test + 1;
                }
            }
            else
            {
                if (r_test - 1 < d1_r_hi)
                {
                    d1_r_hi = r_test - 1;
                    d1_c_hi = c_test - 1;
                }
            }
        }
        if (r_test - r_q == c_q - c_test)
        {
            if (r_test < r_q)
            {
                if (r_test + 1 > d2_r_lo)
                {
                    d2_r_lo = r_test + 1;
                    d2_c_lo = c_test - 1;
                }
            }
            else
            {
                if (r_test - 1 < d2_r_hi)
                {
                    d2_r_hi = r_test - 1;
                    d2_c_hi = c_test + 1;
                }
            }
        }
    }
    int total = 0;
    total += r_q_hi - r_q_lo;
    total += c_q_hi - c_q_lo;
    total += d1_r_hi - d1_r_lo;
    total += d2_r_hi - d2_r_lo;
    return total;
}
int queensAttack ( int n , int k , int r_q , int c_q , int obstacles_rows , int obstacles_columns , int * * obstacles ) { int r_q_lo = 1 ; int r_q_hi = n ; int c_q_lo = 1 ; int c_q_hi = n ; int r_qi = n - r_q + 1 ; int c_qi = n - c_q + 1 ; int d1_r_lo = r_q + 1 - min ( r_q , c_q ) ; int d1_c_lo = c_q + 1 - min ( r_q , c_q ) ; int d1_r_hi = r_q + n - max ( r_q , c_q ) ; int d1_c_hi = c_q + n - max ( r_q , c_q ) ; int d2_r_lo = r_q + 1 - min ( r_q , c_qi ) ; int d2_c_lo = - c_qi + n + min ( r_q , c_qi ) ; int d2_r_hi = r_q + n - max ( r_q , c_qi ) ; int d2_c_hi = c_q - ( n - max ( r_q , c_qi ) ) ; printf ( " \n " , d1_r_lo , d1_c_lo ) ; printf ( " \n " , d1_r_hi , d1_c_hi ) ; printf ( " \n " , d2_r_lo , d2_c_lo ) ; printf ( " \n " , d2_r_hi , d2_c_hi ) ; for ( int j = 0 ; j < k ; j ++ ) { int r_test = obstacles [ j ] [ 0 ] ; int c_test = obstacles [ j ] [ 1 ] ; if ( r_test == r_q ) { if ( c_test < c_q ) { c_q_lo = max ( c_q_lo , c_test + 1 ) ; } else { c_q_hi = min ( c_q_hi , c_test - 1 ) ; } } if ( c_q == c_test ) { if ( r_test < r_q ) { r_q_lo = max ( r_q_lo , r_test + 1 ) ; } else { r_q_hi = min ( r_q_hi , r_test - 1 ) ; } } if ( r_test - r_q == c_test - c_q ) { if ( r_test < r_q ) { if ( r_test + 1 > d1_r_lo ) { d1_r_lo = r_test + 1 ; d1_c_lo = c_test + 1 ; } } else { if ( r_test - 1 < d1_r_hi ) { d1_r_hi = r_test - 1 ; d1_c_hi = c_test - 1 ; } } } if ( r_test - r_q == c_q - c_test ) { if ( r_test < r_q ) { if ( r_test + 1 > d2_r_lo ) { d2_r_lo = r_test + 1 ; d2_c_lo = c_test - 1 ; } } else { if ( r_test - 1 < d2_r_hi ) { d2_r_hi = r_test - 1 ; d2_c_hi = c_test + 1 ; } } } } int total = 0 ; total += r_q_hi - r_q_lo ; total += c_q_hi - c_q_lo ; total += d1_r_hi - d1_r_lo ; total += d2_r_hi - d2_r_lo ; return total ; }
Complete the queensAttack function below.
Complete the queensAttack function below .
// TODO: real diagonal coordinates // row blocks // column blocks // diagonal 1 // diagonal 2