• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Feb 14th, 2020 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <iostream>
2. using namespace std;
3.
4. void way(int curX, int curY, int x2, int y2, int n, int m, int pathPointer, int& steps, int path[]) { //cur is short for current
5.     if ((curX == x2) && (curY == y2)) { return; }
6.     for (int i = 0; i < 4; i++) {
7.         switch (i){
8.             case 0: {
9.                 if ((curY-1 >= 0) && (curY-1 < n)) {
10.                     steps++; path[pathPointer++] = 0;
11.                     way(curX, curY-1, x2, y2, n, m, pathPointer, steps, path);
12.                     steps--; pathPointer--;
13.                 }
14.                 break;
15.             }
16.             case 1: {
17.                 if ((curX+1 >= 0) && (curX+1 < m)) {
18.                     steps++; path[pathPointer++] = 1;
19.                     way(curX+1, curY, x2, y2, n, m, pathPointer, steps, path);
20.                     steps--; pathPointer--;
21.                 }
22.                 break;
23.             }
24.             case 2: {
25.                 if ((curY+1 >= 0) && (curY+1 < n)) {
26.                     steps++; path[pathPointer++] = 2;
27.                     way(curX, curY+1, x2, y2, n, m, pathPointer, steps, path);
28.                     steps--; pathPointer--;
29.                 }
30.                 break;
31.             }
32.             case 3: {
33.                 if ((curX-1 >= 0) && (curX-1 < m)) {
34.                     steps++; path[pathPointer++] = 3;
35.                     way(curX-1, curY, x2, y2, n, m, pathPointer, steps, path);
36.                     steps--; pathPointer--;
37.                 }
38.                 break;
39.             }
40.         }
41.     }
42. }
43.
44. int main()
45. {
46.     int n, m, x1, y1, x2, y2, labyrinth = {}, path = {}, steps = 0;
47.     cout << "Enter X1 = "; cin >> x1;
48.     cout << "Enter Y1 = "; cin >> y1;
49.     cout << "Enter X2 = "; cin >> x2;
50.     cout << "Enter Y2 = "; cin >> y2;
51.     cout << "Enter N (height) = "; cin >> n;
52.     cout << "Enter M (length) = "; cin >> m;
53.     cout << "start entering the labyrinth now! 1 - blocked, 0 - free\n";
54.     for (int i = 0; i < n; i++) {
55.         for (int j = 0; j < m; j++) {
56.             cin >> labyrinth[i][j];
57.         }
58.     }
59.     way(x1, y1, x2, y2, n, m, 0, steps, path);
60.     if (steps == 0) {
61.         cout << "No way found!";
62.     } else {
63.         for (int i = 0; i < steps; i++) {
64.             switch (path[i]) {
65.                 case 0: {
66.                     cout << "up ";
67.                     break;
68.                 }
69.                 case 1: {
70.                     cout << "right ";
71.                     break;
72.                 }
73.                 case 2: {
74.                     cout << "down ";
75.                     break;
76.                 }
77.                 case 3: {
78.                     cout << "left ";
79.                     break;
80.                 }
81.             }
82.         }
83.     }
84.     return 0;
85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top