-1

So I have a a 2darray of colors. The colors are represented by numbers. I have made a dfs that currently only checks for the number 1, which is red. I don;t think I hav the code right. I have a get neighbors that does get the neighbors around the node it is at and adds them to a list. The boards is the array of ints and the visited is an array of true or false if the node has been visited. Here is my dfs:

private int dfs(int startRow, int startCol, int[][]boards, boolean[][] visitedd){
    int f;
    int t;
    visitedd[startRow][startCol] = true;
    for(; startRow < q; startRow++){
        for(; startCol < q; startCol++){
            if(boards[startRow][startCol] == 1 && visitedd[startRow][startCol] == false){
                g+=1;
                f = startRow;
                t = startCol;
                dfs(f, t, boards, visitedd);
            }
        }
    }
    return g;

I'm not sure how to use the get neighbors to properly traverse to the next red.

user2835532
  • 113
  • 1
  • 1
  • 10
  • 1
    Where is `q` initialized? Also, why exactly isn't it working? What is it actually doing? – Brian Mar 19 '14 at 02:27
  • q is the size of the board. The board in this case is 4, but it can change. I want this dfs to use the getNeighbors to find the next red and say if it makes a continous line to another point. like if there is a board that is [1231[1231][1111] it will follow all the ones and return if it continous and count them up. But I'm not sure how to do that. – user2835532 Mar 19 '14 at 02:33
  • Sorry, I'm not sure I understand completely – Brian Mar 19 '14 at 02:40
  • I have a list of neighbors, but i'm not sure how to use that list to traverse to the next valid neighbor. – user2835532 Mar 19 '14 at 02:50

1 Answers1

1

In DFS you mark child nodes explored as you LEAVE them. Check out this Pseudocode from Wikipedia:

procedure DFS(G,v):
  label v as discovered
  for all edges from v to w in G.adjacentEdges(v) do
    if vertex w is not labeled as discovered then
      recursively call DFS(G,w)

Notice you don't check the parent for being visited. You check the children.

Brian
  • 7,098
  • 15
  • 56
  • 73
  • Ok, that fixed it giving back true al the time. But it never moves past the first row in the array. – user2835532 Mar 19 '14 at 02:41
  • Its because you're not entering the if logic because you set the value to `true` Dont check if the parent is visited, check if the child is visited. – Brian Mar 19 '14 at 02:44
  • How would I go about doing that? I have a getNeighbors that creates a list of neighbors. But I'm not sure how to check those neighbors in the list. – user2835532 Mar 19 '14 at 02:48
  • @user2835532 get the neighbors and iterate through them checking your visited matrix to see if they are visited, if not visited, recursively call DFS on your neighbor node. – Brian Mar 19 '14 at 02:50
  • ok, so in my if statement I would call getneighbors. use a for each loop on it to check if they are the right type of node and that they have not been visited, and then call the dfs on that neighbor? – user2835532 Mar 19 '14 at 02:53
  • getNeighbors then iterate through that array with a for loop and use the if block within the for loop. If not visited call DFS on those neighbor indices – Brian Mar 19 '14 at 02:55
  • Ok, thank you. I have one more question. I t turns out that my getNeighbors doesn't add a 2d point into a list it only adds one. this is what my initialized list looks like: List neighbors = new ArrayList();. and here is the out put, 3,0,1,3. It needs to be [3,1],[0,1] and so on. How can i fix this? – user2835532 Mar 19 '14 at 03:34
  • @user2835532 You should create a `Node` object which holds its i and j index location in your 2d array and its value. It can also hold its neighbors which you can compute by checking whether its neighboring indices are within the bounds of your array. Then you can return these neighbors. Please upvote and accept my answer if it has helped you. – Brian Mar 19 '14 at 03:40