This is supposed to be a sort of way to find the longest ski slope. Enter the input as a matrix, and it should find the longest path where each path has each cell less than the one before it. However, I am getting a segfault. Can anyone help? The problem code is in the Notes and Credits (c++ code)
#include <iostream> #include <vector> using namespace std; int dirsy[4] = {1, 0, -1, 0}; int dirsx[4] = {0, 1, 0, -1}; vector<vector<int>> maze = {}; int n, m; int length = 0; int longest = 0; bool vis[100][100] = {false}; bool valid(int x, int y) { return (x >= 0 && y >= 0 && x < n && y < m && !vis[x][y]); } void floodish(int x, int y) { if (!valid) {return;} length += 1; vis[x][y] = true; if (length > longest) { longest = length; } for (int i = 0; i < 4; i++) { if (maze[x + dirsx[i]][y + dirsy[i]] < maze[x][y]) { floodish(x + dirsx[i], y + dirsy[i]); } } vis[x][y] = false; length -= 1; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { vector<int> append = {}; int dummy; for (int c = 0; c < m; c++) { cin >> dummy; append.push_back(dummy); } maze.push_back(append); } for (int i = 0; i < n; i++) { for (int c = 0; c < m; c++) { length = 0; floodish(i, c); } } cout << longest; }