Thanks leppy, but I'm still confused. Here is what I have done so far:
- By default initialized distance to some random number, and initialized the "visited[i][j]" to false.
Accepted input for grid from the user (well, the problem clearly does not have a whitespace between pixels, so string parsing will be required, but lets worry about that later).
3.Whichever grid pixels were "1", I pushed them into queue s.Now how can I modify my BFS for distance?
[code]
int main()
{
int t;
cin>>t;
while(t--)
{
int n, m;
for(int i=1; i <= n; i++)
for(int j=1; j <= m; j++)
{
distance[i][j]=99999; visited[i][j]=false;
}
for(int i=1; i <= n; i++)
for(int j=1; j <= m; j++)
{
int temp;
cin>>temp;
grid[i][j]=temp;
if(temp==1)
s.push(node(i, j));
dist[i][j]=0;
}
TraverseGrid(1,1);
}[/code]
BFS
[code]
int TraverseGrid(int x, int y)
{
while(s.empty()==false)
{
node top=s.front();
s.pop();
if(grid[x][y]==1)
continue;
if(top.x < 0 || top.x>=n) continue;
if (top.y < 0 || top.y >=m) continue;[/code]