Guest User

Untitled

a guest
Dec 15th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <chrono>
  5. #include <vector>
  6. #include <iostream>
  7. #include <sys/time.h>
  8. #define RAND_MAX 5
  9.  
  10. /*
  11. 1
  12. 2 3
  13. 4
  14. */
  15.  
  16. int grid[5][5] = {
  17. {0, 0, 0, 0, 2},
  18. {0, 0, 0, 0, 2},
  19. {0, 0, 0, 0, 2},
  20. {0, 0, 0, 0, 2},
  21. {0, 0, 0, 0, 2}
  22. };
  23.  
  24. int InitPos[2] = {2, 2};
  25.  
  26. int MaxExp = 5000000;
  27.  
  28. bool Success = false;
  29. int StepCount = 0;
  30. int ExpNumber = 1;
  31. int AntsBag = 0;
  32. void Init();
  33. void CarryFood(int * pos);
  34. void LeftFood(int * pos);
  35. bool checkMovability(int * pos, int direction);
  36. bool moveToDirection(int pos[2], int direction);
  37. bool checkSuccess();
  38. void ShowResult(std::vector <int> &l_path);
  39.  
  40.  
  41. FILE * file = fopen("FinalDataUnsig.txt", "a+");
  42.  
  43. int main(int argc, char const *argv[])
  44. {
  45. time_t t;
  46. srand((unsigned)time(&t));
  47. fprintf(file, "%%Total Experiment Number : %d. Exp Number-Step Count-Pathn", MaxExp);
  48. while(ExpNumber <= MaxExp)
  49. {
  50. Init();
  51. std::vector <int> path;
  52. int pos[2];
  53. pos[0] = InitPos[0];
  54. pos[1] = InitPos[1];
  55. do{
  56. int direction = (rand() % 4) + 1;
  57. if (moveToDirection(pos, direction))
  58. {
  59. StepCount++;
  60. path.push_back(direction);
  61. }
  62. if (pos[1] == 4&&grid[pos[0]][4]==2&&AntsBag==0)
  63. {
  64. CarryFood(pos);
  65. }
  66. if (pos[1] == 0&&grid[pos[0]][0]==0&&AntsBag==2)
  67. {
  68. LeftFood(pos);
  69. }
  70. checkSuccess();
  71. }
  72. while(!Success);
  73.  
  74. ShowResult(path);
  75. ExpNumber++;
  76. }
  77. fclose(file);
  78. return 0;
  79. }
  80.  
  81. void Init()
  82. {
  83. Success = false;
  84. StepCount = 0;
  85. AntsBag = 0;
  86. int gridInit[5][5] = {
  87. {0, 0, 0, 0, 2},
  88. {0, 0, 0, 0, 2},
  89. {0, 0, 0, 0, 2},
  90. {0, 0, 0, 0, 2},
  91. {0, 0, 0, 0, 2}
  92. };
  93. for (int i = 0; i < 5; ++i)
  94. {
  95. for (int j = 0; j < 5; ++j)
  96. {
  97. grid[i][j] = gridInit[i][j];
  98. }
  99. }
  100. }
  101.  
  102. void ShowResult(std::vector <int> &l_path)
  103. {
  104. fprintf(file, "%d %d ", ExpNumber, StepCount);
  105. if (file != NULL)
  106. {
  107. for (std::vector<int>::iterator it = l_path.begin(); it != l_path.end(); it++)
  108. {
  109. fprintf(file, "%d", *it);
  110. }
  111. }
  112. fprintf(file, "n");
  113. }
  114.  
  115. void CarryFood(int * pos)
  116. {
  117. if (pos[1] == 4&&grid[pos[0]][4]==2&&AntsBag==0)
  118. {
  119. AntsBag = 2;
  120. grid[pos[0]][4] = 0;
  121. }
  122. }
  123.  
  124. void LeftFood(int * pos)
  125. {
  126. if (pos[1] == 0&&grid[pos[0]][0]==0&&AntsBag==2)
  127. {
  128. AntsBag = 0;
  129. grid[pos[0]][0] = 2;
  130. }
  131. }
  132.  
  133. bool checkMovability(int * pos, int direction)
  134. {
  135. switch(direction)
  136. {
  137. case 1:
  138. {
  139. if(pos[1]==0){
  140. return false;
  141. }
  142. break;
  143. }
  144. case 2:
  145. {
  146. if (pos[0]==0)
  147. {
  148. return false;
  149. }
  150. break;
  151. }
  152. case 3:
  153. {
  154. if (pos[0]==4)
  155. {
  156. return false;
  157. }
  158. break;
  159. }
  160. case 4:
  161. {
  162. if (pos[1]==4)
  163. {
  164. return false;
  165. }
  166. break;
  167. }
  168. default:
  169. {
  170. printf("Wrong direction input is given!!n");
  171. return false;
  172. break;
  173. }
  174. }
  175. return true;
  176.  
  177. }
  178.  
  179. bool moveToDirection(int * pos, int direction)
  180. {
  181. if ( !checkMovability(pos, direction) )
  182. {
  183. return false;
  184. }
  185.  
  186. switch(direction){
  187. case 1:
  188. {
  189. pos[1] -= 1;
  190. break;
  191. }
  192. case 2:
  193. {
  194. pos[0] -= 1;
  195. break;
  196. }
  197. case 3:
  198. {
  199. pos[0] += 1;
  200. break;
  201. }
  202. case 4:
  203. {
  204. pos[1] += 1;
  205. break;
  206. }
  207. default:
  208. {
  209. printf("I'm stunned!n");
  210. return false;
  211. break;
  212. }
  213. }
  214. return true;
  215. }
  216.  
  217. bool checkSuccess()
  218. {
  219. for (int i = 0; i < 5; ++i)
  220. {
  221. if (grid[i][0] != 2)
  222. {
  223. return false;
  224. }
  225. }
  226. Success = true;
  227. return true;
  228. }
Add Comment
Please, Sign In to add comment