Winston Gutkowski wrote:
George Junglin wrote:I understand the min function only takes two parameters but for understanding sake.
Right, and that's why I was saying that describing the problem (or in this case, the solution you've been given) is very important.
First: That min() call takes the cell and all its immediate neighbours. Why not make it a method? And if you do, what do you think:
(a) It should look like?
(b) It should be called?
Second: I suspect that 'a' stands for 'adjacent' and 'd' stands for 'diagonal', and that the algorithm is meant to reflect the number of moves that only involve 90-degree turns. So: document it; either at the class level or with a comment on each field.
Third: That algorithm is NOT run on obstacles (you didn't mention that; at least not directly). It's also different if the "centre" cell is on one of the edges of the grid.
Fourth: What about 'rows' and 'columns' instead of 'xmax' and 'ymax'?
Fifth: You could also make your program a lot easier to read by adding a descriptive constant or two, for example:
private static final int OBSTACLE = Integer.MAX_VALUE;
...
private final int FREE_CELL_START_VALUE = rows * columns; (Question: Why do you think this value is used?)
Sixth: Get out some paper and a pen and work out what happens when you run the algorithm once on every cell (assuming they're properly initialized).
Does that produce what you want?
If not, what do you think you might need to do?
How will you know when you DO have what you want?
You can't code a problem until you understand how it works. And when you DO write your code, making it easily readable for everybody is a major step to becoming a good programmer, rather than just "someone who writes Java code".
HIH
Winston
PS: I broke up that enormous line of yours. They make threads very hard to read. Please re-read the UseCodeTags page. Thoroughly.
Campbell Ritchie wrote:Welcome to the Ranch
Are you supposed to work out the algorithm for yourself from scratch? Or are you allowed to find it? It should be easy enough to find in an artificial intelligence book (e.g. Russell and Norvig), or on Wikipedia.