first: this post is bs, i have no real idea what i am talking about. i cant even spell properly.
iamatiger is right about identifing which acorns can be reached at all, but i note that as no pig can land on the same cell twice, and they can only move to ajacent cells, an acorn only needs to be boxed on three sides to make it unreachable, two if it is on a side, and one if it is in a corrner.
the next step depends on the power of your computer (and on M)
if you can:
identify every possible path, and every possible set of three paths, and eliminate all sets that do not include all the acorns. then take the set with the shortest sum of lengths of paths.
if you want to save cpu:
preprogram in everything easy. the solutions for every possible set up with m<4 should be figurable.
then, if 3<M<8:
*find all the possible paths, and take the five that catch the most acorns, then listh the 125 possible sets of three, and check that at least 30 catch all acorns.
if yes: of those 30+, take the shortest.
if no: repeat from the astrix, execpt take one more than last time (instead of "...the five that...", "...the six that..." )
if M>8, then divide the feild into sections with horrisontal or vertical lines, each thourgh the entire feild, between cells, use (M-9) more than the fewest such lines nessesary to place each acorn in it's own section.
treat each section as a cell and repeat from the top.
then, within each section, solve that section as if it were a compleat feild, changing the rules for this step only in that the feild may not be square, and all that matters is that the pig gets the acorn, it starts on the correct side, and it ends on the correct side. if two pigs cross the same section, then figure both and take the shorter.
this is not garenteed to give you the best answer, but it should give you a good answer every time.
it occored to me that you could shorten your path/sets even more if you did one pig, eliminated the acorns he picked up, then did the next pig, except that i dont know how to determin how many acorns the first pig should be required to get, and that is importnat.
anyway, my attention span is running out, i have to bike home, and it's getting colder out by the minute.
all these numbers are arbitrary, i choose them because they seemed about right.