fork download
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. int flag = 0;
  6. struct parent
  7. {
  8. int row;
  9. int col;
  10. };
  11.  
  12. void check(int ar[5][5], int row, int col, struct parent pos[30],int *num)
  13. {
  14. if (col - 1 >= 0 && row - 1 >= 0) //Checking Upper-left
  15. {
  16. if (ar[row - 1][col - 1] == 1) {
  17. ar[row - 1][col - 1] = -1;
  18. (*num)+=1;
  19. pos[*num].row = row; pos[*num].col = col;
  20. check(ar, row - 1, col - 1, pos, num);
  21. }
  22. }
  23. if (col - 1 >= 0) //Checking Left
  24. {
  25. if (ar[row][col - 1] == 1) {
  26. ar[row][col - 1] = -1;
  27. (*num)+=1;
  28. pos[*num].row = row; pos[*num].col = col;
  29. check(ar, row, col - 1, pos, num);
  30. }
  31. }
  32. if (col - 1 >= 0 && row + 1 <= 4) //Checking Bottom-Left
  33. {
  34. if (ar[row + 1][col - 1] == 1) {
  35. ar[row + 1][col - 1] = -1;
  36. (*num)+=1;
  37. pos[*num].row = row; pos[*num].col = col;
  38. check(ar, row + 1, col - 1, pos, num);
  39. }
  40. }
  41. if (row + 1 <= 4) //Checking Bottom
  42. {
  43. if (ar[row + 1][col] == 1) {
  44. ar[row + 1][col] = -1;
  45. (*num)+=1;
  46. pos[*num].row = row; pos[*num].col = col;
  47. check(ar, row + 1, col, pos, num);
  48. }
  49. }
  50. if (col + 1 <= 4 && row + 1 <= 4) //Checking Bottom-Right
  51. {
  52. if (ar[row + 1][col + 1] == 1) {
  53. ar[row + 1][col + 1] = -1;
  54. (*num)+=1;
  55. pos[*num].row = row; pos[*num].col = col;
  56. check(ar, row + 1, col + 1, pos, num);
  57. }
  58. }
  59. if (col + 1 <= 4) //Checking Right
  60. {
  61. if (ar[row][col + 1] == 1) {
  62. ar[row][col + 1] = -1;
  63. (*num)+=1;
  64. pos[*num].row = row; pos[*num].col = col;
  65. check(ar, row, col + 1, pos, num);
  66. }
  67. }
  68. if (col + 1 <= 4 && row - 1 >= 0) //Checking Upper-Right
  69. {
  70. if (ar[row - 1][col + 1] == 1) {
  71. ar[row - 1][col + 1] = -1;
  72. (*num)+=1;
  73. pos[*num].row = row; pos[*num].col = col;
  74. check(ar, row - 1, col + 1, pos, num);
  75. }
  76. }
  77. if (row - 1 >= 0) //Checking Up
  78. {
  79. if (ar[row - 1][col] == 1) {
  80. ar[row - 1][col] = -1;
  81. (*num)+=1;
  82. pos[*num].row = row; pos[*num].col = col;
  83. check(ar, row - 1, col, pos, num);
  84. }
  85. }
  86. if (*num == 0)
  87. return;
  88. else
  89. {
  90. flag++;
  91. if (flag >= 2)
  92. (*num)-=1;
  93. check(ar, pos[*num].row, pos[*num].col, pos, num);
  94. }
  95.  
  96. }
  97.  
  98. void random5x5(int ar[][5])
  99. {
  100. for (int cnt = 0; cnt < 5; cnt++)
  101. {
  102. for (int c2 = 0; c2 < 5; c2++)
  103. {
  104. ar[cnt][c2] = rand() % 2;
  105. printf("%d ", ar[cnt][c2]);
  106. }
  107. printf("\n");
  108. }
  109. }
  110.  
  111.  
  112. int main()
  113. {
  114. int ar[5][5] = { 0,0,0,0,1,
  115. 1,0,0,0,1,
  116. 0,0,0,0,0,
  117. 0,0,0,0,0,
  118. 0,0,0,0,0 };
  119. int countblock = 0,num = 0;
  120. struct parent pos[30] = { 0 };
  121. srand(time(NULL));
  122. printf(" ---Find the connected blocks--- \n\n");
  123. printf("Creating a random array:\n");
  124. //random5x5(ar);
  125. for (int row = 0, col; row < 5; row++)
  126. {
  127. for (col = 0; col < 5; col++)
  128. {
  129. if (ar[row][col] == 1)
  130. {
  131. countblock = countblock + 1;
  132. ar[row][col] = -1;
  133. flag = 0;
  134. check(ar, row, col, pos, &num);
  135. }
  136. printf("%d ", ar[row][col]);
  137. }
  138. printf("\n");
  139. }
  140. printf("\n\n Found the connected blocks! I see only %d of them.\n\n", countblock);
  141. return 0;
  142. }
  143.  
  144.  
Success #stdin #stdout 0s 2156KB
stdin
Standard input is empty
stdout
      ---Find the connected blocks---     

Creating a random array:
0 0 0 0 -1 
-1 0 0 0 -1 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 


 Found the connected blocks! I see only 1 of them.