# Too Many Loops young man!

This is a discussion on Too Many Loops young man! within the C Programming forums, part of the General Programming Boards category; In a pathfinding routine i am working on i would like to chart the estimated distance(in movement cost) from each ...

1. ## Too Many Loops young man!

In a pathfinding routine i am working on i would like to chart the estimated distance(in movement cost) from each square on my search area to the given target node. I am doing this before doing the actual search to initialise the search area ahead of time.

* horizontal and vertical steps only
* Each step costs 10movement points.
* all squares are valid steps

The target can be anywhere but for now i will say its in the centre of a 20 x 20 square area.

My current solution to this problem uses four (while) loops
each loop works outwards from the target node to a corner of the search area incrementing the cost by 10 on each move and storing in Hscore[Down][Across]

1st loop > back and up to top left
2nd > forward and up to top right
3rd > forward and down to bottom right
4th > back and down to bottom left

I am sure that i can just use one loop to achieve this! using a little calculation in the loop i would like to just sweep the search area from top left to bottom right adding the value as i go using a sum of some kind,
Ihad a quick go at a couple of versions but think i was getting my precedence in wrong order becasue it was producing all kinds of minus numbers.

any ideas??

2. In your 20 x 20 grid, every row that a sqr is on = 10 * abs(target's row - sqr's row) + 10 * abs(target's column - sqr's column), imo.

One while or for loop, should do fine.

3. Whee! Cheers, that sounds like just the ticket.... here let me try.... my productivity in work is about to drop a little methinks..

4. W-H-A-T Productivity?? j/k

5. aww noo man! that kinda worked but not the way i mean,i think i need to tweak this a bit, i will post in bit with desired output...

6. Dude! Been 40 minutes. Are you now in the Geritol Time Zone, or what?

7. haha, just bin tooooo busy for play! doing it now tho

8. ok so for example, this is how its supposed to look if i printed out output of array contents,

i have just compiled this in work but the setup is not same as at home and am wasting time messing about getting display right, so am just going to type example for array 5 x 5 grid

40 30 20 30 40
30 20 10 20 30
20 10 0 10 20
30 20 10 20 30
40 30 20 30 40

as you can see the target square in red has a value of 0, and all the other surrounding squares increase the movement cost outwards in all directions as the distance increases, (bearing in mind the target square could be placed anywhere)

9. Originally Posted by rogster001
ok so for example, this is how its supposed to look if i printed out output of array contents,

i have just compiled this in work but the setup is not same as at home and am wasting time messing about getting display right, so am just going to type example for array 5 x 5 grid

40 30 20 30 40
30 20 10 20 30
20 10 0 10 20
30 20 10 20 30
40 30 20 30 40

as you can see the target square in red has a value of 0, and all the other surrounding squares increase the movement cost outwards in all directions as the distance increases, (bearing in mind the target square could be placed anywhere)
In your 20 x 20 grid, every row that a sqr is on = 10 * abs(target's row - sqr's row) + 10 * abs(target's column - sqr's column), imo.
Yep, perfect!

e.g., upper left corner is 0, 0, let's say. Target is 2,2.

Upper left sqr = 10 * abs(2 - 0) + 10 * abs(2 - 0)
sqr = 10 * 2 + 10 * 2
sqr = 20 + 20
sqr = 40

If your array numbering begins with 1, the numbers change, but the value of the same sqr is still going to be 40.

10. haha, super, will give that a go then, thanks for having a look, this pesky work is getting in the way of me doing it right now!

11. haha!!!! Man that is an absolute peach, works a treat! am chuffed, i love neat fixes like that! that will be goin straight in a function, an i can then see the wood for the trees and start on the problem of 'parent' squares...erm...!

cheers!