You're pretending that you can't look at the map when trying to determine if something is "5 squares away."
Lets say this is our map. * are the path, T is a Task Object.
.*......
.*......
.*..#...
.*..#T..
.*..#...
.*..#...
.*......
.*......
If we count diagonals as 1 space, then T is 5 away from the path (go around the wall on the top). So we want this to be included, but we don't want to run an A* check on it (and by definition, every other task on the map).
What can we do?
Well, we do this: find every square adjacent to the path, and every square adjacent to that, etc. until we have 5 deep and note every task that falls inside that area. Essentially we're doing a limited distance flood-fill.
1*12345.
1*12345.
1*12#45.
1*12#5..
1*12#5..
1*12#45.
1*12345.
1*12345.
Voila. And if the object is in another room not direclty accessible, then the item isn't categorized:
1*12345.
1*12345.
1*12####
1*12#T..
1*12#...
1*12####
1*12345.
1*12345.
Now you have a list of every task that is inside some boundary distance of the dwarf's desired path. Some tasks have a starting point and an ending point ("fetch quests" and for those you need to check the starting point and the ending point with the requirement that the start location is closer to the dwarf's starting location (and vice versa) which you can do through normal distance calculations (no need to do a path-find distance, as we already know that both points are within 5 of the existing path).