fork download
  1. #include <iostream>
  2. #define MAX_CELL 64
  3. #include <cmath>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. struct Cell {
  10. int m_Row;
  11. int m_Col;
  12. };
  13. int cMult;
  14. int rMult;
  15. float avgCost;
  16. float CostSoFar = 0.0F;
  17.  
  18. class CellTracker
  19. {
  20. //Establish String to hold all coords, separated by |~|
  21. std::string SeenCell = "|~|";
  22. public:
  23. bool seenCell(Cell tCell, bool storeCell)
  24. {
  25. std::string searchString(tCell.m_Row+","+tCell.m_Col);//store str as a string temporarily
  26. if (SeenCell.find("|~|"+searchString+"|~|")!=std::string::npos)
  27. //if the cell has been seen before, it will be stored, delimited by |~|
  28. //this searches for the full cell with delimiters
  29. //if it finds the cell, it returns true
  30. {
  31. return true;
  32. }
  33. else
  34. {
  35. //if the cell hasn't been seen before, it isn't stored,
  36. // we need to store it then return false;
  37. if(storeCell){SeenCell = SeenCell+searchString+"|~|";}//update string
  38. return false;//return false
  39. }
  40. }
  41. };
  42.  
  43.  
  44. CellTracker visited;//stores visited cells
  45. CellTracker badCells;//stores cells that I shouldn't visit again
  46.  
  47. // have a class object hold the following:
  48. // *Cells to avoid
  49. // *path taken
  50. // *return if path crosses itself (if new point is already on path) and send it to cells to avoid
  51. // *start cell
  52. // *end cell
  53. // *calculate average weight of cells
  54. // *current weight
  55. // *function to find most direct path, and add weights
  56. class custPather
  57. {
  58. Cell start;
  59. Cell end;
  60.  
  61.  
  62. };
  63.  
  64.  
  65. // cellCost represents a row-major 2d grid, each entry is cost of moving through that cell.
  66. int bestPath(Cell dest[], int maxDestLength,
  67. const Cell* start, const Cell* end,
  68. float cellCost[MAX_CELL][MAX_CELL])
  69. {
  70.  
  71. //float avgCost = avgWeight(cellCost);
  72.  
  73.  
  74.  
  75.  
  76. }
  77.  
  78. float dPathCheck(float currWeight, Cell* currCell, Cell* tarCell)//check cost of new move before making
  79. {
  80. return 0;
  81. }
  82.  
  83. bool moveVert(Cell currCell, Cell tarCell)
  84. {
  85. float cDiff = tarCell.m_Col-currCell.m_Col;
  86. float rDiff = tarCell.m_Row-currCell.m_Row;
  87. cMult = cDiff > 0? 1:-1;
  88. rMult = rDiff > 0? 1:-1;
  89. return((abs(cDiff)>abs(rDiff)));
  90. //return true if there is more vertical distance
  91. }
  92.  
  93.  
  94.  
  95. float returnCellWeight(Cell weightedCell, float cellCost[MAX_CELL][MAX_CELL])//TODO REMOVE CELL COST
  96. {
  97. return(cellCost[weightedCell.m_Row][weightedCell.m_Col]);
  98. }
  99.  
  100. float shortestRoute(Cell currCell, Cell tarCell, float cellCost[MAX_CELL][MAX_CELL])//TODO REMOVE CELL
  101. {
  102. Cell testCell = {currCell.m_Row,currCell.m_Col};
  103. float moves = 0;
  104. while((testCell.m_Col != tarCell.m_Col)&&(testCell.m_Row != tarCell.m_Row))
  105. {
  106. if(moveVert(testCell, tarCell))
  107. {
  108. testCell.m_Col+= cMult;
  109. }
  110. else
  111. {
  112. testCell.m_Row+= rMult;
  113. }
  114. moves+=cellCost[testCell.m_Row][testCell.m_Col];
  115. }
  116. return moves;
  117. }
  118.  
  119. Cell checkSurroundingCells(Cell curCell, Cell tarCell, float cellCost[MAX_CELL][MAX_CELL])
  120. {
  121. float smallestF;
  122. float currentF;
  123. Cell smallestC;
  124. if((curCell.m_Col > 0)&&!(badCells.seenCell(Cell{curCell.m_Row,curCell.m_Col-1},false)))
  125. {
  126. currentF = smallestF = returnCellWeight(Cell{curCell.m_Row,curCell.m_Col-1}, cellCost)+CostSoFar+shortestRoute(curCell, tarCell, cellCost);
  127. cout << smallestF << endl;
  128. smallestC = {curCell.m_Row,curCell.m_Col-1};
  129. }
  130. if((curCell.m_Col < MAX_CELL)&&!(badCells.seenCell(Cell{curCell.m_Row,curCell.m_Col+1},false)))
  131. {
  132. currentF = returnCellWeight(Cell{curCell.m_Row,curCell.m_Col+1}, cellCost)+CostSoFar+shortestRoute(curCell, tarCell, cellCost);
  133. cout << currentF << endl;
  134. if(currentF<smallestF)
  135. {
  136. smallestF=currentF;smallestC={curCell.m_Row,curCell.m_Col+1};
  137. }
  138. }
  139. if((curCell.m_Row > 0)&&!(badCells.seenCell(Cell{curCell.m_Row-1,curCell.m_Col},false)))
  140. {
  141. currentF = returnCellWeight(Cell{curCell.m_Row-1,curCell.m_Col}, cellCost)+CostSoFar+shortestRoute(curCell, tarCell, cellCost);
  142. cout << currentF << endl;
  143. if(currentF<smallestF)
  144. {
  145. smallestF=currentF;smallestC={curCell.m_Row-1,curCell.m_Col};
  146. }
  147. }
  148. if((curCell.m_Row < MAX_CELL)&&!(badCells.seenCell(Cell{curCell.m_Row+1,curCell.m_Col}, false)))
  149. {
  150. currentF = returnCellWeight(Cell{curCell.m_Row+1,curCell.m_Col}, cellCost)+CostSoFar+shortestRoute(curCell, tarCell, cellCost);
  151. cout << currentF << endl;
  152. if(currentF<smallestF)
  153. {
  154. smallestF=currentF;smallestC={curCell.m_Row+1,curCell.m_Col};
  155. }
  156. }
  157.  
  158. if (visited.seenCell(smallestC,true)){badCells.seenCell(curCell,true);}
  159. return smallestC;
  160.  
  161. }
  162.  
  163. float avgWeight(float cellCosts[MAX_CELL][MAX_CELL])//calculate average weight of cells
  164. {
  165. float totalCosts = 0.0F;
  166. for(int i = 0; i < MAX_CELL; i++)// using MAX_CELL since it's already been established
  167. {
  168. for(int j = 0; j < MAX_CELL; j++)//for every cell in array, add weight
  169. {
  170. totalCosts += cellCosts[i][j];
  171. }
  172. }
  173. return (totalCosts / (MAX_CELL*MAX_CELL));//using MAX_CELL since it's already been established
  174. // we return the average weight of all cells;
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181. int main()
  182. {
  183. //cout << "Hello World" << endl;
  184. Cell sCell = {10,10};
  185. Cell eCell = {32,10};
  186. Cell nCell;
  187. float L[MAX_CELL][MAX_CELL];
  188. for(int i = 0; i < MAX_CELL; i++)
  189. {
  190. for(int j = 0; j < MAX_CELL; j++)
  191. {
  192. L[i][j]=(float)(rand()%100);
  193. //cout << L[i][j] << endl;
  194. }
  195. }
  196. avgCost = avgWeight(L);
  197. cout << avgCost << endl;
  198. //cout << moveVert(sCell,eCell) << endl;
  199. //cout << rMult << endl;
  200. //cout << cMult << endl;
  201. //cout << shortestRoute(sCell,eCell) << endl;
  202.  
  203. nCell = checkSurroundingCells(sCell, eCell, L);
  204.  
  205. cout << "This move was to: ";
  206. cout << nCell.m_Row << " " << nCell.m_Col << endl;
  207. for (int i = 0; i < 5; i++)
  208. {
  209.  
  210. nCell = checkSurroundingCells(nCell, eCell, L);
  211. cout << "This move was to: ";
  212. cout << nCell.m_Row << " " << nCell.m_Col << endl;
  213. }
  214.  
  215. return 0;
  216. }
  217.  
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
49.8643
85
4
71
25
This move was to: 10 11
1298
1338
1291
1353
This move was to: 9 11
1337
1266
1352
1270
This move was to: 9 12
1422
1439
This move was to: 9 12
1422
1439
This move was to: 9 12
1422
1439
This move was to: 9 12