Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. // Labyrinthv2.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. void varInput(int& x1, int& y1, int& x2, int& y2, int& h, int& l, int labyrinth[][100]) {
  8. cout << "Enter X1 = "; cin >> x1;
  9. cout << "Enter Y1 = "; cin >> y1;
  10. cout << "Enter X2 = "; cin >> x2;
  11. cout << "Enter Y2 = "; cin >> y2;
  12. cout << "Enter H (height) = "; cin >> h;
  13. cout << "Enter L (length) = "; cin >> l;
  14. cout << "start entering the labyrinth now! 1 - blocked, 0 - free\n";
  15. cout << "Press space after each number, enter after each row for clarity and program stability\n";
  16. x1 -= 1; y1 -= 1; x2 -= 1; y2 -= 1;
  17. for (int i = 0; i < h; i++) {
  18. for (int j = 0; j < l; j++) {
  19. cin >> labyrinth[j][i];
  20. }
  21. }
  22. }
  23.  
  24. void output(int path[], int steps, bool possible) {
  25. if (possible == true) {
  26. for (int i = 0; i < steps; i++) {
  27. switch (path[i]) {
  28. case 0: {
  29. cout << "up ";
  30. break;
  31. }
  32. case 1: {
  33. cout << "right ";
  34. break;
  35. }
  36. case 2: {
  37. cout << "down ";
  38. break;
  39. }
  40. case 3: {
  41. cout << "left ";
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. else {
  48. cout << "No way found!";
  49. }
  50. }
  51.  
  52. bool way(int curX, int curY, int finX, int finY, int h, int l, int& pathPointer, int path[], int was[][100], int labyrinth[][100]) { //cur is short for current
  53. if ((curX == finX) && (curY == finY)) { return true; }
  54. //lol
  55. if ((curY - 1 >= 0) && (curY - 1 < h) && (was[curX][curY - 1] != 1) && (labyrinth[curX][curY - 1] != 1)) { //up
  56. path[pathPointer++] = 0; was[curX][curY - 1] = 1;
  57. if (way(curX, curY - 1, finX, finY, h, l, pathPointer, path, was, labyrinth) == true) { return true; }
  58. pathPointer--;
  59. }
  60. //lol
  61. if ((curX + 1 >= 0) && (curX + 1 < l) && (was[curX + 1][curY] != 1) && (labyrinth[curX + 1][curY] != 1)) { //right
  62. path[pathPointer++] = 1; was[curX + 1][curY] = 1;
  63. if (way(curX + 1, curY, finX, finY, h, l, pathPointer, path, was, labyrinth) == true) { return true; }
  64. pathPointer--;
  65. }
  66. //lol
  67. if ((curY + 1 >= 0) && (curY + 1 < h) && (was[curX][curY + 1] != 1) && (labyrinth[curX][curY + 1] != 1)) { //down
  68. path[pathPointer++] = 2; was[curX][curY + 1] = 1;
  69. if (way(curX, curY + 1, finX, finY, h, l, pathPointer, path, was, labyrinth) == true) { return true; }
  70. pathPointer--;
  71. }
  72. //lol
  73. if ((curX - 1 >= 0) && (curX - 1 < l) && (was[curX - 1][curY ] != 1) && (labyrinth[curX - 1][curY] != 1)) { //left
  74. path[pathPointer++] = 3; was[curX - 1][curY] = 1;
  75. if (way(curX - 1, curY, finX, finY, h, l, pathPointer, path, was, labyrinth) == true) { return true; }
  76. pathPointer--;
  77. }
  78. return false;
  79. }
  80.  
  81.  
  82. int main()
  83. {
  84. int h, l, x1, y1, x2, y2, labyrinth[100][100] = {}, path[100] = {}, was[100][100] = {}, pathPointer = 0;
  85. varInput(x1, y1, x2, y2, h, l, labyrinth); was[x1][y1] = 1;
  86. bool possible = way(x1, y1, x2, y2, h, l, pathPointer, path, was, labyrinth);
  87. output(path, pathPointer, possible);
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement