The Regional BPA competition was today, and I got 2nd in C++! (Well, there were 2 contestants… But in my defense, Nathan got 1st in National last year.) We got 1 hour, and I want to know what you guys think of mine. Optimizations, tips, etc.

So:

Checkerboard. Given a string input of coordinates with comma delimiters, evaluate the maximum possible black pieces a white piece can eat in 1 move.

Sounds easy, doesn't it? It wasn't. I spent 20 minutes trying to get the input working. In the end, I gave up and made an input system in which the user manually inputs coordinates, in a loop until the user inputs the escape value, 99 (no checkerboard has 99 rows or 99 columns). Here's my code:

#include <iostream> #include <cstdio> #include <algorithm> using namespace std; bool valid(int x, int y, int input[], int size) { if (x<0) {return false;} if (x>7) {return false;} for(int i = 2; i < size; i+=2) { if (x==input[i]) { if (y==input[i+1]) { return false; }} } return true; } int function(int input[], int size) { int temp; int x=input[0]; int y=input[1]; int number1=0; int number2=0; for(int i = 3; i < size; i+=2) { if (input[i]-1==y) { temp = input[i-1]; if ((temp-x)==1) { if (valid(x+2,y+2,input, size)) { input[0]=x+2; input[1]=y+2; number1 = 1+function(input, size); if (y==6) { cout << "King, "; } } } if ((x-temp)==1) { if (valid(x-2,y+2,input, size)) { input[0]=x+2; input[1]=y+2; number2 = 1+function(input, size); if (y==6) { cout << "King, "; } } } } } return max(number1, number2); //this is recursive. } int main() { int temp = 0; int i = 0; int input [99]; while (temp!=99) { //repeat until user inputs an exit number 99 cout << "Enter input (99 to exit):"; cin >> temp; input[i]=temp; i++; // i is the number of inputs } i--; //last input does not count if ((i%2)==1 or (i<4)) { cout << "invalid input."; return 0; } // must be even number of entries (coordinates), must have at least 1 black piece cout << function(input, i); return 0; }