Advertisement
alex_doan_

Untitled

Dec 10th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. struct Point {
  11. int r, c;
  12. char sym;
  13. int id;
  14. };
  15.  
  16. struct Place {
  17. int id;
  18. string n;
  19. };
  20.  
  21. double dist(double cc, double cr, double pc, double pr){
  22. double x, y, d;
  23. x = pow((cc-pc),2);
  24. y = pow((cr-pr),2);
  25. d = pow(x+y, 0.5);
  26. return d;
  27. }
  28.  
  29. int findAP(vector <int> v, double n){
  30. int s = 1;
  31. for(int i = 0; i<v.size(); i++){
  32. if (v.at(i) == n){
  33. s = 0;
  34. }
  35. }
  36. return s;
  37. }
  38.  
  39. int main(){
  40. https://declaring variables
  41.  
  42. string locfile;
  43. string namfile;
  44. double gr, gc, sr, sc, er, ec;
  45. Point p;
  46. vector <Point> data (1);
  47. double n = 0;
  48. int i, j;
  49. Place pl;
  50. int y,z;
  51. vector <Place> names (0);
  52. double cx, cy;
  53. double mind = -1, potd;
  54. double nx, ny;
  55. Point npoint, cpoint;
  56. int index;
  57. int cid, idi;
  58. vector <int> useddata (0);
  59.  
  60.  
  61. https://getting file names
  62. cout<<"Enter Locations Filename: "<<endl;
  63. getline(cin,locfile);
  64.  
  65. cout<<"Enter Names Filename: "<<endl;
  66. getline(cin,namfile);
  67.  
  68. https://opening files and checking for failure
  69. ifstream inloc;
  70. inloc.open(locfile.c_str());
  71. ifstream innam;
  72. innam.open(namfile.c_str());
  73. if (inloc.fail() or innam.fail()){
  74. cout<<"Input file could not be opened"<<endl;
  75. exit(EXIT_FAILURE);
  76. }
  77.  
  78. https://reading location file
  79. inloc>>gr>>gc>>sr>>sc>>er>>ec;
  80.  
  81. https://creating grid
  82. vector <char> row (gc,'.');
  83. vector < vector <char> > grid (gr, row);
  84.  
  85. https://placing starting and ending points
  86. grid.at(sr-1).at(sc-1) = 'S';
  87. grid.at(er-1).at(ec-1) = 'E';
  88.  
  89. https://reading in safe point information
  90. inloc>>p.r>>p.c>>p.sym>>p.id;
  91. if (p.r > gr or p.c > gc or p.r<=0 or p.c<=0){
  92. cout<<p.id<<" out of bounds - ignoring"<<endl;
  93. n = 1;
  94. }
  95. else {
  96. data.at(0) = p;
  97. grid.at(p.r-1).at(p.c-1) = p.sym;
  98. }
  99. inloc>>p.r;
  100. while (! inloc.fail()){
  101. inloc>>p.c>>p.sym>>p.id;
  102. if (p.r > gr or p.c > gc or p.r<=0 or p.c<=0){
  103. cout<<p.id<<" out of bounds - ignoring"<<endl;
  104. }
  105. else {
  106. if (n){
  107. data.at(0) = p;
  108. n=0;
  109. }
  110. else {
  111. data.push_back(p);
  112. grid.at(p.r-1).at(p.c-1) = p.sym;
  113. }
  114. }
  115. inloc>>p.r;
  116. }
  117.  
  118. https://starting output
  119. ofstream omap("journey.txt");
  120.  
  121. https://printing map
  122. for (i=0;i<grid.size();i++){
  123. for(j=0;j<grid.at(i).size();j++){
  124. omap<<grid.at(i).at(j);
  125. }
  126. omap<<endl;
  127. }
  128.  
  129. https://reading in place names and correcting names
  130. innam>>pl.id;
  131. while (! innam.fail()){
  132. innam>>pl.n;
  133. z = pl.n.find("XX");
  134. while (z != pl.n.npos){
  135. pl.n.erase(z, 2);
  136. z = pl.n.find("XX");
  137. }
  138. y = pl.n.find("_");
  139. while (y != pl.n.npos){
  140. pl.n.erase(y,1);
  141. pl.n.insert(y," ");
  142. y = pl.n.find("_");
  143. }
  144. names.push_back(pl);
  145. innam>>pl.id;
  146. }
  147.  
  148. https://continuing output
  149. omap<<"Start at "<<sr<<" "<<sc<<endl;
  150.  
  151.  
  152. https://processing data and values
  153. cx = sc;
  154. cy = sr;
  155.  
  156. https://main loop to pick next point
  157. while (useddata.size()<data.size()){
  158. for(i = 0; i<data.size(); i++){
  159. potd = dist(cx, cy, data.at(i).c, data.at(i).r);
  160. if (mind == -1 and findAP(useddata,data.at(i).id)){
  161. nx = data.at(i).c;
  162. ny = data.at(i).r;
  163. npoint = data.at(i);
  164. index = i;
  165. idi = data.at(i).id;
  166. mind = potd;
  167. }
  168. else if (potd < mind and findAP(useddata,data.at(i).id)){
  169. nx = data.at(i).c;
  170. ny = data.at(i).r;
  171. npoint = data.at(i);
  172. index = i;
  173. idi = data.at(i).id;
  174. mind = potd;
  175.  
  176. }
  177. else if (potd == mind and findAP(useddata,data.at(i).id)){
  178. if (data.at(i).id < npoint.id){
  179. nx = data.at(i).c;
  180. ny = data.at(i).r;
  181. npoint = data.at(i);
  182. index = i;
  183. idi = data.at(i).id;
  184. mind = potd;
  185. }
  186.  
  187. }
  188. }
  189. https://outputs next point to report
  190. cx = nx;
  191. cy = ny;
  192. cpoint = npoint;
  193. useddata.push_back(idi);
  194. for (i = 0; i<names.size(); i++){
  195. if (idi == names.at(i).id){
  196. cid = i;
  197. }
  198. }
  199. omap<<"Go to "<<names.at(cid).n<<" at "<<cy<<" "<<cx<<endl;
  200. mind = -1;
  201. }
  202.  
  203. https://final output
  204. omap<<"End at "<<er<<" "<<ec<<endl;
  205.  
  206. https://closing input and output streams
  207. omap.close();
  208. innam.close();
  209. inloc.close();
  210.  
  211. return 0;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement