// NAME                       :     Angus Gibbs

// SCHOOL               :     Kalamazoo Math Science

// DIVISION             :     Intermediate-5

// PROBLEM              :     Chess Queen Intermediate

// ROUND                :     3

// DESCRIPTION          :     Finds how many pieces are safe from being taken

//                                  by a queen on a chess piece.

 

#include <iostream>

 

using namespace std;

 

int abs(int);

 

int main()

{

      // Signature

      cout << "Angus Gibbs" << endl

             << "AP CS" << endl

             << "ACSL Chess Queen Intermediate" << endl

             << endl << endl << endl;

 

      // Declare variables

      const int ROWS = 8;

      const int COLS = 8;

      int range, queenX[2], queenY[2], numSafe;

      bool isSafe;

 

      // Run 5 times

      for (int run = 0; run < 5; run++)

      {

            // Get the input data

            cout << "Enter the board data:  ";

            cin >> queenY[0] >> queenX[0]

                  >> queenY[1] >> queenX[1]

                  >> range;

 

            // Reset the number of safe cells

            numSafe = 0;

 

            // Go through each location on the board

            for (int row = 1; row <= ROWS; row++)

            {

                  for (int col = 1; col <= COLS; col++)

                  {

                        // Ignore if this is the coordinates of one of the queens

                        if (row == queenY[0] && col == queenX[0] ||

                              row == queenY[1] && col == queenX[1])

                        {

                              continue;

                        }

 

                        // Assume it is safe

                        isSafe = true;

 

                        // Check both queens

                        for (int q = 0; q < 2; q++)

                        {

                              // Check if it's in the same row within the range

                              if (abs(row - queenY[q]) <= range && col == queenX[q])

                              {

                                    isSafe = false;

                                    break;

                              }

 

                              // Check if it's in the same column within the range

                              if (abs(col - queenX[q]) <= range && row == queenY[q])

                              {

                                    isSafe = false;

                                    break;

                              }

 

                              // Check if it's in the same diagonal within the range

                              if (((row - queenY[q]) == (col - queenX[q]) ||

                                    (row - queenY[q]) == -(col - queenX[q])) &&

                                    abs(row - queenY[q]) <= range)

                              {

                                    isSafe = false;

                                    break;

                              }

                        }

 

                        // Increment the counter if it's safe

                        if (isSafe)

                        {

                              numSafe++;

                        }

                  }

            }

 

            // Output answer

            cout << endl << numSafe << endl << endl;

      }

 

      // Exit

      return 0;

}

 

int abs(int a)

{

      return a > 0 ? a : -1 * a;

}