Snack and ladder

getting segmentation fault

https://practice.geeksforgeeks.org/problems/snake-and-ladder-problem4816/1

// { Driver Code Starts
// Initial Template for C++

#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
// User function Template for C++

class Solution{
public:

int bfs(int x,int y,vectoradj[])
{
int dist[30]={INT_MAX};

dist[0]=0;

queue<int>q;
q.push(x);

while(!q.empty())
{
    int temp=q.front();
    q.pop();
    
    
    
    for(auto it: adj[temp])
    {
        if(dist[it]==INT_MAX)
        {
            q.push(it);
            dist[it]= dist[temp] + 1;
            
            
        }
        
    }
    
     
}




return dist[y];

}

void addEdge(vectoradj[],int x,int y)
{
adj[x].push_back(y);

}

int minThrow(int N, int arr[])
{

  int vec[30]={0};
  
  for(int i=0;i<N-1;i+=2)  
    {
        vec[arr[i]]=arr[i+1] - arr[i];

 
        
    }
    
    
    vector<int >adj[8];        
    
    
    
    for(int i=0;i<30;i++)
    {
        
        for(int dice=1;dice<=6;dice++)
        {
            
            int j=i+dice;
            j+=vec[j];
            
            
            
            if(j<=30)
            addEdge(adj,i,j);
        }
    }
    
    
    
  return  bfs(0,30,adj);
    
    
}

};

// { Driver Code Starts.

int main(){
int t;
cin>>t;
while(t–){
int N;
cin>>N;
int arr[2N];
for(int i = 0;i < 2
N;i++)
cin>>arr[i];

    Solution ob;
    cout<<ob.minThrow(N, arr)<<"\n";
}
return 0;

} // } Driver Code Ends

Hey @deepaksharma42045 share your using ide.codingblocks.com

how to do it … I am a newbie here

Go to ide.codingblocks.com paste your code there. Save it. A special url will be generated in your search bar. Send that url to me.

pls do reply, I am waiting for this solution to this problem

hey @deepaksharma42045 sorry for late reply, was facing network issues. have seen your code and it’s implementation doesn’t seem right. Take this code for reference and apply it in your code.
Also code isn’t exactly same so you have to made modifications as per you.


if you have any issue in understanding this code. ask it here only. will reply asap.

i dont want your code
want to know mistakes in my code
whats wrong in my code

So your vec vector is as follows for custom input

vec[3] is equal to 19
vec[5] is equal to 3
vec[11] is equal to 15
vec[20] is equal to 9

send size of 2*N in line 67
also your
dist[x]=0;//update
segmentation fault might occur cause of accessing of accessing wrong indexes.
int dist[30]={INT_MAX};
this initializes only dist[0[ equals to int max
there is issue with implementation but logic is correct. That’s why i have provided you with the code so that you can make your implementation right.

this time i am just getting only run time error

check this, your board is now like this->


Vec[27] is equal to: -26
Deal with such cases.
Use map<int, list < int > >l;
and make all nodes distance as INT_MAX by making map like this

map<int,int>p;
			for(auto i:l)
			{
				int node=i.first;
				p[node]=INT_MAX;
			}