Okay just saw my previous code had
if(grid[x][y]==1)
continue;
but I've removed that
// if(top.x <= 0 || top.x>n) continue;
// if (top.y <= 0 || top.y >m) continue;
I tried using the print statements and it appears that these two statements are causing the trouble as it skips the rest of the statements. If i skip those two however, it results into an infinte loop but it does go through rest of the statements.
updated code:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
int result=0;
bool visited[210][210];
int grid[210][210];
int dist[210][210];
int n, m;
class node
{
public:
int x;
int y;
node() : x( 0 ), y( 0 ) {}
node( int pX, int pY ) : x( pX ), y( pY ) {}
/*node(int pX, int pY)
{
x=pX;
y=pY;
}*/
};
queue<node> s;
void TraverseGrid()
{
while(s.empty()!=true)
{
node top=s.front();
cout<<"top.x "<<top.x<<" top.y: "<<top.y<<" visited: "<<visited[top.x][top.y]<<endl;
s.pop();
cout<<"FLAG 1 PASSED"<<endl;
if(top.x <= 0 || top.x>n) continue;
cout<<"FLAG 2 PASSED"<<endl;
if (top.y <= 0 || top.y >m) continue;
cout<<"FLAG 3 PASSED"<<endl;
cout<<visited[top.x][top.y]<<endl;
cout<<"FLAG 4 PASSED"<<endl;
visited[top.x][top.y]=true;
if(visited[top.x][top.y]==true)cout<<"YES VISITED"<<endl;
cout<<visited[top.x][top.y]<<endl;
if(!visited[top.x+1][top.y])
{
dist[top.x+1][top.y]=1+dist[top.x][top.y];
s.push(node(top.x+1, top.y));
}
if(!visited[top.x][top.y+1])
{dist[top.x][top.y+1]=1+dist[top.x][top.y];
s.push(node(top.x, top.y+1)); }
if(!visited[top.x][top.y-1])
{dist[top.x][top.y-1]=1+dist[top.x][top.y];s.push(node(top.x, top.y-1)); }
if(!visited[top.x-1][top.y])
{ dist[top.x-1][top.y]=1+dist[top.x][top.y];s.push(node(top.x-1, top.y));}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n, m;
cin>>n>>m;
for(int i=1; i <= 200; i++)
for(int j=1; j <= 200; j++)
{dist[i][j]=9999; 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();
for(int i=1; i <= n; i++){
for(int j=1; j <= m; j++)
cout<<dist[i][j];
cout<<endl;
}
}
}