Skip to content

Instantly share code, notes, and snippets.

@CompSciRocks
Last active November 28, 2018 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CompSciRocks/2073a20c7bc2fb89762e4ffe20533ae5 to your computer and use it in GitHub Desktop.
Save CompSciRocks/2073a20c7bc2fb89762e4ffe20533ae5 to your computer and use it in GitHub Desktop.
Working solution to the 2016 AP Computer Science Crossword free response question :: https://compsci.rocks/crossword-solution/
public Crossword(boolean[][] blackSquares) {
puzzle = new Square[blackSquares.length][blackSquares[0].length];
int num = 1;
for (int r=0; r<blackSquares.length; r++) {
for (int c=0; c<blackSquares[r].length; c++) {
if (blackSquares[r][c]) {
puzzle[r][c] = new Square(true, 0);
}
else {
if (toBeLabeled(r, c, blackSquares) {
puzzle[r][c] = new Square(false, num);
num++;
}
else {
puzzle[r][c] = new Square(false, 0);
}
}
}
}
public class Crossword {
private Square[][] puzzle;
private boolean toBeLabeled( int r, int c, boolean[][] blackSquares ) {
return (!(blackSquares[r][c]) &&
( r == 0 || c == 0 || blackSquares[r-1][c] || blackSquares[r][c-1] );
}
public Crossword(boolean[][] blackSquares) {
puzzle = new Square[blackSquares.length][blackSquares[0].length];
int num = 1;
for (int r=0; r<blackSquares.length; r++) {
for (int c=0; c<blackSquares[r].length; c++) {
if (blackSquares[r][c]) {
puzzle[r][c] = new Square(true, 0);
}
else {
if (toBeLabeled(r, c, blackSquares) {
puzzle[r][c] = new Square(false, num);
num++;
}
else {
puzzle[r][c] = new Square(false, 0);
}
}
}
}
}
public class Square {
public Square( boolean isBlack, int num ) {
/* Implementation not shown */
}
// There may be instance variables, constructors, and
// methods that are not shown
}
private boolean toBeLabeled( int r, int c, boolean[][] blackSquares ) {
return (!(blackSquares[r][c]) && ( r == 0 || c == 0 || blackSquares[r-1][c] || blackSquares[r][c-1] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment