for any tile, it can swap with the empty tileļ¼and each swap counts for max(|y1-y2|,|x1-x2|) moves, and if they are in the same row or same col, this move will count for 0. every time, if the empty tile is not in the right place(the tile the empty tile should be in goal position), then swap it with the number should be in this tile in the goal position(to make this number matched). If the empty tile is in goal position, then swap it with the farthest mismatched number, do until to the goal position. And heuristic is the sum of moves.