-
Code Challenge Solution
Here's a simple solution to the Level1, Easy TopCoder Challenge.
- Kim Dam Petersen, email: kimdam@petersen.mail.dk
-- The solution goes here...
/*
* Created on 13-07-2004
*
* Topcoder Challenge: RedSquare.
*/
package challenge;
/**
* <p>RedSquare counts the number of free red squares on a checker
* board with a black square on (1,maxFile).</p>
*
* @author Kim Dam Petersen
*/
public class RedSquare
{
/** Count number of free red squares.
*
* @param maxRank vertical size.
* @param maxFile horizontal size.
* @param rank 1-based vertical position of pieces.
* @param file 1-based horizontal position of pieces.
* @return
*/
public int countTheEmptyReds(int maxRank, int maxFile, int[] rank, int[] file) {
int blackOddity = (1 + maxFile) & 1;
int redCount = (maxRank * maxFile) / 2;
for (int i= 0; i<rank.length; i++) {
int pieceOddity = (rank[i] + file[i]) & 1;
if (pieceOddity != blackOddity) {
redCount--;
}
}
return redCount;
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks