Skip to content

Instantly share code, notes, and snippets.

@EightAndAHalfTails
Created January 17, 2013 14:58
Show Gist options
  • Save EightAndAHalfTails/4556485 to your computer and use it in GitHub Desktop.
Save EightAndAHalfTails/4556485 to your computer and use it in GitHub Desktop.
Project Euler 15
#include <iostream>
#define HEIGHT 16
#define WIDTH 16
using namespace std;
int main(void)
{
bool done = false;
int steps=1;
int counters[HEIGHT]={0};
//Increment latest possible counter such that no earlier counter is larger than any later counter
while(counters[HEIGHT-1]!=WIDTH)
{
done = false;
steps++;
for(int i= HEIGHT-1; i>=0 && !done; i--)
{
//cout << "Checking position " << i << "..." << endl;
if(i!=0)
{
if(counters[i] < counters[i-1])
{
//cout << "incrementing position" << endl;
counters[i]++;
for(int j = i+1;j<HEIGHT;j++)
{
counters[j] = 0;
}
done = true;
}
}
else
{
counters[i]++;
for(int j = i+1;j<HEIGHT;j++)
{
counters[j] = 0;
}
}
}
/*for(int k=0; k<HEIGHT;k++)
{
cout << counters[k] << " ";
}
cout << endl;*/
}
cout << steps << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment