If found cycle then return false
if ( visited [ startx ] [ starty ] == true ) return false ; visited [ startx ] [ starty ] = true ; int x = grid [ startx ] [ starty ] . first ; int y = grid [ startx ] [ starty ] . second ;