Okay, here's some commentary for you.
I am not sure why you have
two city maps. (I think the additional one is messing up your clarity.) You only need one.
I am also unsure of your extra fields in the
Parcel:
10 11 12 13 14 15 16
|
struct Parcel
{
int goalY;
int goalX;
bool exchanged; //?
bool atDest; //?
};
|
You do not need to track whether or not a parcel has been exchanged.
You only need to care if it is at its final destination (the 'goal') or not. (And you do not need a flag to remember that.)
What
might be useful is directional tokens
1. As part of the algorithm, you will encounter the need to move parcels that are already in their correct row or column.
For your initial example:
0,1 1,0 0,2
→ ↓ ←
1,1 2,0 1,2
→ ↓ ←
2,2 2,1 0,0
→→ ↑↑ ←← |
That parcel in cell 2,2 has to move a total of four cells. It needs to move now if you are to obtain your the potential best result of four moves.
One way to start (and this is not the only one) is to make sure that each cell with arrows does move at least one cell.
0,1▶◀1,0 0,2 1,0 0,1 0,2
→ ↓ ← ↓
1,1▶◀2,0 1,2 --> 2,0 1,1 0,0
→ ↓ ← ▼ ↓ ↑ ←←
▲
2,2▶◀2,1 0,0 2,1 2,2 1,2
→→ ↑↑ ←← → → ↑ |
The next thing to notice is that we now have a clear loop -- almost.
Fortunately, we can borrow arrows from the last cell with arrows and carry them along to cells without arrows. (You can do this in any order, but here are the two choices.):
1,0 0,1 0,2 1,0 0,1 0,2
↓ [←] ↓ [←] [←]
2,0 1,1 0,0 2,0 1,1 0,0
↓ [↑] []← ↓ ↑[]
2,1 2,2 1,2 2,1 2,2 1,2
→ → ↑ → → ↑ |
Conveniently, we can now move cells along that complete loop.
(The resulting configuration requires only two simple transitions to solve.)
The purpose of the toy I made for you was so that you
2 could play with these patterns and see how to design your algorithm to choose to move cells.
1. The arrows can easily be calculated instead of stored as additional fields. Do whichever is more convenient; I doubt there is any computational difference either way.
2. I actually made it so I could play with it, but it was cool enough I decided to share. :O)
Hope this helps.