Last active
November 28, 2018 17:42
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Square { | |
public Square( boolean isBlack, int num ) { | |
/* Implementation not shown */ | |
} | |
// There may be instance variables, constructors, and | |
// methods that are not shown | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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