Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #define MAX 60000l
  6. #define MLD 1000000000.0
  7. /////////////////////////////////////////////
  8. // PROCEDURY POMOCNICZE //
  9. /////////////////////////////////////////////
  10. void utworz_MACIERZ(int n, int ***M){
  11. // alokuję pamięć na tablicę rozmiaru nxn
  12. // i wpisuje losowe wartości 0/1 w macierzy
  13. int i,j;
  14. (*M) = (int **)malloc(n*sizeof(int *));
  15. for(i=0;i<n;i++){
  16. (*M)[i]=(int *)malloc(n*sizeof(int));
  17. }
  18. for(i=0;i<n;i++){
  19. for(j=0;j<n;j++){
  20. (*M)[i][j]=rand()% 2;
  21. }
  22. }
  23. }
  24. /////////////////////////////////////////////
  25. void utworz_MACIERZ_x(int n, int ***M, int x){
  26. // alokuję pamięć na tablicę rozmiaru nxn
  27. // i wpisuje do macierzy wszędzie wartości x
  28. int i,j;
  29. (*M) = (int **)malloc(n*sizeof(int *));
  30. for(i=0;i<n;i++){
  31. (*M)[i]=(int *)malloc(n*sizeof(int));
  32. }
  33. for(i=0;i<n;i++){
  34. for(j=0;j<n;j++){
  35. (*M)[i][j]=x;
  36. }
  37. }
  38. }
  39. /////////////////////////////////////////////
  40. void wypisz_MACIERZ(int n, int **M){
  41. // wypisuje wartości macierzy
  42. int i,j;
  43.  
  44. for(i=0;i<n;i++){
  45. for(j=0;j<n;j++)
  46. printf("%d",M[i][j]);
  47. printf("\n");
  48. }
  49. }
  50. /////////////////////////////////////////////
  51. void zwolnij_MACIERZ(int n, int **M){
  52. // zwalania pamięć zarezerwowaną dla macierzy
  53. int i;
  54. for(i=0;i<n;i++)
  55. {
  56. free(M[i]);
  57. }
  58. free(M);
  59. }
  60. /////////////////////////////////////////////
  61. // ALGORYTM PIERWSZY //
  62. /////////////////////////////////////////////
  63. int ALGO_NAIWNY(int n, int **M){
  64. int x1,y1,x2,y2,x,y;
  65. int max=0;
  66. int local_max=0;
  67.  
  68. for(x1=0;x1<n;x1++)
  69. for(y1=0;y1<n;y1++)
  70. for(x2=n-1;x2>x1-1;x2--)
  71. for(y2=n-1;y2>y1-1;y2--){
  72. local_max=0;
  73. for(x=x1;x<x2+1;x++)
  74. for(y=y1;y<y2+1;y++)
  75. local_max+=M[x][y];
  76. if ((local_max==(x2-x1+1)*(y2-y1+1)) && (local_max>max)) max=local_max;
  77. }
  78. return max;
  79. }
  80. /////////////////////////////////////////////
  81. // ALGORYTM DRUGI //
  82. /////////////////////////////////////////////
  83. int REKURENCJA(int **M,int x1, int y1, int x2, int y2){
  84. if ((x2==x1)&&(y2==y1)) return M[x1][y1];
  85. else if ((x2-x1)>(y2-y1))
  86. return REKURENCJA(M,x1,y1,(int)(x1+x2)/2,y2)*REKURENCJA(M,(int)(x1+x2+1)/2,y1,x2,y2);
  87. else return REKURENCJA(M,x1,y1,x2,(int)(y1+y2)/2)*REKURENCJA(M,x1,(int)(y1+y2+1)/2,x2,y2);
  88. }
  89. /////////////////////////////////////////////
  90. int ALGO_REKURENCYJNY(int n, int **M){
  91. int x1,y1,x2,y2;
  92. int max=0;
  93. int local_max;
  94.  
  95. for(x1=0;x1<n;x1++)
  96. for(y1=0;y1<n;y1++)
  97. for(x2=x1;x2<n;x2++)
  98. for(y2=y1;y2<n;y2++){
  99. local_max=REKURENCJA(M,x1,y1,x2,y2)*(x2-x1+1)*(y2-y1+1);
  100. if (local_max>max) max=local_max;
  101. }
  102. return max;
  103. }
  104. /////////////////////////////////////////////
  105. // ALGORYTM TRZECI //
  106. /////////////////////////////////////////////
  107. int ALGO_DYNAMICZNY(int n, int **M){
  108. int x1,x2,y;
  109. int max=0;
  110. int iloczyn;
  111. int **MM;
  112.  
  113. utworz_MACIERZ_x(n,&MM,0);
  114.  
  115. for(y=0;y<n;y++)
  116. for(x1=0;x1<n;x1++){
  117. iloczyn=1;
  118. for(x2=x1;x2<n;x2++){
  119. iloczyn*=M[x2][y];
  120. MM[x1][x2]=iloczyn*(x2-x1+1+MM[x1][x2]);
  121. if (MM[x1][x2]>max) max=MM[x1][x2];
  122. }
  123. }
  124. return max;
  125. }
  126. /////////////////////////////////////////////
  127. // ALGORYTM CZWARTY //
  128. /////////////////////////////////////////////
  129. int ALGO_CZULY(int n, int **M){
  130. int x1,y1,x2,y2,ymax;
  131. int max=0;
  132. int local_max=0;
  133.  
  134. for(x1=0;x1<n;x1++)
  135. for(y1=0;y1<n;y1++){
  136. local_max=0;
  137. x2=x1;
  138. ymax=n-1;
  139. while ((x2<n)&&(M[x2][y1]==1)){
  140. y2=y1;
  141. while((y2<ymax+1)&&(M[x2][y2]==1)){
  142. y2++;
  143. }
  144. ymax=y2-1;
  145. local_max=(x2-x1+1)*(ymax-y1+1);
  146. if (local_max>max) max=local_max;
  147. x2++;
  148. }
  149. }
  150. return max;
  151. }
  152. /////////////////////////////////////////////
  153. /////////////////////////////////////////////
  154. /////////////////////////////////////////////
  155. int main(){
  156.  
  157.  
  158. int **Macierz;
  159. srand(time(NULL));
  160.  
  161.  
  162.  
  163. struct timespec tp0, tp1;
  164. double Tn,Fn;
  165. long int n; // liczba testów
  166. int x;
  167.  
  168. for(n=15;n<300;n=n+3){
  169. utworz_MACIERZ(n,&Macierz);
  170.  
  171. clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp0);
  172.  
  173. x=ALGO_NAIWNY( n, Macierz);
  174.  
  175. // przykładowe obliczenia
  176. //x=ALGO_NAIWNY(n, Macierz);
  177.  
  178. clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp1);
  179.  
  180.  
  181.  
  182. //////////////////////////////////////////////////////
  183. // algorytm naiwny
  184. //Fn=n*n*n*n*n*log(n)*sqrt(n); git dla losowych
  185. //Fn=n*n*n*n*n;
  186. //Fn=n*n; git dla zer
  187. //Fn=n*n*n*n; git dla 1
  188. /////////////////////////////////////////////////////
  189.  
  190.  
  191.  
  192. /////////////////////////////////////////////////////
  193. // drugi rekurencyjny
  194. //Fn=n*n*n*n*n*log2(n)*log2(n)*log2(n); git dla losowych
  195. //Fn=n*n*n*n; git dla jedynek
  196. //Fn=n*n; git dla 0
  197. //////////////////////////////////////////////////////
  198.  
  199. //////////////////////////////////////////////////////
  200. // trzeci dynamiczny
  201. //Fn=n*n*n; git dla losowych
  202. //Fn=n*n; git dla 0
  203. //Fn=n*n*n*n; git dla 1
  204. //////////////////////////////////////////////////////
  205.  
  206. //////////////////////////////////////////////////////
  207. // czwarty czuły
  208. //Fn=n*n; git dla zer
  209. // przy 0 rosnie wspolczynnik, a czas sie skraca
  210. //Fn=n*n; <- git dla losowych
  211. //Fn=n*n*n*n; dla 1, pesymistyczny przypadek
  212. //////////////////////////////////////////////////////
  213.  
  214.  
  215. Tn=(tp1.tv_sec+tp1.tv_nsec/MLD)-(tp0.tv_sec+tp0.tv_nsec/MLD);
  216. printf("n: %5ld \tczas: %3.10lf \twspolczynnik: %3.5lf\n",n,Tn, Fn/Tn);
  217. //utworz_MACIERZ_x(n,&Macierz,0);
  218. //utworz_MACIERZ_x(n,&Macierz,1);
  219. zwolnij_MACIERZ(n,Macierz);
  220. }
  221. return 1;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement