Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. /*******************************************************************************
  2. * Name : waterjugpuzzle.cpp
  3. * Author : Albert Chen
  4. * Date : October 15th, 2019
  5. * Description : Solves the water jug puzzle.
  6. * Pledge : I pledge my honor that I have abided by the Stevens Honor System.
  7. ******************************************************************************/
  8. #include <iostream>
  9. #include <vector>
  10. #include <algorithm>
  11. #include <sstream>
  12. #include <iomanip>
  13.  
  14. using namespace std;
  15.  
  16. // Struct to represent state of water in the jugs.
  17. struct State {
  18. int a, b, c;
  19. vector<string> directions;
  20.  
  21. State(int _a, int _b, int _c) : a(_a), b(_b), c(_c) { }
  22.  
  23. // String representation of state in tuple form.
  24. string to_string() {
  25. ostringstream oss;
  26. oss << "(" << a << ", " << b << ", " << c << ")";
  27. return oss.str();
  28. }
  29. };
  30.  
  31. //if been visited already, dont put it in the queue
  32. //break outta queue while not empty loop when u find the goal
  33. //if the queue is empty, return 0
  34. //function that returns a vector that contains all the possible states u can go from current
  35. //vectors of strings in the state class: add the vector after every move
  36.  
  37. //dhorowit@stevens.edu
  38.  
  39. //returns a vector of the possible moves for that state
  40. vector<State> possiblepours(State s, int capA, int capB, int capC)
  41. {
  42. vector<State> possible;
  43. //C to A
  44. if (s.a != capA && s.c != 0)
  45. {
  46. State temp(s.a,s.b,s.c);
  47. temp.directions = s.directions;
  48. int pamount;
  49. if (temp.c + temp.a <= capA)
  50. {
  51. pamount = temp.c;
  52. temp.a += pamount;
  53. temp.c = 0;
  54. }
  55. else
  56. {
  57. pamount = capA - temp.a;
  58. temp.a += pamount;
  59. temp.c -= pamount;
  60. }
  61. temp.directions.push_back("Pour " << pamount << " gallons from C to A.");
  62. possible.push_back(temp);
  63. }
  64. //B to A
  65. if (s.a != capA && s.b != 0)
  66. {
  67. State temp(s.a,s.b,s.c);
  68. temp.directions = s.directions;
  69. int pamount;
  70. if (temp.b + temp.a <= capA)
  71. {
  72. pamount = temp.b;
  73. temp.a += pamount;
  74. temp.b = 0;
  75. }
  76. else
  77. {
  78. pamount = capA - temp.a;
  79. temp.a += pamount;
  80. temp.b -= pamount;
  81. }
  82. temp.directions.push_back("Pour " << pamount << " gallons from B to A.");
  83. possible.push_back(temp);
  84. }
  85. //C to B
  86. if (s.b != capB && s.c != 0)
  87. {
  88. State temp(s.a,s.b,s.c);
  89. temp.directions = s.directions;
  90. int pamount;
  91. if (temp.c + temp.b <= capB)
  92. {
  93. pamount = temp.c;
  94. temp.b += pamount;
  95. temp.c = 0;
  96. }
  97. else
  98. {
  99. pamount = capB - temp.a;
  100. temp.b += pamount;
  101. temp.c -= pamount;
  102. }
  103. temp.directions.push_back("Pour " << pamount << " gallons from C to B.");
  104. possible.push_back(temp);
  105. }
  106. //A to B
  107. if (s.b != capB && s.a != 0)
  108. {
  109. State temp(s.a,s.b,s.c);
  110. temp.directions = s.directions;
  111. int pamount;
  112. if (temp.a + temp.b <= capA)
  113. {
  114. pamount = temp.a;
  115. temp.b += pamount;
  116. temp.a = 0;
  117. }
  118. else
  119. {
  120. pamount = capB - temp.a;
  121. temp.b += pamount;
  122. temp.a -= pamount;
  123. }
  124. temp.directions.push_back("Pour " << pamount << " gallons from A to B.");
  125. possible.push_back(temp);
  126. }
  127. //B to C
  128. if (s.c != capC && s.b != 0)
  129. {
  130. State temp(s.a,s.b,s.c);
  131. temp.directions = s.directions;
  132. int pamount;
  133. if (temp.b + temp.c <= capA)
  134. {
  135. pamount = temp.b;
  136. temp.c += pamount;
  137. temp.b = 0;
  138. }
  139. else
  140. {
  141. pamount = capC - temp.b;
  142. temp.c += pamount;
  143. temp.b -= pamount;
  144. }
  145. temp.directions.push_back("Pour " << pamount << " gallons from B to C.");
  146. possible.push_back(temp);
  147. }
  148. return possible;
  149. }
  150. State breadthSearch (vector<int> nums)
  151. {
  152.  
  153. int cols = nums[0]+1;
  154. int rows = nums[1]+1;
  155. bool** visited = new bool*[rows];
  156. for (int i=0; i<rows; i++)
  157. {
  158. visited[i] = new bool[cols];
  159. fill(visited[i], visited[i] + cols, false);
  160. }
  161.  
  162.  
  163. vector<State> queue;
  164. vector<State> moves;
  165. State start(0, 0, nums[2]);
  166. State goal(nums[3], nums[4], nums[5]);
  167. State current(0, 0, 0);
  168. queue.push_back(start);
  169. while (!(queue.empty()))
  170. {
  171. current = queue.pop_front (); //ERROR
  172. if (current.a == goal.a && current.b == goal.b && current.c == goal.c)
  173. {
  174. break;
  175. }
  176. else
  177. {
  178. moves = possiblepours(current, current.a, current.b, current.c);
  179. for (int i = 0; i < (int)moves.size(); i++)
  180. {
  181. if (moves[i].a == goal.a && moves[i].b == goal.b && moves[i].c == goal.c)
  182. {
  183. current = moves[i];
  184. break; //only breaks out of 1 loop, need to fix
  185. }
  186.  
  187. if (visited[moves[i].a][moves[i].b] == false)
  188. {
  189. queue.push_back(moves[i]);
  190. visited[moves[i].a][moves[i].b] = true;
  191. }
  192. }
  193.  
  194.  
  195. }
  196.  
  197.  
  198. }
  199.  
  200.  
  201. for (int i = 0; i < cols; i++)
  202. {
  203. delete [] visited[i];
  204. }
  205. delete [] visited;
  206.  
  207. return current;
  208. }
  209.  
  210.  
  211.  
  212. int main(int argc, char * const argv[]) {
  213. vector<int> nums;
  214. //all user input works
  215. if (argc != 7)
  216. {
  217. cout << "Usage: ./waterjugpuzzle <cap A> <cap B> <cap C> <goal A> <goal B> <goal C>" << endl;
  218. return 0;
  219. }
  220.  
  221. for (int i = 1; i < 4; i++)
  222. {
  223. istringstream iss(argv[i]);
  224. int isnum;
  225. if (!(iss>>isnum) || isnum <= 0)
  226. {
  227. if (i == 1)
  228. {
  229. cout << "Error: Invalid capacity '" << argv[i] << "' for jug A." << endl;
  230. }
  231. if (i == 2)
  232. {
  233. cout << "Error: Invalid capacity '" << argv[i] << "' for jug B." << endl;
  234. }
  235. if (i == 3)
  236. {
  237. cout << "Error: Invalid capacity '" << argv[i] << "' for jug C." << endl;
  238. }
  239. return 1;
  240. }
  241.  
  242. nums.push_back(isnum);
  243.  
  244. }
  245. for (int i = 4; i < 7; i++)
  246. {
  247. istringstream iss(argv[i]);
  248. int isnum;
  249. if (!(iss>>isnum) || isnum < 0)
  250. {
  251. if (i == 4)
  252. {
  253. cout << "Error: Invalid goal '" << argv[i] << "' for jug A." << endl;
  254. }
  255. if (i == 5)
  256. {
  257. cout << "Error: Invalid goal '" << argv[i] << "' for jug B." << endl;
  258. }
  259. if (i == 6)
  260. {
  261. cout << "Error: Invalid goal '" << argv[i] << "' for jug C." << endl;
  262. }
  263. return 1;
  264. }
  265. nums.push_back(isnum);
  266. }
  267. for (int i = 4; i < 7; i++)
  268. {
  269. if (nums[i-1] > nums[i-4])
  270. {
  271. if (i == 4)
  272. {
  273. cout << "Error: Goal cannot exceed capacity of jug A." << endl;
  274. }
  275. if (i == 5)
  276. {
  277. cout << "Error: Goal cannot exceed capacity of jug B." << endl;
  278. }
  279. if (i == 6)
  280. {
  281. cout << "Error: Goal cannot exceed capacity of jug C." << endl;
  282. }
  283. return 1;
  284. }
  285. }
  286. if (!(nums[4] + nums[5] + nums[6] == nums[3]))
  287. {
  288. cout << "Error: Total gallons in goal state must be equal to the capacity of jug C." << endl;
  289. return 0;
  290. }
  291. //----------------------------------------------------------------------------------------------------
  292.  
  293.  
  294. State found = breadthSearch(nums);
  295. cout << "Initial state. (0, 0, " << num[2] <<")." << endl;
  296. int patha, pathb, pathc;
  297. for (int i = 0; i < (int) found.size(); i++)
  298. {
  299.  
  300. }
  301.  
  302. /*State s(0, 0, 8);
  303. cout << s.to_string() << endl;
  304. s.a += 3;
  305. s.c -= 3;
  306. cout << s.to_string() << endl;*/
  307. return 0;
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement