Neon_Falcon

Untitled

Apr 9th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include <stdlib.h>/* memory */
  2. #include <malloc.h>
  3. #include <stdio.h>
  4. #define n 9
  5.  
  6.  
  7. int fill(int (**array))
  8. {
  9.  
  10. char direction = 'E'; /* N - змейка двигалась наверх, W - налево,S - вниз, E - вправо */
  11. int x = 4, y = 4, d = 1;
  12. array[x][y] = d;
  13. while (d != 81)
  14. {
  15. switch (direction)
  16. {
  17. case 'N':
  18. if ((array[x - 1][y]) > 0)
  19. {
  20. d++;
  21. y--;
  22. array[x][y] = d;
  23. }
  24. else
  25. {
  26. d++;
  27. x--;
  28. array[x][y] = d;
  29. direction = 'W';
  30. }
  31. break;
  32.  
  33. case 'W':
  34. if ((array[x][y+1]) > 0)
  35. {
  36. d++;
  37. x--;
  38. array[x][y] = d;
  39. }
  40. else
  41. {
  42. d++;
  43. y++;
  44. array[x][y] = d;
  45. direction = 'S';
  46. }
  47. break;
  48.  
  49. case 'S':
  50. if ((array[x+1][y]) > 0)
  51. {
  52. d++;
  53. y++;
  54. array[x][y] = d;
  55. }
  56. else
  57. {
  58. d++;
  59. x++;
  60. array[x][y] = d;
  61. direction = 'E';
  62. }
  63. break;
  64.  
  65. case 'E':
  66. if ((array[x][y-1]) > 0)
  67. {
  68. d++;
  69. x++;
  70. array[x][y] = d;
  71. }
  72. else
  73. {
  74. d++;
  75. y--;
  76. array[x][y] = d;
  77. direction = 'N';
  78. }
  79. break;
  80. }
  81. }
  82. return 0;
  83. }
  84.  
  85. int print(int(**array))
  86. {
  87. for (int o = 0; o < 9; o++) {
  88. for (int p = 0; p < 9; p++) {
  89. if (array[p][o] < 10)
  90. {
  91. printf("%d ", array[p][o]);
  92. }
  93. else {
  94. printf("%d ", array[p][o]);
  95. }
  96.  
  97. }
  98. printf("\n");
  99. }
  100. return 0;
  101. }
  102. int main()
  103. {
  104. int** array;
  105. int i, j, k;
  106. array = (int**)malloc(sizeof(int*) * n); /* создание ячеек памяти для работы */
  107. for (i = 0; i < n; i++)
  108. {
  109. array[i] = (int*)malloc(sizeof(int*) * n);
  110. }
  111. fill(array); /* заполнение массива */
  112. print(array);
  113. for (i = 0; i < n; i++) /*удаление массива*/
  114. {
  115. free(array[i]);
  116. }
  117. free(array);
  118. system("pause");
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment