Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8116339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:47:52+00:00 2026-06-06T03:47:52+00:00

I am stuck solving a task requiring me to calculate the minimal number of

  • 0

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.

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

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

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 < dimension-1; step++) {
    for (int x = 0; x < dimension; x++) {
      for (int j = 0; j < dimension; j++) {
        if (chessBoard[x, j] == step) {
          for (int k = 0; k < 8; k++) {
            if (movementY[k] + x>= 0 && movementY[k] + x < dimension && movementX[k] + j >= 0 && movementX[k]+j < dimension) {
              if (chessBoard[movementY[k]+x,movementX[k]+j] == -1) {
                chessBoard[movementY[k]+x,movementX[k]+j] = step + 1;
              }
            }
          }
        }
      }
    }
  }
}

The input and output for a knight’s moves are as follows:

-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

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:47:54+00:00Added an answer on June 6, 2026 at 3:47 am

    You need two parameters to describe the n*m board.

    Instead of looping to the theoretical maximum number of steps, just loop until you have filled the board:

    public void AllPaths(int[,] chessBoard, int startX, int startY, int dimensionX, int dimensionY) {
      // All the moves a knight can make
      int[] movementY = { -2, -2, -1, -1,  1,  1,  2,  2 };
      int[] movementX = { -1,  1, -2,  2, -2,  2, -1,  1 };
      chessBoard[startX, startY] = 0;
      int cnt = dimensionX * dimensionY - 1:
      int step = 0;
    
      while (cnt > 0) {
        for (int x = 0; x < dimension; x++) {
          for (int y = 0; y < dimension; y++) {
            if (chessBoard[x, y] == step) {
              for (int k = 0; k < 8; k++) {
                int dx = movementX[k] + x, dy = movementY[k] + y;
                if (dx >= 0 && dx < dimensionX && dy >= 0 && dy < dimensionY) {
                  if (chessBoard[dx, dy] == -1) {
                    chessBoard[dx, dy] = step + 1;
                    cnt--;
                  }
                }
              }
            }
          }
        }
        step++;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am stuck while solving this question, What is the minimum number of 2
I'm totally stuck and have no idea how to go about solving this. Let's
I was solving last years GATE question paper where i am stuck with this
I'm stuck in a problem in which I need to calculate something like: ((500!)/(20!x20!x20!x20!...))
I was stuck in solving the following interview practice question: I have to write
I am stuck with solving my assignment. Here is what it states, Consider the
i had a problem and solving which required a functionality built in mootools .
I am stuck at solving Accelerated C++ exercise 8-5 and I don't want to
I'm stuck with the JavaScript code I am using for solving a problem which
I am kind of stuck on this and would appreciate help in solving this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.