Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class Program
- {
- public static int width;
- public static int length;
- public static Queue<Node> queue = new Queue<Node>();
- public static void Main(string[] args)
- {
- string line = Console.ReadLine();
- width = (int)Char.GetNumericValue(line[0]);
- length = (int)Char.GetNumericValue(line[2]);
- var grid = new Node[width, length];
- int atLine = 0;
- while ((line = Console.ReadLine()) != null)
- {
- var currentLine = line.ToCharArray().Select(x => {
- var num = (int)Char.GetNumericValue(x);
- if (num < 0 || num > 9) return 0;
- else return num;
- }).ToArray();
- for (int j = 0; j < length; j++)
- {
- if (j >= currentLine.Length) grid[atLine, j] = new Node(0);
- else grid[atLine, j] = new Node(currentLine[j]);
- }
- if (++atLine == width) break;
- }
- initGraph(grid);
- /*Console.WriteLine();
- PrintArray(grid);
- Console.WriteLine();
- PrintNodes(grid);*/
- BFS(grid[0, 0], grid[width - 1, length - 1]);
- //PrintVisited(grid);
- }
- private static void BFS(Node start, Node goal)
- {
- start.level = 0;
- start.visited = true;
- queue.Enqueue(start);
- while (queue.Count > 0)
- {
- Node node = queue.Dequeue();
- foreach (var n in node.nodes)
- {
- if (!n.visited)
- {
- n.level = node.level + 1;
- n.visited = true;
- queue.Enqueue(n);
- if (n == goal)
- {
- Console.WriteLine(n.level);
- return;
- }
- }
- }
- }
- Console.WriteLine(-1);
- }
- public static void initGraph(Node[,] nodes)
- {
- for (int i = 0; i < nodes.GetLength(0); i++)
- {
- for (int j = 0; j < nodes.GetLength(1); j++)
- {
- var currentNode = nodes[i, j];
- int mvAmount = currentNode.mvAmount;
- if (mvAmount == 0) continue;
- int right = j + mvAmount;
- int left = j - mvAmount;
- int down = i + mvAmount;
- int up = i - mvAmount;
- if (right < length) currentNode.AddNode(nodes[i, right]);
- if (left >= 0) currentNode.AddNode(nodes[i, left]);
- if (down < width) currentNode.AddNode(nodes[down, j]);
- if (up >= 0) currentNode.AddNode(nodes[up, j]);
- }
- }
- }
- public static void PrintArray(Node[,] array)
- {
- int i = 0;
- foreach (var item in array)
- {
- Console.Write(item.mvAmount + " ");
- if (++i % length == 0) Console.WriteLine();
- }
- }
- public static void PrintNodes(Node[,] nodes)
- {
- int i = 0;
- foreach (var n in nodes)
- {
- Console.Write(n.nodes.Count + " ");
- if (++i % length == 0) Console.WriteLine();
- }
- }
- public static void PrintVisited(Node[,] nodes)
- {
- int i = 0;
- foreach (var n in nodes)
- {
- Console.Write(n.visited + " ");
- if (++i % length == 0) Console.WriteLine();
- }
- }
- public class Node
- {
- public int level { get; set; }
- public bool visited { get; set; }
- public int mvAmount { get; set; }
- public List<Node> nodes = new List<Node>();
- public Node(int mvAmount)
- {
- this.mvAmount = mvAmount;
- visited = false;
- }
- public void AddNode(Node n)
- {
- nodes.Add(n);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement