在 c# 中使用 bfs 的国王的最短路径

问题描述 投票:0回答:0

我正在尝试用 C# 编写一个程序来查找 8x8 棋盘上的最短路径,该程序应打印最短路径的正方形坐标。输入应如下所示:

第一行是障碍物个数,第二行是障碍物坐标,第三行是起点坐标,第四行是终点坐标

输入示例:

2
2 1 1 2
1 1
3 3

输出应该是:

1 1
2 2
3 3

我的问题是我不知道如何在同一条线上输入多个障碍物,我现在的代码只需要一条线上的一对坐标。

这是我现在的代码:

using System;
using System.Collections.Generic;

namespace kralnasachovnici2
{
    public class Chessboard
    {
        public List<(int, int)> FindShortestPath(int numberObstacles, List<(int, int)> coordinatesObstacles, (int, int) startCoordinates, (int, int) endCoordinates)
        {
            var blocked = new HashSet<(int, int)>(coordinatesObstacles);

            if (blocked.Contains(endCoordinates))
            {
                return null;
            }

            var queue = new Queue<((int, int) square, int distance)>();
            queue.Enqueue((startCoordinates, 0));

            var visited = new HashSet<(int, int)>();
            var parent = new Dictionary<(int, int), (int, int)>();

            var dx = new int[] { 1, 1, 0, -1, -1, -1, 0, 1 } ;  
            var dy = new int[] { 1, 1, 0, -1, -1, -1, 0, 1 } ;

            while (queue.Count > 0)
            {
                var (square, distance) = queue.Dequeue();
 
                if (square == endCoordinates) 
                {
                    var path = new List<(int, int)>();
                    var current = endCoordinates;

                    while (current != startCoordinates) 
                    {
                        path.Add(current);
                        current = parent[current];
                    }
                    path.Add(startCoordinates);
                    path.Reverse();
                    return path;
                }
                visited.Add(square);

                for (var i = 0; i < 8; i++)
                {
                    var x = square.Item1 + dx[i];
                    var y = square.Item2 + dy[i];
                    var neighbor = (x, y);

                    if (x >= 0 && x < 8 && y >= 0 && y < 8 && !blocked.Contains(neighbor) && !visited.Contains(neighbor))

                        {
                            queue.Enqueue((neighbor, distance + 1));
                            parent[neighbor] = square;
                    }
                }
            }
            return null; 
        }

        private static (int, int) ParseCoordinates(string input)
        {
            var parts = input.Split(' ');
            var x = int.Parse(parts[0]) - 1;
             var y = int.Parse(parts[1]) - 1;
            return (x, y);
        }

            public static void Main(string[] args)
            {
                var numberObstacles = int.Parse(Console.ReadLine());
                var coordinatesObstacles = new List<(int, int)>();
               
                for (var i = 1; i <= numberObstacles; i++)
                {
                    var obstacles = ParseCoordinates(Console.ReadLine());
                    coordinatesObstacles.Add(obstacles);
                }

                var startCoordinates = ParseCoordinates(Console.ReadLine());
                var endCoordinates = ParseCoordinates(Console.ReadLine());

                var board = new Chessboard();

                var shortestPath = board.FindShortestPath(numberObstacles, coordinatesObstacles, startCoordinates, endCoordinates);
                if (shortestPath != null)
                {
                    foreach (var square in shortestPath)
                    {
                        var row = square.Item1 + 1;
                        var col = square.Item2 + 1;
                        Console.WriteLine($"{row} {col}");
                    }
                }
                else 
                {
                    Console.WriteLine("-1");
                }
            }
    }
}

我是编码初学者,对于如何做到这一点,我将不胜感激。

c# breadth-first-search
© www.soinside.com 2019 - 2024. All rights reserved.