Вывод кратчайшего пути в черепашке - C#
Формулировка задачи:
Значение кратчайшего пути нашел, а вот как вывести этот путь не знаю.
Можно ли вывести матрицу типа:
0000x
00xxx
00x00
00x00
xxx00 ?
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Turtle
- {
- class Program
- {
- static void Main()
- {
- int min;
- Console.WriteLine("Введите размерность поля");
- string x = Console.ReadLine();
- string y = Console.ReadLine();
- int[,] a = new int[Int32.Parse(x), Int32.Parse(y)];
- Console.WriteLine("Построчно заполните поле");
- for (int i = 0; i < a.GetLength(0); i++)
- {
- string s = Console.ReadLine();
- string[] s2 = s.Split(new Char[] { ' ' });
- for (int j = 0; j < a.GetLength(1); j++)
- a[i, j] = Int32.Parse(s2[j]);
- }
- for (int i = a.GetLength(0) - 1; i >= 0; i--)
- for (int j = 0; j < a.GetLength(1); j++)
- {
- if (i == a.GetLength(0) - 1 && j == 0)
- min = 0;
- else
- {
- if (i == a.GetLength(0) - 1 && j != 0)
- min = a[i, j - 1];
- else
- {
- if (i != a.GetLength(0) - 1 && j == 0)
- min = a[i + 1, j];
- else
- min = Math.Min(a[i + 1, j], a[i, j - 1]);
- }
- }
- a[i, j] += min;
- }
- Console.WriteLine(a[0, a.GetLength(1) - 1]);
- Console.ReadKey();
- }
- }
- }
Решение задачи: «Вывод кратчайшего пути в черепашке»
textual
Листинг программы
- int [ , , , , ] a = new int[5, 5, 5, 5, 5];
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д