Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>/* memory */
- #include <malloc.h>
- #include <stdio.h>
- #define n 9
- int fill(int (**array))
- {
- char direction = 'E'; /* N - змейка двигалась наверх, W - налево,S - вниз, E - вправо */
- int x = 4, y = 4, d = 1;
- array[x][y] = d;
- while (d != 81)
- {
- switch (direction)
- {
- case 'N':
- if ((array[x - 1][y]) > 0)
- {
- d++;
- y--;
- array[x][y] = d;
- }
- else
- {
- d++;
- x--;
- array[x][y] = d;
- direction = 'W';
- }
- break;
- case 'W':
- if ((array[x][y+1]) > 0)
- {
- d++;
- x--;
- array[x][y] = d;
- }
- else
- {
- d++;
- y++;
- array[x][y] = d;
- direction = 'S';
- }
- break;
- case 'S':
- if ((array[x+1][y]) > 0)
- {
- d++;
- y++;
- array[x][y] = d;
- }
- else
- {
- d++;
- x++;
- array[x][y] = d;
- direction = 'E';
- }
- break;
- case 'E':
- if ((array[x][y-1]) > 0)
- {
- d++;
- x++;
- array[x][y] = d;
- }
- else
- {
- d++;
- y--;
- array[x][y] = d;
- direction = 'N';
- }
- break;
- }
- }
- return 0;
- }
- int print(int(**array))
- {
- for (int o = 0; o < 9; o++) {
- for (int p = 0; p < 9; p++) {
- if (array[p][o] < 10)
- {
- printf("%d ", array[p][o]);
- }
- else {
- printf("%d ", array[p][o]);
- }
- }
- printf("\n");
- }
- return 0;
- }
- int main()
- {
- int** array;
- int i, j, k;
- array = (int**)malloc(sizeof(int*) * n); /* создание ячеек памяти для работы */
- for (i = 0; i < n; i++)
- {
- array[i] = (int*)malloc(sizeof(int*) * n);
- }
- fill(array); /* заполнение массива */
- print(array);
- for (i = 0; i < n; i++) /*удаление массива*/
- {
- free(array[i]);
- }
- free(array);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment