Why is dest[g]=0 for any value for 1<=g<=10000000 before calling bfs() in the below code even after initialising the global array with -1. it should be showing dest[g]=-1 and not zero?
But why is it not so?
#include<bits/stdc++.h>
using namespace std;
int vis[1000001]={0};
int dist[1000001]={-1};
int f,s,g,u,d;
void bfs()
{
queue<int> q;
q.push(s);
vis[s]=1;
dist[s]=0;
while(!q.empty())
{
int v=q.front();
q.pop();
//cout<<v<<endl;
int nextup = v+u;
int nextdown = v-d;
if (nextup<=f && vis[nextup] == 0)
{
q.push(nextup);
vis[nextup]=1;
dist[nextup]=dist[v]+1;
//cout<<nextup<<" "<<dist[nextup]<<endl;
}
if (nextdown>=1 && vis[nextdown] == 0)
{
q.push(nextdown);
vis[nextdown]=1;
//cout<<v<<" "<<dist[v]<<endl;
dist[nextdown]=dist[v]+1;
//cout<<nextdown<<" "<<dist[nextdown]<<endl;
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>f>>s>>g>>u>>d;
//cout<<dist[g]<<endl;
bfs();
//cout<<dist[g]<<endl;
if (dist[g] == -1)
cout<<"use the stairs";
else
cout<<dist[g];
return 0;
}