Snakes and Ladders

Hello,
I coded the problem, snakes and ladders, and the first time I run it, it gives me the correct output, the second time i run it, it shows me segmentation fault, core dumped.
I cannot find an error in program. If i print something else, it shws me correct output.
Thank you, please see the code I have linked below for the Snakes and Ladder Board problem.

https://pastebin.com/WY3b33Wg

you have declared the size of board 100 and trying to access 100+6 th element of the board.

hello saurabh, thank you for the help, after making changes in my code it gives me run time error for the coding blocks IDE, but it works on my machine??
what do you think could be the reason.
@Saurabh-Kumar-1331476656958199

	Author - Arten
*/

#include <bits/stdc++.h>

using namespace std;

#define endl "\n"
#define ll long long
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int main(){
	IOS;
	int t;
	cin >> t;
	while(t--){
		int board[100] = {};
		int n, m, from, to;
		cin >> n;
		for(int i = 0; i < n; i++){
			cin >> from >> to;
			board[from] = to - from;
		}
		cin >> m;
		for(int i = 0; i < m; i++){
			cin >> from >> to ;
			board[from] = to - from;
		}
		vector <int> adjList[101];

		for(int i = 1; i < 100; i++){
			if(i + 1 > 100)
				continue;
			if(i + 1 + board[i + 1] <= 100)
				adjList[i].push_back(i + 1 + board[i + 1]);
			if(i + 2 + board[i + 2] <= 100)
				adjList[i].push_back(i + 2 + board[i + 2]);
			if(i + 3 + board[i + 3] <= 100)
				adjList[i].push_back(i + 3 + board[i + 3]);
			if(i + 4 + board[i + 4] <= 100)
				adjList[i].push_back(i + 4 + board[i + 4]);
			if(i + 5 + board[i + 5] <= 100)
				adjList[i].push_back(i + 5 + board[i + 5]);
			if(i + 6 + board[i + 6] <= 100)
				adjList[i].push_back(i + 6 + board[i + 6]);
		}
		/*
		for(int i = 1; i <= 100; i++){
			cout << i << "->";
			for(auto j: adjList[i]){
				cout <<j << ",";
			}
			cout << endl;
		}*/
		queue<int> q;
		vector<int> dist(101, INT_MAX);

		q.push(1);
		dist[1] = 0;

		while(!q.empty()){
			int node = q.front();
			q.pop();
			for(auto i: adjList[node]){
				if(dist[i] == INT_MAX){
					q.push(i);
					dist[i] = dist[node] + 1;
				}
			}
		}
		cout << dist[100] << endl;
	}

	return 0;
}

increase the size of array from 100 to 110 you wont get runtime error.

The issue was resolved by Saurabh Kumar. Really helpful guy, took out time for me and helped me personally.

1 Like

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.