this is the question and here is the link for reference https://www.pepcoding.com/resources/online-java-foundation/recursion-backtracking/flood-fill-official/ojquestion#
i used the following code and checked it many times i dont find any error in it please help me figure out whats wrong, i am coding in c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void floodfill(vector<vector<int>> maze, int sr, int sc, string psf, vector<vector<int>> visited)
{
if (sr < 0 || sc < 0 || sr == maze.size() || sc == maze[0].size() ||
maze[sr][sc] == 1 || visited[sr][sc] == 1)
return;
if (sr == maze.size() - 1 && sc == maze[0].size() - 1)
{
cout << psf << endl;
return;
}
visited[sr][sc] == 1;
floodfill(maze, sr - 1, sc, psf + "t", visited);
floodfill(maze, sr, sc - 1, psf + "l", visited);
floodfill(maze, sr + 1, sc, psf + "d", visited);
floodfill(maze, sr, sc + 1, psf + "r", visited);
visited[sr][sc] == 0;
}
int main()
{
int n, m;
cin >> n >> m;
vector<vector<int>> arr(n, vector<int>(m));
vector<vector<int>> visited(n, vector<int>(m));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> arr[i][j];
floodfill(arr, 0, 0, "", visited);
}
please someone help thnx..
You need to add references to your vectors. C and C++ are pass by value languages you need to explicitly tell C++ that you are passing by reference.
void floodfill(vector<vector<int>>& maze, int sr, int sc, string psf, vector<vector<int>>& visited)
{
if (sr < 0 || sc < 0 || sr == maze.size() || sc == maze[0].size() ||
maze[sr][sc] == 1 || visited[sr][sc] == 1)
return;
if (sr == maze.size() - 1 && sc == maze[0].size() - 1)
{
cout << psf << endl;
return;
}
visited[sr][sc] = 1;
floodfill(maze, sr - 1, sc, psf + "t", visited);
floodfill(maze, sr, sc - 1, psf + "l", visited);
floodfill(maze, sr + 1, sc, psf + "d", visited);
floodfill(maze, sr, sc + 1, psf + "r", visited);
visited[sr][sc] = 0;
}
Also I think you may want to do assignment here visited[sr][sc] == 1;