forked from ujjwalpuri29/Maze-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.cpp
103 lines (86 loc) · 2.39 KB
/
Code.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <bits/stdc++.h>
using namespace std;
#define N 5
int minCost(int maze[N][N], int x, int y, int sol[N][N]);
int getDirection(int maze[N][N], int x, int y, int sol[N][N]);
void printSolution(int arr[N][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
printf("%d\t", arr[i][j]);
printf("\n");
}
}
void solveMaze(int maze[N][N])
{
int sol[N][N] = {0};
int x = minCost(maze, N - 1, 0, sol);
cout<<"Cost: "<<x<<endl;
if (x!=0) {
cout<<"\nSolution:\n";
printSolution(sol);
cout<<"\n[1 denotes path taken]\n";
}
else cout<<"The least cost path was blocked by walls on all sides.\n";
}
int minCost(int maze[N][N], int x, int y, int sol[N][N]) {
int cost = maze[x][y];
while (x != 0 || y != N - 1) {
int dir = getDirection(maze, x, y, sol);
sol[x][y] = 1;
switch(dir){
case 1: cost += maze[--x][y]; break;
case 2: cost += maze[x][++y]; break;
case 3: cost += maze[++x][y]; break;
case 4: cost += maze[x][--y]; break;
case 0: cost = 0; return cost;
}
}
sol[0][N-1] = 1;
return cost;
}
int getDirection(int maze[N][N], int x, int y, int sol[N][N])
{
int min = 1000;
int dir = 0;
if (0 <= (x-1) && (x-1) < N && 0 <= y && y < N && sol[x-1][y] != 1) {
if (maze[x-1][y] < min && maze[x-1][y] != 0) {
min = maze[x-1][y];
dir = 1; //up
}
}
if (0 <= x && x < N && 0 <= (y+1) && (y+1) < N && sol[x][y+1] != 1) {
if (maze[x][y+1] < min && maze[x][y+1] != 0) {
min = maze[x][y+1];
dir = 2; //right
}
}
if (0 <= (x+1) && (x+1) < N && 0 <= y && y < N && sol[x+1][y] != 1) {
if (maze[x+1][y] < min && maze[x+1][y] != 0){
min = maze[x+1][y];
dir = 3; //down
}
}
if (0 <= x && x < N && 0 <= (y-1) && (y-1) < N && sol[x][y-1] != 1) {
if (maze[x][y-1] < min && maze[x][y-1] != 0){
min = maze[x][y-1];
dir = 4; //left
}
}
return dir;
}
int main()
{
int maze[N][N] = {
31, 100, 65, 12, 18,
-1, 13, -89, 170, 6,
0, 113, 0, 11, 33,
65, 182, -20, 50, 0,
99, 32, 11, 41, 0
};
cout<<"\nMaze: \n[0 denotes walls]\n\n";
printSolution(maze);
cout<<endl;
solveMaze(maze);
return 0;
}