I think I understand what you're saying. I make the 4 steps. When "Up" finishes, there is a "have been there" in one position upper (row), so it has to turn right again and the other 3 steps, and continue doing that.. isn't it?
Yes!
And since there is ALWAYS another empty square in this pirouette pattern, until they're all full, only one loop is needed.
OK. So, if you're saying that, I don't understand the code you wrote
I don't know what to do. For Sunday afternoon (today) I have to finish this program and 2 more, but I don't know how to do this one, I can't think it in the correct way, I suppose
Last edited by juanjuanjuan; 02-15-2014 at 10:01 PM.
Speaking of spirals, I would think that the answer has something to do with polar coordinates and traversing roughly the points specified by an Archimedes spiral.
Finally, I did it! It works!
Thanks!!!!
Although the question is solved, perhaps the following code may inspire a different approach to the problem. Whether a different method is "better" or "worse" than the maze walk is irrelevant because Hodor likes trying to achieve the same result in different ways. I haven't posted a complete solution, the code below is not a solution at all, but maybe it will trigger an idea.
I will start by saying that multidimensional arrays irk me for some reason; I honestly prefer working with just one dimension and "project" that single dimension in different ways.
Code:#include <stdio.h> #include <stdlib.h> enum matrixorder { ROWMAJOR, COLMAJOR, ROWMAJOR_R, COLMAJOR_R }; void int_swap(int *a, int *b); int *matrix_init(int *matrix, int w, int h); void matrix_print(int *matrix, enum matrixorder order, int w, int h); int matrix_getelem_rowmajor(int *matrix, int w, int h, int x, int y); int matrix_getelem_rowmajor_rev(int *matrix, int w, int h, int x, int y); int matrix_getelem_colmajor(int *matrix, int w, int h, int x, int y); int matrix_getelem_colmajor_rev(int *matrix, int w, int h, int x, int y); int main(void) { int *matrix; const int w = 4, h = 2; matrix = malloc(sizeof *matrix * w * h); if (!matrix) { perror("Could not allocate memory for matrix"); exit(1); } matrix_init(matrix, w, h); matrix_print(matrix, ROWMAJOR, w, h); printf("-------------------\n"); matrix_print(matrix, ROWMAJOR_R, w, h); printf("-------------------\n"); matrix_print(matrix, COLMAJOR, w, h); printf("-------------------\n"); matrix_print(matrix, COLMAJOR_R, w, h); free(matrix); return 0; } void int_swap(int *a, int *b) { int t; t = *a; *a = *b, *b = t; } int *matrix_init(int *matrix, int w, int h) { int row, col, i; /* Initialise with test data */ for (i = row = 0; row < h; row++) for (col = 0; col < w; col++) matrix[row * w + col] = ++i; return NULL; } void matrix_print(int *matrix, enum matrixorder order, int w, int h) { int row, col; if (order == COLMAJOR || order == COLMAJOR_R) int_swap(&w, &h); for (row = 0; row < h; row++) { for (col = 0; col < w; col++) { int val; if (order == ROWMAJOR) val = matrix_getelem_rowmajor(matrix, w, h, col, row); else if (order == ROWMAJOR_R) val = matrix_getelem_rowmajor_rev(matrix, w, h, col, row); else if (order == COLMAJOR) val = matrix_getelem_colmajor(matrix, w, h, col, row); else val = matrix_getelem_colmajor_rev(matrix, w, h, col, row); printf("%4d", val); } printf("\n"); } } int matrix_getelem_rowmajor(int *matrix, int w, int h, int x, int y) { return matrix[x + y * w]; } int matrix_getelem_rowmajor_rev(int *matrix, int w, int h, int x, int y) { x = w - x - 1; y = h - y - 1; return matrix_getelem_rowmajor(matrix, w, h, x, y); } int matrix_getelem_colmajor(int *matrix, int w, int h, int x, int y) { int_swap(&w, &h); return matrix[y + x * w]; } int matrix_getelem_colmajor_rev(int *matrix, int w, int h, int x, int y) { x = w - x - 1; y = h - y - 1; return matrix_getelem_colmajor(matrix, w, h, x, y); }
O_oWhether a different method is "better" or "worse" than the maze walk is irrelevant because walking pickle pastries likes trying to achieve the same result in different ways.
Type with sense man...
Soma
“Salem Was Wrong!” -- Pedant Necromancer
“Four isn't random!” -- Gibbering Mouther
I can't. Unfortunately my pirouette resembles scrambled eggs gone wrong. O_o
Code:#include <stdio.h> #include <stdlib.h> int *matrix_init(int *matrix, int w, int h); void matrix_print(int *matrix, int w, int h); void matrix_pirouette(int *matrix, int w, int h); int main(void) { int *matrix; const int w = 4, h = 3; matrix = malloc(sizeof *matrix * w * h); if (!matrix) { perror("Could not allocate memory for matrix"); exit(1); } matrix_init(matrix, w, h); matrix_print(matrix, w, h); printf("\n"); matrix_pirouette(matrix, w, h); free(matrix); return 0; } int *matrix_init(int *matrix, int w, int h) { int row, col, i; /* Initialise with test data */ for (i = row = 0; row < h; row++) for (col = 0; col < w; col++) matrix[row * w + col] = ++i; return NULL; } void matrix_print(int *matrix, int w, int h) { int row, col; for (row = 0; row < h; row++) { for (col = 0; col < w; col++) printf("%4d", matrix[row * w + col]); printf("\n"); } } void matrix_pirouette(int *matrix, int w, int h) { int nelems; int i, j, origin_x, origin_y; int totalw; nelems = w * h; totalw = w; i = 0; origin_x = origin_y = 0; while (i < nelems) { for (j = 0; j < w; j++) { printf("%d ", matrix[origin_x + j + origin_y * totalw]); i++; } if (i == nelems) break; for (j = 1; j < h - 1; j++) { printf("%d ", matrix[origin_x + w - 1 + (origin_y + j) * totalw]); i++; } if (i == nelems) break; for (j = w - 1; j >= 0; j--) { printf("%d ", matrix[origin_x + j + (origin_y + h - 1) * totalw]); i++; } if (i == nelems) break; for (j = h - 2; j > origin_y; j--) { printf("%d ", matrix[origin_x + (origin_y + j) * totalw]); i++; } if (i == nelems) break; w -= 2; h -= 2; if (w < 0) w = 0; if (h < 0) h = 0; origin_y++; if (w == 0) { /* yuck */ printf("%d ", matrix[origin_x + origin_y * totalw]); i++; } origin_x++; } printf("\n"); }