The ghosts use an A-Star algorithm to calculate the path to follow. Each cell is calculated to see if the ghost can go to next cell: Up, Down, Left and Right. The location the ghost wants to get to is marked with the number 1, then it checks to see if it can move up. it will then add a 2. Next it checks right and down and finally left. Each time adding a number if it can move in that direction.
Once the possible movement is mapped the current cell value is used to calculate the way back to the target. if the ghost is on 42, then it checks if there is a 41 near by. then 40 and so on, until it reaches one.
Each time the ghost changes to another cell or the target moves. the entire routine has to be updated.