Repository navigation
Expand file tree
/
Copy pathCarParking.cpp
More file actions
102 lines (101 loc) · 1.99 KB
/
Copy pathCarParking.cpp
File metadata and controls
102 lines (101 loc) · 1.99 KB
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
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
#include <sstream>
using namespace std;
void Play(int grid[4][5]);
int Right_Stairs(int grid[4][5], int idx, int rows);
int Left_Stairs(int grid[4][5], int idx, int rows);
int main()
{
int grid[4][5] = {
{1, 0, 0, 2, 0},
{0, 0, 0, 1, 0},
{0, 1, 0, 0, 0},
{0, 0, 0, 0, 0}};
Play(grid);
return 0;
}
void Play(int grid[4][5])
{
int idx = 0,count = 0;
for (int i = 0; i < 5; i++)
{
if (grid[0][i] == 2)
{
idx = i;
}
}
for (int i = 0; i < 3; i++)
{
idx = Right_Stairs(grid, idx, i);
}
for (int i = idx; i < 5; i++)
{
count++;
}
count--;
if (count!=0)
{
cout<<"Move Right By : "<<count<<endl;
}
else
cout<<"Stay Here"<<endl<<endl;
cout<<"Park Here >>> :)";
}
int Right_Stairs(int grid[4][5], int idx, int rows)
{
int count = 0, stair_idx = 0;
for (int i = idx; i < 5; i++)
{
count++;
if (grid[rows][i] == 1)
{
count--;
stair_idx = i;
break;
}
if (i == 4)
{
count = 0;
}
}
if (count != 0)
{
cout << "Move Right By: " << count << endl;
return stair_idx;
}
else
{
Left_Stairs(grid, idx, rows);
}
}
int Left_Stairs(int grid[4][5], int idx, int rows)
{
int count = 0, stair_idx = 0;
for (int i = idx; i >= 0; i--)
{
count++;
if (grid[rows][i] == 1)
{
stair_idx = i;
count--;
break;
}
if (i == 0)
{
count = 0;
}
}
if (count != 0)
{
cout << "Move Left By: " << count << endl;
return stair_idx;
}
else
{
cout << "No Stairs Found....";
exit(0);
}
}