Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. HOMEWORK #5:
  2. Heist!
  3. Due Date: Friday, April the 7th, 11:59:59pm
  4.  
  5. For this assignment, submit as many files as you need, but let your ‘main()’ function be in a file called : ‘heist.cpp’. Remember to put your name and section at the top of all files. Your program should expect all input to come from ‘cin’, and all your output should be to ‘cout’.
  6. Problem:
  7. Bender is trapped in the middle of a heist! Suppose the bank Bender is robbing can be mapped as a 2 dimensional grid. Some cells are occupied by walls, so Bender cannot enter them. There are also traps in some of the cells that will do terrible things to Bender if disturbed.
  8.  
  9. Your job is to write a program that finds, for every map, a path to the exit.
  10.  
  11. Input:
  12. The input will consist of a sequence of maps. Each map input starts with the number of rows and the number of columns of the map. In a map, a ‘#’ character denotes a wall. A character ‘T’ denotes a trap. A ‘ ‘ (blank space) character denotes a clear section. ‘B’ marks Bender’s starting point and ‘E’ marks the exit. The input is finished when a map of size 0 by 0 is indicated.
  13. Output:
  14. Output each map with a path from the “Start Point” to the “Exit” . Mark the path using cookie crumbles (character ‘.’). Follow the format as in the sample output.
  15. Details:
  16. Use Recursive Backtracking.
  17. Bender can move in any of the cardinal directions (North, East, West and South). No diagonal moves are possible.
  18.  
  19. Implementation Guidelines:
  20. Build your own simple test cases.
  21. Print plenty of status messages to track the progress of your algorithm.
  22. Start with the Recursive Backtracking algorithm, and refine it into your implementation as done in class.
  23. Recursive Backtracking Algorithm:
  24. solve i’th step
  25. Initialize possible choices
  26. DO
  27. select choice
  28. IF choice acceptable
  29. record choice
  30. IF solution complete
  31. return success
  32. ELSE
  33. solved = solve i+1’th step
  34. IF solved
  35. return success
  36. ELSE
  37. undo choice recording
  38. WHILE more choices available.
  39. return fail
  40. First Refinement:
  41. solve (x, y)
  42. choices = {north, south, east, west}
  43. FOR each choice C
  44. x', y' = moving in the C direction from x,y
  45. IF x', y' is a valid location
  46. record C
  47. IF exit at x', y'
  48. return success
  49. ELSE
  50. solved = solve(x', y')
  51. IF solved
  52. return success
  53. ELSE
  54. undo recording of C
  55. RETURN fail
  56. Reading Lines with White-Space:
  57. In this assignment, you are required to read lines with white spaces. You may attempt to use something like:
  58. cin >> maze[i][j];
  59. But that would NOT work, as the extraction operator ‘>>’ ignores white spaces.
  60.  
  61. You will therefore be forced to use one of the following library functions:
  62. string function getline() to read string objects.
  63. stream function getline() to read “null terminated character arrays”.
  64. stream function get() to read character by character.
  65.  
  66. Code Sample #1: Reading Strings objects:
  67.  
  68. // Maze is an array of strings
  69. string* maze;
  70.  
  71. // Readin size of maze
  72. cin >> cs >> rs;
  73. cout << cs << " " << rs << endl;
  74. cin.ignore(); // to move read head to next line
  75.  
  76. // Allocate Maze Array
  77. maze = new string[rs];
  78.  
  79. // Read Maze
  80. // each row is a string;
  81. for(int k=0; k < rs; k++){
  82. getline(cin, maze[k]);
  83. }
  84.  
  85. // Print Maze Array
  86. for(int k=0; k < rs; k++){
  87. cout << maze[k] << endl;
  88. }
  89.  
  90. // De-allocate Maze Array
  91. delete [] maze;
  92.  
  93.  
  94. Code Sample #2: Reading “Null Terminated Character Arrays”:
  95.  
  96. // Maze is a 2D array of characters
  97. char** maze;
  98.  
  99. // Readin size of Maze
  100. cin >> cs >> rs;
  101. cout << cs << " " << rs << endl;
  102. cin.ignore(); // to move read head to next line
  103.  
  104. // Allocate Maze Array
  105. // Notice that an EXTRA cell is added to the columns
  106. // to account for NULL termination
  107. maze = new char*[rs];
  108. for(int k=0; k < rs; k++){
  109. maze[k] = new char[cs+1];
  110. }
  111.  
  112. // Read Maze Array
  113. // Notice that we are reading each line as
  114. // a "NULL Terminated Character Array"
  115. for(int k=0; k < rs; k++){
  116. cin.getline(maze[k], cs+1);
  117. }
  118.  
  119. // Print Maze Array
  120. for(int k=0; k < rs; k++){
  121. cout << maze[k] << endl;
  122. }
  123.  
  124. // De-allocate Maze Array
  125. for(int k=0; k < rs; k++){
  126. delete [] maze[k];
  127. }
  128. delete [] maze;
  129.  
  130.  
  131.  
  132. Code Sample #3: Reading Character by Character:
  133. // Maze is a 2D array of characters
  134. char** maze;// Readin size of Maze
  135.  
  136. // Readin size of Maze
  137. cin >> cs >> rs;
  138. cout << cs << " " << rs << endl;
  139. cin.ignore(); // to move read head to next line
  140.  
  141. // Allocate Maze Array
  142. maze = new char*[rs];
  143. for(int k=0; k < rs; k++){
  144. maze[k] = new char[cs];
  145. }
  146.  
  147. // Read Maza Array
  148. // Notice that we are reading *Character by Character*
  149. // and after every row we need to read an extra character
  150. // to account for the 'end-of-line' character
  151. char dummy;
  152. for(int k=0; k < rs; k++){
  153. for(int j=0; j < cs; j++){
  154. cin.get(maze[k][j]);
  155. }
  156. cin.get(dummy); // read end-of-line
  157. }
  158.  
  159. // Print Maze Array
  160. for(int k=0; k < rs; k++){
  161. for(int j=0; j < cs; j++){
  162. cout << maze[k][j];
  163. }
  164. cout << endl; // read end-of-line
  165. }
  166.  
  167. // De-allocate Maze Array
  168. for(int k=0; k < rs; k++){
  169. delete [] maze[k];
  170. }
  171. delete [] maze;
  172.  
  173. END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement