Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <netpbm/pgm.h>
  3. #include <time.h>
  4. #include <math.h>
  5.  
  6. #define N 100
  7. #define T 20
  8.  
  9. void moveSource(int* x, int* y, float array[N][N], float value){
  10. int xDirection, yDirection, xSource, ySource;
  11.  
  12. xSource = *x;
  13. ySource = *y;
  14. xDirection = rand()%2 * 2 - 1;
  15. yDirection = rand()%2 * 2 - 1;
  16. xSource += xDirection;
  17. ySource += yDirection;
  18. xSource = (xSource==0)? 1: xSource;
  19. xSource = (xSource==N-1)? N-2 : xSource;
  20. ySource = (ySource==0)? 1 : ySource;
  21. ySource = (ySource==N-1)? N-2 : ySource;
  22. array[xSource][ySource] = value;
  23. *x = xSource;
  24. *y = ySource;
  25. }
  26.  
  27. void algo(float array[N][N], int xSource, int ySource, int localSourceX[5], int localSourceY[5]){
  28. int i;
  29. int j;
  30. int k;
  31. int check;
  32. for(i = 1 ; i < N-1; i++){
  33. for(j = 1 ; j < N-1; j++){
  34. if(i == xSource && j == ySource) continue;
  35. check = 0;
  36. for(k = 0; k < 5; k++){
  37. if(i == localSourceX[k] && j == localSourceY[k]) check = 1;
  38. }
  39. if(check) continue;
  40. array[i][j] = (array[i+1][j] + array[i-1][j] + array[i][j+1] + array[i][j-1])/4.0;
  41.  
  42. }
  43. }
  44. }
  45.  
  46. void climb(int *a, int *b, float array[N][N]){
  47. int i, j;
  48. int x;
  49. x = *a;
  50. int y;
  51. y = *b;
  52. int newX, newY;
  53. float max;
  54. max = array[x][y];
  55. newX = *a;
  56. newY = *b;
  57. for(i = x - 1; i <= x + 1; i++){
  58. for(j = y - 1; j <= y + 1; j++){
  59. if(i==x && j== y) continue;
  60. if(i < 0 || i >= N || j < 0 || j >= N) continue;
  61. if(max < array[i][j]){
  62. max = array[i][j];
  63. newX = i;
  64. newY = j;
  65. }
  66. }
  67. }
  68. //printf("max = %lf\n", max);
  69. //printf("Check max: %lf\n", array[newX][newY]);
  70. *a = newX;
  71. *b = newY;
  72. }
  73.  
  74. void climbHipek(int *x, int *y, float array[N][N]){
  75. int tmpX;
  76. tmpX = *x;
  77. int tmpY;
  78. tmpY = *y;
  79. climb(&tmpX, &tmpY, array);
  80. if(tmpX == *x && tmpY == *y){
  81. *x = rand()%N;
  82. *y = rand()%N;
  83. }else{
  84. *x = tmpX;
  85. *y = tmpY;
  86. }
  87. }
  88.  
  89. int main()
  90. {
  91. gray** image;
  92. int i, j;
  93. float array [N][N] = { 0 };
  94. srand(time(NULL));
  95.  
  96. int xSource = rand()%(N-1)+1;
  97. int ySource = rand()%(N-1)+1;
  98. array[xSource][ySource] = 100;
  99.  
  100. int localSourceX[5] = {0};
  101. int localSourceY[5] = {0};
  102. for(i = 0; i < 5; i++){
  103. localSourceX[i] = rand()%(N-1)+1;
  104. localSourceY[i] = rand()%(N-1)+1;
  105. array[localSourceX[i]][localSourceY[i]] = 75.0;
  106. }
  107.  
  108.  
  109. int iteration = 0;
  110. int counter = 0;
  111. char filename[100] = {0};
  112. int fileNumber = 0;
  113. FILE* file;
  114.  
  115. int hipekWin = 0;
  116. int xHipek = rand()%N;
  117. int yHipek = rand()%N;
  118. int xDirection;
  119. int yDirection;
  120. int xDestination = 1;
  121. int yDestination = 1;
  122. int xHipDestination = 1;
  123. int yHipDestination = 1;
  124. float maxHip = 0;
  125. int temp = 0;
  126. float vec[T] = {0};
  127. int test = 0;
  128.  
  129. while(!hipekWin && ++iteration < 250)
  130. {
  131. maxHip = 0;
  132. xHipDestination = xHipek;
  133. yHipDestination = yHipek;
  134.  
  135. for(int z = 0; z < T; z++)
  136. {
  137. vec[z] = array[xSource - z][ySource];
  138. }
  139.  
  140. /*for(int z = 1; z < 21; z++)
  141. {
  142. printf("%lf ", vec[z]);
  143. }
  144.  
  145. printf("\n");*/
  146.  
  147.  
  148.  
  149.  
  150.  
  151. for(i = xHipek-1; i<=xHipek+1;i++)
  152. {
  153. for(j=yHipek-1; j<=yHipek+1; j++)
  154. {
  155. maxHip = array[i][j];
  156. for(int k = 0; k < T; k++)
  157. {
  158. if(vec[k]=maxHip)
  159. {
  160. test = 1;
  161. }
  162.  
  163. }
  164.  
  165. if(i==xHipek && j==yHipek) continue;
  166. if(test >= 0)
  167. {
  168. maxHip = array[i][j];
  169. xHipDestination = i;
  170. yHipDestination = j;
  171. //xHipek = xHipDestination;
  172. //yHipek = yHipDestination;
  173.  
  174. }
  175.  
  176. }
  177. }
  178.  
  179. if(test < 1)
  180. {
  181. if(xHipek >= N-2) xDestination = -1;
  182. if(yHipek >= N-2) yDestination = -1;
  183. if(xHipek <= 1) xDestination = 1;
  184. if(yHipek <= 1) yDestination = 1;
  185. xHipek += xDestination;
  186. yHipek += yDestination;
  187.  
  188. }else
  189. {
  190. xHipek = xHipDestination;
  191. yHipek = yHipDestination;
  192. }
  193.  
  194. //climbHipek(&xHipek, &yHipek, array);
  195.  
  196. if(xHipek == xSource && yHipek == ySource) hipekWin = 1;
  197.  
  198. if((counter+1)%10 == 0)
  199. {
  200. moveSource(&xSource, &ySource, array, 100.0);
  201. for(i = 0 ; i < 5; i++){
  202. moveSource(&(localSourceX[i]), &(localSourceY[i]), array, 75.0);
  203. }
  204.  
  205. }
  206.  
  207. image = pgm_allocarray(N, N);
  208. for(i=0;i<N;i++)
  209. {
  210. for(j=0;j<N;j++)
  211. {
  212. image[i][j] = array[i][j]/100.0*255.0;
  213. if(i==xHipek && j==yHipek)
  214. image[i][j] = 255.0;
  215. }
  216. }
  217. fileNumber++;
  218. sprintf(filename, "image%d.pgm", fileNumber);
  219. file = fopen(filename, "wb");
  220. pgm_writepgm(file, image, N, N, 255, 1);
  221. fclose(file);
  222.  
  223. algo(array, xSource, ySource, localSourceX, localSourceY);
  224. counter++;
  225. }
  226. printf("Hipek win after %d iteration!\n", counter);
  227. return 0;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement