1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
void
fillSquare(const string &str, Board &board, size_t pos, int startR, int startC, int size)
{
int r = startR, c = startC;
int i;
if (size == 1) {
board[r][c] = str[pos];
return;
}
// Fill the square from the top left corner going clockwise.
// each loop below does one side
--size;
for (i=0; i<size && pos < str.size(); ++i) { // top
board[r][c++] = str[pos++];
}
for (i=0; i<size && pos < str.size(); ++i) { // right
board[r++][c] = str[pos++];
}
for (i=0; i<size && pos < str.size(); ++i) { // bottom
board[r][c--] = str[pos++];
}
for (i=0; i<size && pos < str.size(); ++i) { // left
board[r--][c] = str[pos++];
}
if (pos < str.size()) {
fillSquare(str, board, pos, startR+1, startC+1, size-1);
}
}
void
fillSpiral(const string &str, Board &board, int size)
{
fillSquare(str, board, 0, 0, 0, size);
}
| |