
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to find the minimum number of steps needed by knight to reach the destination using C#?
We have to make the knight cover all the cells of the board and it can move to a cell only once.
There can be two ways of finishing the knight move - the first in which the knight is one knight's move away from the cell from where it began, so it can go to the position from where it started and form a loop, this is called closed tour, the second in which the knight finishes anywhere else, this is called open tour. A move is valid if it is inside the chessboard and if the cell is not already occupied. We will make the value of all the unoccupied cells equal to -1.
Example
using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace ConsoleApplication{ public class KnightWalkProblem{ public class cell{ public int x, y; public int dis; public cell(int x, int y, int dis){ this.x = x; this.y = y; this.dis = dis; } } static bool isInside(int x, int y, int N){ if (x >= 1 && x <= N && y >= 1 && y <= N) return true; return false; } public int minStepToReachTarget(int[] knightPos, int[] targetPos, int N){ int[] dx = { -2, -1, 1, 2, -2, -1, 1, 2 }; int[] dy = { -1, -2, -2, -1, 1, 2, 2, 1 }; Queue<cell> q = new Queue<cell>(); q.Enqueue(new cell(knightPos[0], knightPos[1], 0)); cell t; int x, y; bool[,] visit = new bool[N + 1, N + 1]; for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) visit[i, j] = false; visit[knightPos[0], knightPos[1]] = true; while (q.Count != 0){ t = q.Peek(); q.Dequeue(); if (t.x == targetPos[0] && t.y == targetPos[1]) return t.dis; for (int i = 0; i < 8; i++){ x = t.x + dx[i]; y = t.y + dy[i]; if (isInside(x, y, N) && !visit[x, y]){ visit[x, y] = true; q.Enqueue(new cell(x, y, t.dis + 1)); } } } return int.MaxValue; } } class Program{ static void Main(string[] args){ KnightWalkProblem kn = new KnightWalkProblem(); int N = 30; int[] knightPos = { 1, 1 }; int[] targetPos = { 30, 30 }; Console.WriteLine( kn.minStepToReachTarget( knightPos, targetPos, N)); } } }
Output
20
- Related Articles
- Program to find number of optimal steps needed to reach destination by baby and giant steps in Python
- Program to find minimum steps to reach target position by a chess knight in Python
- Find the minimum cost to reach destination using a train
- C++ program to find out the minimum amount of time needed to reach from source to destination station by train
- Find the minimum number of steps to reach M from N in C++
- Minimum Initial Points to Reach Destination
- Program to find minimum number of heights to be increased to reach destination in Python
- Program to find number of minimum steps to reach last index in Python
- C++ program to find minimum number of steps needed to move from start to end
- C++ program to find minimum number of punches are needed to make way to reach target
- Find minimum steps required to reach the end of a matrix in C++
- How to find the minimum number of jumps required to reach the end of the array using C#?
- Program to find minimum number of vertices to reach all nodes using Python
- C++ program to count number of operations needed to reach n by paying coins
- Program to find minimum number of steps required to catch the opponent in C++

Advertisements