javapathshortest

How can i find the closest path in matrix that A to any B?


For example:

m_array = new int[6][6];
m_array[0] = new int[]{2, 0, 0, 0, 0, 0};
m_array[1] = new int[]{0, 2, 0, 0, 0, 2};
m_array[2] = new int[]{2, 0, 0, 1, 0, 0};
m_array[3] = new int[]{0, 0, 0, 0, 0, 0};
m_array[4] = new int[]{0, 2, 0, 0, 2, 0};
m_array[5] = new int[]{0, 0, 2, 0, 0, 0};

How can i find the the closest 2 to 1?

i want a function that it return an array path include points of array. For example i will give the "m_array" to my function and it will return to me the nearest 2 for 1, an array path like [2,3][3,4][4,4]


Solution

  • These are the things you leave us guessing:

    My current thinking is that there is a metric that can be defined for the path length between any two points, so the concepts "a 2 with the shortest path to 1" is equivalent to the concept "the 2 closest to the 1". The metric can be defined as the number of "rings" around the central 1 one must cross to get to a 2:

    0 0 0
    0 1 0
    0 0 2  --> the 2 is in the first ring
    
    0 0 0 0 0
    0 0 0 0 0
    0 0 1 0 0
    0 0 0 0 0
    0 2 0 0 0 --> the 2 is in the second ring.
    

    If all my assumptions are correct, then you need a function that gives all the members of the first ring, the second ring, and so on, and another function that will search a ring for a 2. Then, finally, you need an algorithm to draw a path to the 2. I hope you realize the path is not unique. A trivial algorithm will move diagonally until aligned (either horizontally or vertically) with the 2 and then continue non-diagonally to the target.