Here is the question https://leetcode.com/problems/flood-fill/
please someone tell me what is wrong in my code:
class Solution {
public:
bool isvalid(int x,int y,int s)
{
if(x<0 || y<0 || x>=s || y>=s)
{
return false;
}
//cout<<"cor"<<x<<" "<<y<<endl;
return true;
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int color = image[sr][sc];
queue<pair<int,int>>q;
q.push(make_pair(sr,sc));
image[sr][sc] = newColor;
int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
while(!q.empty())
{
pair<int,int>p = q.front();
int x = p.first;
int y = p.second;
q.pop();
for(int i=0;i<4;i++)
{
int xn = x+dir[i][0];
int yn = y+dir[i][1];
if(isvalid(xn,yn,image.size()) && image[xn][yn] == color)
{
// cout<<"cor"<<xn<<" "<<yn<<endl;
image[xn][yn] = newColor;
q.push(make_pair(xn,yn));
}
}
}
return image;
}};
It is giving wrong answer on :
[[0,0,0],[0,0,0]]
0
0
2
Please help..