I need to create a table (using multidimensional array/vector) that contains datas in a spiral form. if you dont know how spiral looks like, http://images.google.com/images?q=spiral
Here's my work so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int current_char = 0, rows = 3, columns = 4;
string input = "abcdefghijkl";
vector< vector<string> > str_table(rows, vector<string>(columns));
for(int i = 0; i < (int)input.length(); i++)
{
splitted_input.push_back(input.substr(i, 1));
}
for(int x = 0; x < rows; x++)
{
for(int y = 0; y < columns; y++)
{
str_table[x][y] = splitted_input.at(current_char);
current_char++;
}
}
Yes, but if you work with a couple of large grids by hand, you'll have gone thru it enough times to "know" what to do. You just implement that algorithm. You really should try it.
as kbw said, you need to run it by hand, get the values(rows,columns) that change as you fill the spiral, find out how they change(key point)
i did it with a 2x3 grid, grid size dont matter, let me give you a tip,
you need 4 variables to store what has been filled. (startrow,startcolumn,endrow,endcolumn)
you need 4 loops(for)
one going from left(1st available column-startcolumn-) to right(last available column-endcolumn-) on first available row.
one going from first row til last on the last available column.
one going from right(last available column) to left(1st available column) on last available row.
one going from last available row til 1st available row on the first available column.