Jessica Posted August 13, 2009 Share Posted August 13, 2009 I have a 5x5 grid, stored in a multidimensional array. I have some objects in various spots on the grid. Imagine like a chessboard, each spot in the matrix is a square. I want to find how many "moves" basically between two spots. What I have now is the row and column numbers for each object, and I can figure out how many straight moves (up, down, left, right) it would be. But I want to account for diagonal. So if an object is in 3,3 and another in 5,5 that's only 2 moves. But I can't seem to figure out how to account for this in the code. If we have 3,3 and 4,5 - that's down one, over two, so I can see that's 3 moves. Or, 2 moves if we can use diagonal. Having trouble when to subtract moves for diagonal moves. Send me in the right direction? This is more a theory problem than actual code, but if you need code I can post something. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 13, 2009 Author Share Posted August 13, 2009 I think I have it...working on a while statement Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 13, 2009 Share Posted August 13, 2009 Was just going to suggest that. On each iterration of the while loop move 1 step vertically (if there is space) and one step horizontally (again, if there is space). But, consider each iteration of the loop as one move. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 13, 2009 Author Share Posted August 13, 2009 Actually I did it differently than I thought a minute ago. This looks like it works as far as I can test it now. Anyone see a problem? //Distance between start and end in rows and columns $rowDist = abs($row-$dr); $colDist = abs($col-$dc); //the diagonal moves needed is the smallest difference of these two $diagNeeded = min($rowDist,$colDist); $distance = $rowDist+$colDist-$diagNeeded; It works perfectly for all the spots I have in my matrix, I'll post again if I find it doesn't work later. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 13, 2009 Share Posted August 13, 2009 Actually I did it differently than I thought a minute ago. This looks like it works as far as I can test it now. Anyone see a problem? //Distance between start and end in rows and columns $rowDist = abs($row-$dr); $colDist = abs($col-$dc); //the diagonal moves needed is the smallest difference of these two $diagNeeded = min($rowDist,$colDist); $distance = $rowDist+$colDist-$diagNeeded; Yeah, that was different than I thought too, but you know, I think the problem is even simpler than that. Just find the max horizontal or vertical distance! The shorter distance will be completed first, but you still need to complete the longer distance. It wasn't until I saw your code above that it dawned on me. //Total moves needed between start and end in rows and columns $distance = max(abs($row-$dr), abs($col-$dc)); By the way, why so long since your last post? Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 13, 2009 Author Share Posted August 13, 2009 I'm a dog trainer now. Last night I started working on my first programming project since November. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.