Snake and ladders

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

template
class graph{
int v;
map<T,list>m;

public:

graph(int v){
this->v=v;

}
void addedge(int x,int y){
m[x].push_back(y);
// m[y].push_back(x);

}

int bfsshortest(T src,T dest){

 map<T,int>dist;
 bool visited[v];
 for(auto p:m){
	 dist[p.first]=-1;
    visited[p.first]=false;
 }

 queue<T>q;
 q.push(src);

dist[src]=0;
visited[src]=true;
while(!q.empty()){

   T node=q.front();

   q.pop();
   visited[node]=true;
   for(auto p:m[node]){
	   if(!visited[p]){
         q.push(p);
		   dist[p]=dist[node]+1;
		   visited[p]=true;
	   }
    
   }
   
 }
return dist[dest];

}
};
int main() {

int t;
cin>>t;

while(t--){
	int n,l,s;
	cin>>n>>l>>s;
	int board[n]={0};
	graph<int> g(n);
	int p=l+s;
	while(p--){
		int x,y;
		cin>>x>>y;
		board[x]=y-x;
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=6;j++){
			int k=i+j;
			 k=k+board[k];
			g.addedge(i,k);
		}
	}
	cout<<g.bfsshortest(1,n)<<endl;
}
return 0;

}

segmentation fault occurs why

hi @kumarakash121005, here’s the upadated and commented code https://ide.codingblocks.com/s/661258

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

edges are from 1 to n
so why loop starts from 0

image
this loop is not understandable to me

@kumarakash121005, the loop here is adding edge if the nodes are less than n*n as the cells can be max upto n*n.
board[i] is having either a jump or a ditch as we added in the above loop now we need to consider those also say board[i] contains 2 and dice comes 1 then edge will be of length 3 as we can go three stops from there. similarly for ditches also