Advertisement
definho

optimalnije

May 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #ifndef DEBUG
  7. #define DEBUG(...) printf(__VA_ARGS__)
  8. #endif
  9.  
  10. #define MAXR 12
  11. #define MAXS 12
  12. #define PUNO 1000
  13.  
  14. void cisti_matricu(char mat[][MAXS], int r, int s){
  15. for (int i = 0; i < r+2; i++){
  16. for (int j = 0; j < s+2; j++){
  17. mat[i][j]='.';
  18. }
  19. }
  20. }
  21.  
  22. void prepisi_matricu_puno(char mat[][MAXS], char mat2[][MAXR][PUNO], int r, int s, int brojac_puno){
  23. for (int i = 0; i < r+2; ++i){
  24. for (int j = 0; j < s+2; ++j){
  25. mat2[i][j][brojac_puno]=mat[i][j];
  26. }
  27. }
  28. }
  29.  
  30. /*void prepisi_matricu_zadnja(char mat[][MAXS], char mat2[][MAXR][PUNO], int r, int s, int brojac_puno){
  31. for (int i = 1; i < r+1; ++i){
  32. for (int j = 1; j < s+1; ++j){
  33. mat[i][j]=mat2[i][j][brojac_puno];
  34. }
  35. }
  36. }*/
  37.  
  38. void prepisi_matricu(char mat[][MAXS], char mat2[][MAXS], int r, int s){
  39. for (int i = 1; i < r+1; ++i){
  40. for (int j = 1; j < s+1; ++j){
  41. mat[i][j]=mat2[i][j];
  42. }
  43. }
  44. }
  45.  
  46. int provjera_neparni(char mat[][MAXS], int r, int s){
  47. int brojac=0;
  48.  
  49. if (mat[r-1][s-1]=='#') brojac++;
  50. if (mat[r-1][s]=='#') brojac++;
  51. if (mat[r][s-1]=='#') brojac++;
  52. if (mat[r][s+1]=='#') brojac++;
  53. if (mat[r+1][s-1]=='#') brojac++;
  54. if (mat[r+1][s]=='#') brojac++;
  55.  
  56. return brojac;
  57. }
  58.  
  59. int provjera_parni(char mat[][MAXS], int r, int s){
  60. int brojac=0;
  61.  
  62. if (mat[r-1][s]=='#') brojac++;
  63. if (mat[r-1][s+1]=='#') brojac++;
  64. if (mat[r][s-1]=='#') brojac++;
  65. if (mat[r][s+1]=='#') brojac++;
  66. if (mat[r+1][s]=='#') brojac++;
  67. if (mat[r+1][s+1]=='#') brojac++;
  68.  
  69. return brojac;
  70. }
  71.  
  72. long long int provjeri_duple(char mat[][MAXS], char mat2[][MAXR][PUNO], int r, int s, long long int brojac_puno){
  73. int triger=0;
  74.  
  75. for (long long int k = 0; k < brojac_puno+1; ++k){
  76. for (int i = 1; i < r+1; ++i){
  77. for (int j = 1; j < s+1; ++j){
  78. if (mat[i][j]!=mat2[i][j][k]) triger++; //ako su poz_dupli, onda ce ostat 0
  79. }
  80. }
  81.  
  82. if (!triger) return k; //poz_dupli su, vrati k!
  83.  
  84. triger=0;
  85.  
  86. }
  87.  
  88. return 0;
  89. }
  90.  
  91. void unos(char mat[][MAXS], int r, int s){
  92. for (int i = 1; i < r+1; ++i){
  93. for (int j = 1; j < s+1; ++j){
  94. scanf (" %c", &mat[i][j]);
  95. }
  96. }
  97. }
  98.  
  99. void ispis(char mat[][MAXS], int r, int s){
  100. for (int i = 1; i < r+1; i++){
  101. for (int j = 1; j < s+1; j++){
  102. printf("%c", mat[i][j]);
  103. }
  104. printf ("\n");
  105. }
  106. }
  107.  
  108. void ispis_tmp(char mat[][MAXS][PUNO], int r, int s, int ispisni_broj){
  109. for (int i=1; i<r+1; i++){
  110. for (int j=1; j<s+1; j++){
  111. printf ("%c", mat[i][j][ispisni_broj]);
  112. }
  113. printf ("\n");
  114. }
  115.  
  116. }
  117.  
  118.  
  119.  
  120. int main() {
  121. int s, r, prekini=0, ispis_tempa=0; //stupac, redak, brojac za izlaz
  122. long long int dan, dobiveni_broj, najnovji, poz_dupli=0, ispisni_broj; //dani 2 ≤ s,r ≤ 10, 1≤ dani ≤2^63−1
  123. char pocetno[MAXR][MAXS]; //pocetna matrica
  124. char tmp[MAXR][MAXS]; //privremena matrica, za upis nakon provjere
  125. char tmp_puno[MAXR][MAXS][PUNO]; //sve prijasnje matrice spremaju se u ovu za usporedbu
  126.  
  127.  
  128. scanf ("%d %d %lli", &r, &s, &dan);
  129.  
  130. cisti_matricu(tmp, r, s);
  131. cisti_matricu(pocetno, r, s);
  132. unos(pocetno, r, s);
  133.  
  134. //glavna for petlja
  135. for (long long int k=0; k<dan; k++){
  136. for (int i = 1; i < r+1; ++i){
  137. for (int j = 1; j < s+1; ++j){
  138. if (i%2!=0){ //za neparne redove
  139. if (provjera_neparni(pocetno, i, j)%2){
  140. tmp[i][j]='#';
  141. prekini++;
  142. }
  143. }
  144.  
  145. if(i%2==0){ //za parne redove
  146. if (provjera_parni(pocetno, i, j)%2){
  147. tmp[i][j]='#';
  148. prekini++;
  149. }
  150. }
  151. }
  152. }
  153.  
  154. prepisi_matricu_puno(pocetno, tmp_puno, r, s, k);
  155. prepisi_matricu(pocetno, tmp, r, s); //prepisi iz tmp sve u pocetnu
  156.  
  157. poz_dupli=provjeri_duple(pocetno, tmp_puno, r, s, k);
  158.  
  159. if (poz_dupli){
  160. dobiveni_broj=k-poz_dupli; //k iz for petlje - broj koji dobivamo iz provjere duplih
  161. najnovji=dan%dobiveni_broj;
  162.  
  163. if (najnovji){
  164. ispisni_broj=k;
  165. }
  166.  
  167. else ispisni_broj=dobiveni_broj;
  168.  
  169. ispis_tempa++;
  170. prekini=0;
  171. }
  172.  
  173.  
  174. cisti_matricu(tmp, r, s); //ocisti tmp za sljedecu uporabu
  175. if (!prekini) break; //ako su u nekom slucaju sve sace prazne, odmah izadi iz petlje ili ako postoje identicne
  176. }
  177.  
  178. if (!ispis_tempa) ispis(pocetno, r, s);
  179. else ispis_tmp(tmp_puno, r, s, ispisni_broj);
  180.  
  181. return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement