Traceback through a Matrix of Directions in R

我的梦境 提交于 2019-12-22 10:08:21

问题


I have a matrix like this:

http://i.imgur.com/3HiEBm4.png

You can load it like this:

matrix = structure(c("-", "-", "C", "G", "C", "A", "-", "0", "V", "V", 
"V", "V", "C", "H", "D", "V", "DV", "V", "A", "H", "H", "D", 
"DV", "D", "C", "H", "DH", "DH", "D", "V", "G", "H", "H", "D", 
"H", "D", "T", "H", "H", "H", "DH", "DH", "A", "H", "H", "H", 
"DH", "D", "T", "H", "H", "H", "DH", "H"), .Dim = c(6L, 9L))

Starting at the bottom-right corner, the goal is to follow the directions (D = move diagonally towards 0, H = move to the left, V = move to the above) so that all paths arrive at zero. As you can see, there are some cells with multiple directions (such as DH).

I'm trying to find ALL possible paths through such a matrix. I did it with recursion. But I'm having difficulty with storing the path(s) properly. It seems like when the function returns to an old cell to take the other direction, it appends the path to the wrong list.

Here's my code for the recursive function:

threading = function(matrix,i,j,list) { #Function wants the matrix to be threaded, number of rows and cols, and an empty list
  if (matrix[i,j] == 0) { #If the recursion has arrived at zero, stop and print out the path that arrived there
    print(list)
  }
  else { #If still elsewhere inside the matrix...
    for (move in strsplit(matrix[i,j],"")[[1]]) { #Iterate through each move in that cell
      if (move == "D") { #If a move is D...
        list = paste(list, "D", sep="") #Append that to the path
        threading(matrix,i-1,j-1,list) #Send the function to the diagonal cell
      }
      if (move == "V") { #If a move is V...
        list = paste(list, "V", sep="") #Append that to the path
        threading(matrix,i-1,j,list) #Send the function to the above cell
      }
      if (move == "H") { #If a move is H...
        list = paste(list, "H", sep="") #Append that to the path
        threading(matrix,i,j-1,list) #Send the function to the left cell
      }
    }
  }
}

So when I run it with the above matrix, it gives this as an output:

> threading(matrix, 6,9, emptylist)
[1] "HDDDDHH"
[1] "HDDDDHHD"
[1] "HDDHHDDD"

The kicker is that the second character of the second two paths are wrong but everything else is right. How do I avoid that? I can't figure out how to store the path properly without it going back to an old path. I think it's something to do with the ordering of the appending and the sending the function to the next cell but if I reverse them then the appending never happens...


回答1:


The problem is with:

list = paste(list, "*", sep="")

When you hit a cell with two choices, e.g. "VH", the for loop will go through two iterations: list is modified by the first iteration, then that modified value is passed to the second iteration. Instead, each iteration must use the original value. So you could replace with:

l = paste(list, "*", sep="")

and pass l instead of list to the threading call.

On a separate note, it is good practice to avoid naming your variables matrix or list because they are also the name of functions.



来源:https://stackoverflow.com/questions/26196728/traceback-through-a-matrix-of-directions-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!