Project Euler Problem #11
Posted
by SoulBeaver
on Stack Overflow
See other posts from Stack Overflow
or by SoulBeaver
Published on 2010-06-15T07:41:26Z
Indexed on
2010/06/15
7:42 UTC
Read the original article
Hit count: 317
c++
|project-euler
Source: http://projecteuler.net/index.php?section=problems&id=11
Quick overview: Take a 20x20 grid of numbers and compute the largest product of 4 pairs of numbers in either horizontal, vertical, or diagonal.
My current approach is to divide the 20x20 grid up into single rows and single columns and go from there with a much more manageable grid. The code I'm using to divide the rows into rows is
void fillRows
( string::const_iterator& fieldIter,
list<int>& rowElements,
vector<list<int>>& rows )
{
int count(0);
for( ; fieldIter < field.end(); ++fieldIter )
{
if(isdigit(field[*fieldIter]))
{
rowElements.push_back(toInt(field[*fieldIter]));
++count;
}
if(count == 40)
{
rows.push_back(rowElements);
count = 0;
rowElements.clear();
}
}
}
Short explanation:
I have the field set as static const std::string field
and I am filling a vector with lists of rows. Why a list? Because the queue doesn't have a clear function. Also practice using STL container lists and not ones I write myself.
However, this thing isn't working. Oftentimes I see it omitting a character( function toInt parses the const char as int ) and I end up with 18 rows, two rows short of the 20x20 grid. The length of the rows seem good.
Rows: 18
RowElements[0]: 40
(instead of pairs I saved each number individually. Will fix that later)
What am I doing wrong?
© Stack Overflow or respective owner