Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting the shortest path for chess pieces on n*m board
    primarykey
    data
    text
    <p>I am stuck solving a task requiring me to calculate the minimal number of steps required to go from point A to point B with different chess pieces on a n*m board that also has obstacles and also output the path taken.</p> <p>I was looking at the A* algorithm, but it requires me to get a good heuristic estimate which I have no idea how to get for a chess piece like a knight</p> <p>What I have been trying to do is first using Breadth-First Search to find the minimal number of steps required to get from point A to point B without obstacles and then use the A* algorithm</p> <pre><code>public void AllPaths(int[,] chessBoard, int startX, int startY, int dimension) { // All the moves a knight can make int[] movementX = new int[8]{-2, -2, -1, -1, 1, 1, 2, 2}; int[] movementY = new int[8]{-1, 1, -2, 2, -2, 2, -1, 1}; chessBoard[startX, startY] = 0; for (int step = 0; step &lt; dimension-1; step++) { for (int x = 0; x &lt; dimension; x++) { for (int j = 0; j &lt; dimension; j++) { if (chessBoard[x, j] == step) { for (int k = 0; k &lt; 8; k++) { if (movementY[k] + x&gt;= 0 &amp;&amp; movementY[k] + x &lt; dimension &amp;&amp; movementX[k] + j &gt;= 0 &amp;&amp; movementX[k]+j &lt; dimension) { if (chessBoard[movementY[k]+x,movementX[k]+j] == -1) { chessBoard[movementY[k]+x,movementX[k]+j] = step + 1; } } } } } } } } </code></pre> <p>The input and output for a knight's moves are as follows:</p> <pre><code>-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 - starting from the top left 0 3 2 3 2 3 4 5 3 4 1 2 3 4 3 4 2 1 4 3 2 3 4 5 3 2 3 2 3 4 3 4 2 3 2 3 4 3 4 5 3 4 3 4 3 4 5 4 4 3 4 3 4 5 4 5 5 4 5 4 5 4 5 6 </code></pre> <p>This works for n*n boards, but I need it to work for n*m boards as well. Am I going in the right direction or should I use something else completely? What do I have to change for it to work for n*m boards? Thanks for any suggestions you may have about solving the problem I have.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload