-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVellore Mapping.cpp
186 lines (179 loc) · 5.61 KB
/
Vellore Mapping.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<iostream>
using namespace std;
struct node
{
node *parent;
int x, y;
float f, g, h;
};
node olist[100], clist[100];
int otop=-1, ctop=-1;
int main()
{
int sx, sy, fx, fy, i, vmap[10][10]={0}, least, j, tx, ty, k, l, x[16]={8,7,8,5,6,4,5,6,3,4,1,2,3,0,1,2}, y[16]={1,2,2,3,3,4,4,4,5,5,6,6,6,7,8,7}, ch1, ch2;
char names[16][30]={"VIT","Gandhinagar","Kangyanellore Road","Vivekanand Nagar","Samuel Nagar","Madina Nagar","CMC","Thendral Nagar","Balaji Nagar","Arcot Road","Mariadoss Nagar","Azad Road","RV Nagar","Kanaraj Nagar","Thorapadi-Bagayam Road","Mullai Road"};
node succ[8], q, *bt;
cout<<"-----------------------VELLORE MAPPING-----------------------"<<endl<<endl<<endl;
sloc:
cout<<"Choose your start location"<<endl;
cout<<"1.VIT"<<" "<<"2.Gandhinagar"<<" "<<"3.Kangyanellore Road"<<endl<<"4.Vivekanand Nagar"<<" "<<"5.Samuel Nagar"<<" "<<"6.Madina Nagar"<<endl<<"7.CMC"<<" "<<"8.Thendral Nagar"<<" "<<"9.Balaji Nagar"<<endl<<"10.Arcot Road"<<" "<<"11.Mariadoss Nagar"<<" "<<"12.Azad Road"<<endl<<"13.RV Nagar"<<" "<<"14.Kanaraj Nagar"<<" "<<"15.Thorapadi-Bagayam Road"<<endl<<"16.Mullai Road ";
cin>>ch1;
cout<<endl;
if(ch1>16 || ch1<=0)
{
cout<<"Wrong choice! Choose again."<<endl;
goto sloc;
}
else
{
sx=x[ch1-1];
sy=y[ch1-1];
}
floc:
cout<<"Choose your destination"<<endl;
cout<<"1.VIT"<<" "<<"2.Gandhinagar"<<" "<<"3.Kangyanellore Road"<<endl<<"4.Vivekanand Nagar"<<" "<<"5.Samuel Nagar"<<" "<<"6.Madina Nagar"<<endl<<"7.CMC"<<" "<<"8.Thendral Nagar"<<" "<<"9.Balaji Nagar"<<endl<<"10.Arcot Road"<<" "<<"11.Mariadoss Nagar"<<" "<<"12.Azad Road"<<endl<<"13.RV Nagar"<<" "<<"14.Kanaraj Nagar"<<" "<<"15.Thorapadi-Bagayam Road"<<endl<<"16.Mullai Road ";
cin>>ch2;
cout<<endl;
if(ch2>16 || ch2<=0 || ch2==ch1)
{
cout<<"Wrong choice! Choose again."<<endl;
goto floc;
}
else
{
fx=x[ch2-1];
fy=y[ch2-1];
}
cout<<"Keep pressing any key to find traffic situation at other places within Vellore"<<endl;
for(i=0;i<16;i++)
{
if(i!=ch1-1 && i!=ch2-1)
{
srand(time(NULL));
getch();
vmap[x[i]][y[i]]=rand()%2;
if(vmap[x[i]][y[i]]==1)
{
cout<<"There is traffic at "<<names[i]<<endl;
}
else
{
cout<<"There is no/less traffic at "<<names[i]<<endl;
}
}
}
cout<<endl;
//Getting starting and final, x and y values according to place name and allotting 1 to obstacles in path
succ[0].parent=NULL;
succ[0].x=sx;
succ[0].y=sy;
succ[0].f=0;
succ[0].h=0;
succ[0].g=0;
otop++;
olist[otop]=succ[0];
while(otop!=-1)
{
least=0;
cout<<"Open List:"<<endl;
for(i=0;i<=otop;i++)
{
cout<<olist[i].x<<" "<<olist[i].y<<" "<<olist[i].f<<endl;
}
for(i=1;i<=otop;i++)
{
if(olist[i].f<olist[least].f)
{
least=i;
}
}
q=olist[least];
cout<<q.x<<" "<<q.y<<" "<<q.f<<endl<<endl;
otop--;
for(i=least;i<=otop;i++)
{
olist[i]=olist[i+1];
}
tx=q.x;
ty=q.y;
k=-1;
ctop++;
clist[ctop]=q;
for(i=-1;i<=1;i++)
{
for(j=-1;j<=1;j++)
{
if(vmap[tx+i][ty+j]!=1 && tx+i>=0 && tx+i<10 && ty+j>=0 && ty+j<10)
{
k++;
succ[k].parent=&clist[ctop];
succ[k].x=tx+i;
succ[k].y=ty+j;
if(abs(i)==abs(j))
{
succ[k].g=q.g+14.14;
}
else
{
succ[k].g=q.g+10;
}
succ[k].h=(abs(fx-succ[k].x)+abs(fy-succ[k].y))*10;
succ[k].f=succ[k].g+succ[k].h;
if(succ[k].x==fx && succ[k].y==fy)
{
ctop++;
clist[ctop]=succ[k];
goto last;
}
}
}
}
for(i=0;i<=k;i++)
{
for(j=0;j<=otop;j++)
{
if(succ[i].x==olist[j].x && succ[i].y==olist[j].y && olist[j].f<=succ[i].f)
{
goto loopend;
}
}
for(j=0;j<=ctop;j++)
{
if(succ[i].x==clist[j].x && succ[i].y==clist[j].y && clist[j].f<=succ[i].f)
{
goto loopend;
}
else if(succ[i].x==clist[j].x && succ[i].y==clist[j].y && clist[j].f>succ[i].f)
{
ctop--;
for(l=j;l<=ctop;l++)
{
clist[l]=clist[l+1];
}
}
}
otop++;
olist[otop]=succ[i];
loopend:;
}
}
last:
if(clist[ctop].x!=fx || clist[ctop].y!=fy)
{
cout<<"Couldn't find a path!";
}
else
{
cout<<"Path Found!"<<endl;
bt=&clist[ctop];
while(bt!=NULL)
{
cout<<bt->x<<" , "<<bt->y<<endl;
bt=bt->parent;
}
}
}