Let's think together so you already know more or less how to come to some logic.
The greatest challenge for programmers is to find a logical and simple way to solve day-to-day problems.
Let's assume that 4 x 4 has been passed to the size of the array.
Let's also think that to find the way we will always have to test 4 positions. (up x down x left x right) to check if you have 0 for "walking".
But we have the points of the matrix that are not 4 positions.
So, our 4 x 4 array has 16 spaces and 12 of those will be tested 2 times because they are tipped.
What are the tip? [0,0] [0,1] [0,2] [0,3] [1,0] [2,0] [3,0] [3,1] [3,2] [3,3] [1.3] [2.3]
Seeing these fields, what do we conclude? Always one or another position is the first or last of the array, that is, 0 or 3.
So how can we validate if we can check the position?
I think your logic will be something like: (where x is vertical and y horizontal)
[x + 1, y] (low) [x-1, y] (top), x, y + 1 and [x, y-1]
Always checking whether the element in question x or y is going to be added or subtracted if it is larger than the largest size in case 3 or smaller than the smaller size 0. If it is one of 2 it means that that path does not exist .
Now the code is on your own ...
I think it's + - around. =]
Edit.
I will explain well this situation of many possible ways because no time.
Let's say you'll need 2 arrays and 1 flag. to solve this.
1 array is the path traveled (let's call it a)
2 array is the path options (let's call b)
and the flag says whether it has another previous path option.
Then I get to the path [2,2] which has 3 possible options.
You mark the array "a" the key [2.2] as the last valid path
And in array "b" which can be a multidimensional associative array, you have marked as:
key ('2,2') value = array (option 1, option 2 and option 3)
E flag = true.
Let's say you went for option 1 and walked 3 houses ...
so in your 'a' array you have 2.2 and + 3 paths. and its flag true.
You have reached a deadend, you verify flag = true? yea
You will see the last record of the array "b" that will be your reference of the last multiple choice path. Let's say the last record is the 2.2 key and the 3 options, you chose 1 and it did not work.
So you see your key that says the last valid position 2.2 so you delete the array 'a' all after 2.2 and delete option 1 from array 'b' also because it did not work.
Dai tries option 2 and follows the same logic.
There are a thousand ways to do this. That was the way I thought it now. I hope that right or at least inspires you to find the correct logic.
At.