I can't think of a way to make it work 100% of the time but you can make the snakes choice more likely to be the best.
You can calculate which side of the snakes has more free squares. For example (see image below) with the snake 1. You can calculate that there is only 4 free squares on the right and much more on the left so it should probably go left.
An easy way to calculate the number of free squares would be with a recursive function. Pseudo code:
int num_free_square(positionX, positionY)
int num = 1;
if (arrayMap[positionX+1,positionY] == FREE)
num += num_free_square(positionX+1, positionY)
... same for each direction
(You would of course have to check if each square has already been counted and if you don't access space outside the array)
The previous solution is far from perfect. For example, on the following image the snake 1 would go right because there is only 1 square free on the left and 4 on the right. Although the next snake 3 move would have freed the path.
The solution could be to calculate the number of move the snake 1 would be able to do if the other snakes were changing direction randomly after meeting a wall. Before choosing a direction you calculate the deplacement of all the snakes during few moves.
This would require a lot of computation but you could stop at 10 moves for example and say : if the snake can go 10 moves on both way then I pick a random direction, else I go to the direction with the highest number of move.
Although the choices of the other snakes will probably not be what you planned so the snakes will still be able to block each other.