Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.00 KB | None | 0 0
  1. #include <getopt.h>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <deque>
  8. #include <map>
  9. #include <string.h>
  10. #include <iterator> // for std::begin, std::end
  11.  
  12. using namespace std;
  13.  
  14. //To Do, for some reason adding duplicate right at the end. Talk about how it works but for some reason duplicates coming and dont
  15. //know how to handle removing them in terms of time efficiency.
  16.  
  17. pair<string, string> getOptions(int argc, char * argv[]) {
  18. opterr = true; // Give us help with errors
  19. int choice;
  20. int option_index = 0;
  21.  
  22. option long_options[] = {
  23. { "help", no_argument, nullptr, 'h'},
  24. { "queue", no_argument, nullptr, 'q'},
  25. { "stack", no_argument, nullptr, 's'},
  26. { "output", required_argument, nullptr, 'o'},
  27. { nullptr, 0, nullptr, '\0' }
  28. };
  29. pair<string, string> args_val;
  30. while ((choice = getopt_long(argc, argv, "hqso:", long_options, &option_index)) != -1) {
  31. switch (choice) {
  32. case 'h':
  33. cout << "Help Printout \n";
  34. exit(1);
  35. break;
  36.  
  37. case 'q':
  38. //Error Handling
  39. if (args_val.first != "") {
  40. cerr << "Error";
  41. exit(1);
  42. }
  43. args_val.first = "queue";
  44. break;
  45.  
  46. case 's':
  47. //Error Handling
  48. if (args_val.second != "") {
  49. cerr << "Error";
  50. exit(1);
  51. }
  52. args_val.first = "stack";
  53. break;
  54.  
  55. case 'o':
  56. if (strcmp(optarg, "list") == 0)
  57. args_val.second = "list";
  58. if (strcmp(optarg, "map") == 0)
  59. args_val.second = "map";
  60. break;
  61.  
  62. default:
  63. cerr << "Error";
  64. exit(1);
  65. } // switch
  66. } // while
  67. //Error Handling
  68. if (args_val.second == "")
  69. args_val.second = "list";
  70. return args_val;
  71. } // getOptions()
  72.  
  73. struct objState {
  74. char color;
  75. int row;
  76. int col;
  77. objState()
  78. :color('^'), row(-1), col(-1) {}
  79. objState(char color_in, int row_in, int col_in)
  80. :color(color_in), row(row_in), col(col_in) {}
  81. };
  82.  
  83. struct Tiles {
  84. char value;
  85. bool visited;
  86. //bool planned_visit = false; // Idea!
  87. Tiles()
  88. : value('0'), visited(false) {}
  89. Tiles(char val, bool visit)
  90. : value(val), visited(visit) {}
  91. };
  92.  
  93. map<char, int> mapOfButtons = {
  94. {'^',0},{'A',1}, {'B',2},{'C',3},{'D',4},{'E',5},{'F',6},{'G',7},{'H',8},{'I',9},{'J',10},{'K',11},{'L',12},{'M',13},{'N',14},{'O',15},{'P',16},{'Q',17},{'R',18},{'S',19},{'T',20},{'U',21},{'V',22},{'W',23},{'X',24},{'Y',25},{'Z',26},{'@',27}
  95. };
  96. map<int, char> mapOfButtonsReverse = {
  97. {0,'^'},{1,'A'}, {2,'B'},{3,'C'},{4,'D'},{5,'E'},{6,'F'},{7,'G'},{8,'H'},{9,'I'},{10,'J'},{11,'K'},{12,'L'},{13,'M'},{14,'N'},{15,'O'},{16,'P'},{17,'Q'},{18,'R'},{19,'S'},{20,'T'},{21,'U'},{22,'V'},{23,'W'},{24,'X'},{25,'Y'},{26,'Z'},{27,'@'}
  98. };
  99. char validInputs[] = { '.','^','@','?','#','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','t','U','V','W','X','Y','Z' };
  100. /*
  101. struct trackingState {
  102. int row;
  103. int col;
  104. trackingState()
  105. :row(-1), col(-1) {}
  106. trackingState(int row_in, int col_in)
  107. : row(row_in), col(col_in){}
  108. };
  109. */
  110.  
  111. //Gonna need to fix the ^ version of saving the direction_vec with numbers likely need to set that number to 0!
  112. deque<objState> backTracking(objState &end, vector<vector<vector<string>>> &tracking_vec) {
  113. deque<objState> backtracking_collection;//TREAT THIS AS A STACK!!!
  114. backtracking_collection.push_back(end);
  115. while (true) {
  116.  
  117. if (tracking_vec[mapOfButtons.find(end.color)->second][end.row][end.col] == "N") {
  118. backtracking_collection.push_back(objState(end.color, end.row - 1, end.col));
  119. end.row = end.row - 1;
  120. }
  121. else if (tracking_vec[mapOfButtons.find(end.color)->second][end.row][end.col] == "E") {
  122. backtracking_collection.push_back(objState(end.color, end.row, end.col + 1));
  123. end.col = end.col + 1;
  124. }
  125. else if (tracking_vec[mapOfButtons.find(end.color)->second][end.row][end.col] == "W") {
  126. backtracking_collection.push_back(objState(end.color, end.row, end.col - 1));
  127. end.col = end.col - 1;
  128. }
  129. else if (tracking_vec[mapOfButtons.find(end.color)->second][end.row][end.col] == "S") {
  130. backtracking_collection.push_back(objState(end.color, end.row + 1, end.col));
  131. end.row = end.row + 1;
  132. }
  133. else if (tracking_vec[mapOfButtons.find(end.color)->second][end.row][end.col] == "-1") {
  134. return backtracking_collection;
  135. }
  136. else {
  137. end.color = mapOfButtonsReverse[stoi((tracking_vec[mapOfButtons.find(end.color)->second][end.row][end.col]))];
  138. backtracking_collection.push_back(objState(end.color, end.row, end.col));
  139. }
  140. }
  141. return backtracking_collection;
  142. }
  143.  
  144.  
  145. void listOutput(deque<objState> &backtracking_collection) {
  146. while (!backtracking_collection.empty()) {
  147. cout << "(" << (char)tolower(backtracking_collection.back().color) << ", " << "(" << backtracking_collection.back().row << ", " << backtracking_collection.back().col << "))\n";
  148. backtracking_collection.pop_back();
  149. }
  150. }
  151.  
  152. void mapOutput(vector<vector<vector<Tiles>>> &puzzle, deque<objState> &backtracking_collection) {
  153. objState current_state;
  154. while (backtracking_collection.size() > 1) {
  155. current_state = backtracking_collection.back();
  156. backtracking_collection.pop_back();
  157. //if colors same use +
  158. if (current_state.color == backtracking_collection.back().color) {
  159. puzzle[mapOfButtons.find(current_state.color)->second][backtracking_collection.back().row][backtracking_collection.back().col].value = '+';
  160. }
  161. else if (current_state.color != backtracking_collection.back().color) {
  162. puzzle[mapOfButtons.find(current_state.color)->second][backtracking_collection.back().row][backtracking_collection.back().col].value = '%';
  163. puzzle[mapOfButtons.find(backtracking_collection.back().color)->second][backtracking_collection.back().row][backtracking_collection.back().col].value = '@';
  164. backtracking_collection.pop_back();
  165. }
  166. }
  167. for (unsigned z = 0; z < puzzle.size(); ++z) {
  168. for (unsigned i = 0; i < puzzle[z].size(); ++i) {
  169. for (unsigned j = 0; j < puzzle[z][i].size(); ++j) {
  170. if (mapOfButtonsReverse.find(z)->second == toupper(puzzle[z][i][j].value))
  171. puzzle[z][i][j].value = '.';
  172. }
  173. }
  174. }
  175. }
  176.  
  177. bool checkNorth(vector<vector<vector<Tiles>>> &puzzle, deque<objState> &reachable_collection, objState &current_state, vector<vector<vector<string>>> &tracking_vec) {
  178. //Check for edge of map, wall, and been their before. NORTH
  179. if (current_state.row - 1 >= 0
  180. && (puzzle[mapOfButtons.find(current_state.color)->second][current_state.row - 1][current_state.col].value != '#')) {
  181. //Handling if not a free space (.)
  182. char northVal = puzzle[mapOfButtons.find(current_state.color)->second][current_state.row - 1][current_state.col].value;
  183. if (northVal != '.' && northVal != '^'&& northVal != '@') {
  184. if (northVal == '?') {
  185. reachable_collection.push_back(objState(current_state.color, current_state.row - 1, current_state.col));
  186. cout << "Found";
  187. return true;
  188. }
  189. //If current color != tile about to try to reach and not UpperCase letter, make new color
  190. if (current_state.color != northVal && mapOfButtons.find(northVal) == mapOfButtons.end()) {
  191. if (puzzle[mapOfButtons.find((char)toupper(northVal))->second][current_state.row - 1][current_state.col].visited == false && puzzle[mapOfButtons.find(current_state.color)->second][current_state.row - 1][current_state.col].visited == false) {
  192. reachable_collection.push_back(objState((char)toupper(northVal), current_state.row - 1, current_state.col));
  193. puzzle[mapOfButtons.find(current_state.color)->second][current_state.row - 1][current_state.col].visited = true;
  194. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row - 1][current_state.col] = 'S';
  195. tracking_vec[mapOfButtons.find((char)toupper(northVal))->second][current_state.row - 1][current_state.col] = to_string((int)mapOfButtons.find(current_state.color)->second);
  196.  
  197. }
  198. }
  199. //If not # and . but UpperCase and same as current color
  200. else if (northVal == current_state.color && northVal != '^') {
  201. if (puzzle[mapOfButtons.find((char)toupper(northVal))->second][current_state.row - 1][current_state.col].visited == false) {
  202. reachable_collection.push_back(objState((char)toupper(northVal), current_state.row - 1, current_state.col));
  203. puzzle[mapOfButtons.find((char)toupper(northVal))->second][current_state.row - 1][current_state.col].visited = true;
  204. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row - 1][current_state.col] = 'S';
  205. }
  206.  
  207. }
  208. }
  209. //Handling if free space (.)
  210. else {
  211. if (puzzle[mapOfButtons.find(current_state.color)->second][current_state.row - 1][current_state.col].visited == false) {
  212. if (northVal == '^') {
  213. reachable_collection.push_back(objState('^', current_state.row - 1, current_state.col));
  214. puzzle[0][current_state.row - 1][current_state.col].visited = true;
  215. if (tracking_vec[0][current_state.row - 1][current_state.col] == "-1") {
  216. tracking_vec[0][current_state.row - 1][current_state.col] = to_string((int)mapOfButtons.find(current_state.color)->second); // Ground floor set to where at currently
  217. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row - 1][current_state.col] = 'S';
  218. }
  219. }
  220. else {
  221. reachable_collection.push_back(objState(current_state.color, current_state.row - 1, current_state.col));
  222. puzzle[mapOfButtons.find(current_state.color)->second][current_state.row - 1][current_state.col].visited = true;
  223. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row - 1][current_state.col] = 'S';
  224. }
  225. }
  226. }
  227. }// //Check for edge of map, wall, and been their before. NORTH
  228. return false;
  229. }
  230.  
  231. bool checkEast(vector<vector<vector<Tiles>>> &puzzle, deque<objState> &reachable_collection, objState &current_state, int cols, vector<vector<vector<string>>> &tracking_vec) {
  232. //Check for edge of map, wall, and been their before. EAST
  233. if (current_state.col + 1 < cols // DOUBLE CHECK THIS LINE
  234. && (puzzle[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col + 1].value != '#')) {
  235. //Handling if not a free space (.)
  236. char northVal = puzzle[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col + 1].value;
  237. if (northVal != '.' && northVal != '^' && northVal != '@') {
  238. if (northVal == '?') {
  239. reachable_collection.push_back(objState(current_state.color, current_state.row, current_state.col + 1));
  240. cout << "Found";
  241. return true;
  242. }
  243. //If current color != tile about to try to reach and not UpperCase letter, make new color
  244. if (current_state.color != northVal && mapOfButtons.find(northVal) == mapOfButtons.end()) {
  245. if (puzzle[mapOfButtons.find((char)toupper(northVal))->second][current_state.row][current_state.col + 1].visited == false && puzzle[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col + 1].visited == false) {
  246. reachable_collection.push_back(objState((char)toupper(northVal), current_state.row, current_state.col + 1));
  247. puzzle[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col + 1].visited = true;
  248. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col + 1] = 'W';
  249. tracking_vec[mapOfButtons.find((char)toupper(northVal))->second][current_state.row][current_state.col + 1] = to_string((int)mapOfButtons.find(current_state.color)->second);
  250. }
  251. }
  252. //If not # and . but UpperCase and same as current color
  253. else if (northVal == current_state.color && northVal != '^') {
  254. if (puzzle[mapOfButtons.find((char)toupper(northVal))->second][current_state.row][current_state.col + 1].visited == false) {
  255. reachable_collection.push_back(objState((char)toupper(northVal), current_state.row, current_state.col + 1));
  256. puzzle[mapOfButtons.find((char)toupper(northVal))->second][current_state.row][current_state.col + 1].visited = true;
  257. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col + 1] = 'W';
  258. }
  259.  
  260. }
  261. }
  262. //Handling if free space (.)
  263. else {
  264. if (puzzle[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col + 1].visited == false) {
  265. if (northVal == '^') {
  266. reachable_collection.push_back(objState('^', current_state.row, current_state.col + 1));
  267. puzzle[0][current_state.row][current_state.col + 1].visited = true;
  268. if (tracking_vec[0][current_state.row][current_state.col + 1] == "-1") {
  269. tracking_vec[0][current_state.row][current_state.col + 1] = to_string((int)mapOfButtons.find(current_state.color)->second); // Ground floor set to where at currently
  270. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col + 1] = 'W';
  271. }
  272. }
  273. else {
  274. reachable_collection.push_back(objState(current_state.color, current_state.row, current_state.col + 1));
  275. puzzle[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col + 1].visited = true;
  276. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col + 1] = 'W';
  277. }
  278. }
  279. }
  280. }// //Check for edge of map, wall, and been their before. EAST
  281. return false;
  282. }
  283.  
  284. bool checkSouth(vector<vector<vector<Tiles>>> &puzzle, deque<objState> &reachable_collection, objState &current_state, int rows, vector<vector<vector<string>>> &tracking_vec) {
  285. //Check for edge of map, wall, and been their before. South
  286. if (current_state.row + 1 < rows
  287. && (puzzle[mapOfButtons.find(current_state.color)->second][current_state.row + 1][current_state.col].value != '#')) {
  288. //Handling if not a free space (.)
  289. char northVal = puzzle[mapOfButtons.find(current_state.color)->second][current_state.row + 1][current_state.col].value;
  290. if (northVal != '.' && northVal != '^'&& northVal != '@') {
  291. if (northVal == '?') {
  292. reachable_collection.push_back(objState(current_state.color, current_state.row + 1, current_state.col));
  293. cout << "Found";
  294. return true;
  295. }
  296. //If current color != tile about to try to reach and not UpperCase letter, make new color
  297. if (current_state.color != northVal && mapOfButtons.find(northVal) == mapOfButtons.end()) {
  298. if (puzzle[mapOfButtons.find((char)toupper(northVal))->second][current_state.row + 1][current_state.col].visited == false && puzzle[mapOfButtons.find(current_state.color)->second][current_state.row + 1][current_state.col].visited == false) {
  299. reachable_collection.push_back(objState((char)toupper(northVal), current_state.row + 1, current_state.col));
  300. puzzle[mapOfButtons.find(current_state.color)->second][current_state.row + 1][current_state.col].visited = true;
  301. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row + 1][current_state.col] = 'N';
  302. tracking_vec[mapOfButtons.find((char)toupper(northVal))->second][current_state.row + 1][current_state.col] = to_string((int)mapOfButtons.find(current_state.color)->second); // Handling level for output
  303. }
  304. }
  305. //If not # and . but UpperCase and same as current color
  306. else if (northVal == current_state.color && northVal != '^') {
  307. if (puzzle[mapOfButtons.find((char)toupper(northVal))->second][current_state.row + 1][current_state.col].visited == false) {
  308. reachable_collection.push_back(objState((char)toupper(northVal), current_state.row + 1, current_state.col));
  309. puzzle[mapOfButtons.find((char)toupper(northVal))->second][current_state.row + 1][current_state.col].visited = true;
  310. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row + 1][current_state.col] = 'N';
  311. }
  312.  
  313. }
  314. }
  315. //Handling if free space (.)
  316. else {
  317. if (puzzle[mapOfButtons.find(current_state.color)->second][current_state.row + 1][current_state.col].visited == false) {
  318. if (northVal == '^') {
  319. reachable_collection.push_back(objState('^', current_state.row + 1, current_state.col));
  320. puzzle[0][current_state.row + 1][current_state.col].visited = true;
  321. if (tracking_vec[0][current_state.row + 1][current_state.col] == "-1") {
  322. tracking_vec[0][current_state.row + 1][current_state.col] = to_string((int)mapOfButtons.find(current_state.color)->second); // Ground floor set to where at currently
  323. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row + 1][current_state.col] = 'N';//Came from B will make jump to B floor
  324. }
  325. }
  326. else {
  327. reachable_collection.push_back(objState(current_state.color, current_state.row + 1, current_state.col));
  328. puzzle[mapOfButtons.find(current_state.color)->second][current_state.row + 1][current_state.col].visited = true;
  329. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row + 1][current_state.col] = 'N';
  330. }
  331. }
  332. }
  333. }// //Check for edge of map, wall, and been their before. SOUTH
  334. return false;
  335. }
  336.  
  337. bool checkWest(vector<vector<vector<Tiles>>> &puzzle, deque<objState> &reachable_collection, objState &current_state, vector<vector<vector<string>>> &tracking_vec) {
  338. //Check for edge of map, wall, and been their before. West
  339. if (current_state.col - 1 >= 0 // DOUBLE CHECK THIS LINE
  340. && (puzzle[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col - 1].value != '#')) {
  341. //Handling if not a free space (.)
  342. char northVal = puzzle[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col - 1].value;
  343. if (northVal != '.' && northVal != '^'&& northVal != '@') {
  344. if (northVal == '?') {
  345. reachable_collection.push_back(objState(current_state.color, current_state.row, current_state.col - 1));
  346. cout << "Found";
  347. return true;
  348. }
  349. //If current color != tile about to try to reach and not UpperCase letter, make new color
  350. if (current_state.color != northVal && mapOfButtons.find(northVal) == mapOfButtons.end()) {
  351. if (puzzle[mapOfButtons.find((char)toupper(northVal))->second][current_state.row][current_state.col - 1].visited == false && puzzle[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col - 1].visited == false) {
  352. reachable_collection.push_back(objState((char)toupper(northVal), current_state.row, current_state.col - 1));
  353. puzzle[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col - 1].visited = true;
  354. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col - 1] = 'E';
  355. tracking_vec[mapOfButtons.find((char)toupper(northVal))->second][current_state.row][current_state.col - 1] = to_string((int)mapOfButtons.find(current_state.color)->second);
  356. }
  357. }
  358. //If not # and . but UpperCase and same as current color
  359. else if (northVal == current_state.color && northVal != '^') {
  360. if (puzzle[mapOfButtons.find((char)toupper(northVal))->second][current_state.row][current_state.col - 1].visited == false) {
  361. reachable_collection.push_back(objState((char)toupper(northVal), current_state.row, current_state.col - 1));
  362. puzzle[mapOfButtons.find((char)toupper(northVal))->second][current_state.row][current_state.col - 1].visited = true;
  363. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col - 1] = 'E';
  364. }
  365.  
  366. }
  367. }
  368. //Handling if free space (.)
  369. else {
  370. if (puzzle[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col - 1].visited == false) {
  371. if (northVal == '^') {
  372. reachable_collection.push_back(objState('^', current_state.row, current_state.col - 1));
  373. puzzle[0][current_state.row][current_state.col - 1].visited = true;
  374. if (tracking_vec[0][current_state.row][current_state.col - 1] == "-1") {
  375. tracking_vec[0][current_state.row][current_state.col - 1] = to_string((int)mapOfButtons.find(current_state.color)->second); // Ground floor set to where at currently
  376. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col - 1] = 'E';
  377. }
  378. }
  379. else {
  380. reachable_collection.push_back(objState(current_state.color, current_state.row, current_state.col - 1));
  381. puzzle[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col - 1].visited = true;
  382. tracking_vec[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col - 1] = 'E';
  383. }
  384. }
  385. }
  386. }// //Check for edge of map, wall, and been their before. WEST
  387. return false;
  388. }
  389. int main(int argc, char *argv[]) {
  390. ios_base::sync_with_stdio(false);
  391. //Getting Command Line Stuff
  392. std::pair<string, string> optionQorS = getOptions(argc, argv);
  393. if (optionQorS.first == "" || optionQorS.second == "") {
  394. cerr << "Error";
  395. exit(1);
  396. }
  397.  
  398. //Reading in first line
  399. int colors;
  400. cin >> colors;
  401. int rows;
  402. cin >> rows;
  403. int cols;
  404. cin >> cols;
  405.  
  406. //Error checking
  407. if (colors < 0 || colors > 26) {
  408. cerr << "Error";
  409. exit(1);
  410. }
  411. if (rows < 1 || cols < 1) {
  412. cerr << "Error";
  413. exit(1);
  414. }
  415. //Reading in Rest lines
  416. string val;
  417. vector<vector<vector<Tiles>>> puzzle(colors + 1, vector<vector<Tiles>>(rows, vector<Tiles>(cols)));
  418. vector<vector<vector<string>>> direction_vec(colors + 1, vector<vector<string>>(rows, vector<string>(cols, "-1")));
  419.  
  420.  
  421.  
  422. //cin.ignore();
  423. string ignore;
  424. getline(cin, ignore);
  425. int row_num = 0;
  426. while (getline(cin, val)) {
  427. if (val[0] != '/') {
  428. for (int i = 0; i < cols; ++i) {
  429. for (int abc = 0; abc < colors + 1; ++abc) {
  430. //Error checking No invalid characters appear in the map (for example, '+' can’t appear in the map, but it could appear in a comment)
  431. if (std::find(std::begin(validInputs), std::end(validInputs), val[i]) == std::end(validInputs)) {
  432. cerr << "Error";
  433. exit(1);
  434. }
  435. //● No invalid door or button appears in the map (for example, if num_colors is 3, then 'z' is invalid)
  436. if (mapOfButtons.find((val[i])) != mapOfButtons.end()) {
  437. if (mapOfButtons.find(val[i])->second > colors) {
  438. cerr << "Error";
  439. exit(1);
  440. }
  441. }
  442. puzzle[abc][row_num][i] = Tiles(val[i], false);
  443. }
  444. }
  445. row_num++;
  446. }
  447. if (row_num == rows)
  448. break;
  449. }
  450.  
  451.  
  452. deque<objState> reachable_collection;
  453. objState startLocation;
  454. objState endLocation;
  455.  
  456.  
  457.  
  458.  
  459. //Find starting position and put that into dequ as current state
  460. for (int i = 0; i < rows; ++i) {
  461. for (int j = 0; j < cols; ++j) {
  462. if (puzzle[0][i][j].value == '@') {
  463. reachable_collection.push_back(objState('^', i, j));
  464. startLocation = objState('^', i, j);
  465. break;
  466. }
  467. }
  468. }
  469. //Find more reachable states now from this original starting position.
  470. while (true) {
  471. if (reachable_collection.empty()) {
  472. cout << "No solution";
  473. return 0;
  474. }
  475.  
  476. objState current_state;
  477.  
  478. //Stack Mode
  479. if (optionQorS.first == "stack") {
  480. current_state = reachable_collection.back();
  481. reachable_collection.pop_back();
  482. }
  483. //Queue Mode
  484. if (optionQorS.first == "queue")
  485. current_state = reachable_collection.front();
  486.  
  487. puzzle[mapOfButtons.find(current_state.color)->second][current_state.row][current_state.col].visited = true;
  488.  
  489. if (checkNorth(puzzle, reachable_collection, current_state, direction_vec)) {
  490. endLocation = reachable_collection.back();
  491. direction_vec[mapOfButtons.find(endLocation.color)->second][endLocation.row][endLocation.col] = "S";
  492. break;
  493. }
  494. if (checkEast(puzzle, reachable_collection, current_state, cols, direction_vec)) {
  495. endLocation = reachable_collection.back();
  496. direction_vec[mapOfButtons.find(endLocation.color)->second][endLocation.row][endLocation.col] = "W";
  497. break;
  498. }
  499. if (checkSouth(puzzle, reachable_collection, current_state, rows, direction_vec)) {
  500. endLocation = reachable_collection.back();
  501. direction_vec[mapOfButtons.find(endLocation.color)->second][endLocation.row][endLocation.col] = "N";
  502. break;
  503. }
  504. if (checkWest(puzzle, reachable_collection, current_state, direction_vec)) {
  505. endLocation = reachable_collection.back();
  506. direction_vec[mapOfButtons.find(endLocation.color)->second][endLocation.row][endLocation.col] = "E";
  507. break;
  508. }
  509.  
  510. //Queue mode
  511. if (optionQorS.first == "q" || "queue")
  512. reachable_collection.pop_front();
  513.  
  514. }//While loop
  515. cout << "\n";
  516. /*
  517. for (int z = 0; z < colors + 1; ++z) {
  518. for (int i = 0; i < rows; ++i) {
  519. for (int j = 0; j < cols; ++j) {
  520. cout << direction_vec[z][i][j] << " ";
  521. if (direction_vec[z][i][j] != "-1")
  522. cout << " ";
  523. }
  524. cout << " " << i << " \n";
  525. }
  526. cout << "\n\n";
  527. }
  528. */
  529.  
  530. deque<objState> tracking_collection = backTracking(endLocation, direction_vec);
  531. listOutput(tracking_collection);
  532. /*mapOutput(puzzle, tracking_collection);
  533.  
  534. for (int z = 0; z < colors + 1; ++z) {
  535. for (int i = 0; i < rows; ++i) {
  536. for (int j = 0; j < cols; ++j) {
  537. cout << puzzle[z][i][j].value;
  538. }
  539. cout << " \n";
  540. }
  541. cout << "\n\n";
  542. }
  543. */
  544.  
  545. return 0;
  546. }
  547. /*
  548. //Direction we got it from,
  549. sofor back tracking, i came from S N W E
  550. I came here from carrot color
  551.  
  552. could make predisesor const to make sure all good
  553.  
  554. USE STACK FOR LIST OUTPUT!!! OR VECTOR STACK PREF NO QUEUQ
  555. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement