Crossword free response answer 2

Crossword free response problem from the 2016 AP Computer Science A Exam.

Crossword is #3 from the from the 2016 AP Computer Science A Free Response problems.

https://secure-media.collegeboard.org/digitalServices/pdf/ap/ap16_frq_computer_science_a.pdf

Part (a) – toBeLabeled method

private boolean toBeLabeled(int r, int c, boolean[][] blackSquares)
{
  if(blackSquares[r][c])
    return false;

  if(r == 0 || blackSquares[r - 1][c])
    return true;

  if(c == 0 || blackSquares[r][c - 1])
    return true;

  return false;
}

It is not acceptable to write the opposite checks and return false. The problem specifies that either or both of the conditions could be true for the square to be labeled.

Outside of an exam setting I would write this as:

return ! blackSquares[r][c] &&
    (r == 0 || blackSquares[r-1][c] || c == 0 || blackSquares[r][c-1]);

Part (b) – CrossWord constructor

public Crossword(boolean[][] blackSquares)
{
  puzzle = new Square[blackSquares.length][blackSquares[0].length];
  
  int num = 1;
  
  for(int r = 0; r < puzzle.length; r++)
  {
    for(int c = 0; c < puzzle[0].length; c++)
    {
      if(toBeLabeled(r, c, blackSquares))
      {
        puzzle[r][c] = new Square(false, num);
        num++;
      }
      else
        puzzle[r][c] = new Square(blackSquares[r][c], 0);
    }
  }
}

2 thoughts on “Crossword free response answer

  1. KaSa May 7,2016 7:35 am

    My only trouble with this question was what to if the square is black or if it doesn’t satisfy the requirements. How did you deduce that we would create another square regardless. Thanks

    • Brandon Horn May 11,2016 6:11 pm

      The instructions for part (b) state “Each element of the puzzle grid should be initialized with a reference to a Square object with the appropriate color and number.”

Comments are closed.