TopCoder problem "RedSquare" used in SRM 198 (Division I Level One , Division II Level Two)



Problem Statement

    

Checkerboards are used for many things besides playing checkers. In fact, the word "checkerboard" is in much wider use, and has more meanings, than the word "checker". In most cases, including here, a checkerboard is a pattern consisting of a rectangular array of squares where adjacent squares (horizontally and vertically) are different colors. There are two colors of squares on our checkerboards, red and black. The squares in our checkerboard are numbered vertically, from 1 to maxRank inclusive, with 1 being at the bottom. The coordinate in this vertical direction is called the rank. The squares in our checkerboard are also numbered horizontally, from 1 to maxFile inclusive, with 1 on the left. The coordinate in this horizontal direction is called the file. The location of a square is specified by the pair of coordinates: (rank, file)

On all of the checkerboards in this problem, the RIGHT-most square in the BOTTOM-most rank, (1, maxFile), IS ALWAYS BLACK.

Given the height and width (maxRank and maxFile) of our checkerboard, and the coordinates of all of the game pieces on the checkerboard, return the count of all the red squares which are empty. A square is empty if no game piece is on that square. The locations of the game pieces will be specified with two int[]s, rank and file. (rank[0], file[0]) is the location of piece 0, (rank[1], file[1]) is the location of piece 1, etc.

For example:

maxRank = 3, maxFile = 5, rank = {1, 2, 2}, file = {4, 1, 2}

Our checkerboard would look like this:

b R b R b   where "b" represents an empty black square,
x x R b R         "R" represents an empty red square and
b R b x b         "x" represents the location of a game piece

There are 5 empty red squares, so you return 5.

 

Definition

    
Class:RedSquare
Method:countTheEmptyReds
Parameters:int, int, int[], int[]
Returns:int
Method signature:int countTheEmptyReds(int maxRank, int maxFile, int[] rank, int[] file)
(be sure your method is public)
    
 

Constraints

-maxRank will be between 1 and 50 inclusive.
-maxFile will be between 1 and 50 inclusive.
-rank will contain between 0 and 50 elements, inclusive.
-rank will contain between 0 and maxRank*maxFile elements, inclusive.
-file will contain exactly the same number of elements as rank
-each element of rank will be between 1 and maxRank inclusive.
-each element of file will be between 1 and maxFile inclusive.
-All game piece locations (rank[i], file[i]) will be distinct.
 

Examples

0)
    
3
5
{1, 2, 2}
{4, 1, 2}
Returns: 5
The example from above.
1)
    
3
3
{1,2,3,1,2,3,1,2,3}
{1,1,1,2,2,2,3,3,3}
Returns: 0
A full board, no empty squares of either color.
2)
    
5
5
{1,1,2,2,2,3,3,4,4,4,5,5}
{2,4,1,3,5,2,4,1,3,5,2,4}
Returns: 0
All 12 red squares are occupied, none are empty.
3)
    
5
6
{1,1,2,2,2,3,3,4,4,4,5,5}
{2,4,1,3,5,2,4,1,3,5,2,4}
Returns: 15
12 black squares are occupied, but all 15 red squares are empty.
4)
    
1
1
{}
{}
Returns: 0
One black square, no red squares.
5)
    
50
50
{1}
{1}
Returns: 1249
One piece, which is on a red square.
6)
    
50
50
{1,2,3,4,5,6,7,8,9,10,
 1,2,3,4,5,6,7,8,9,10,
 1,2,3,4,5,6,7,8,9,10,
 1,2,3,4,5,6,7,8,9,10,
 1,2,3,4,5,6,7,8,9,10}
{1,1,1,1,1,1,1,1,1,1,
 2,2,2,2,2,2,2,2,2,2,
 3,3,3,3,3,3,3,3,3,3,
 4,4,4,4,4,4,4,4,4,4,
 5,5,5,5,5,5,5,5,5,5}
Returns: 1225

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=2891

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=5073&pm=2891

Writer:

Rustyoldman

Testers:

brett1479 , schveiguy

Problem categories:

Simple Search, Iteration