public void TestMethod2() { string[] terrain = new string[] { "P...S" , "..#.." , "....." , "....." , "....D" }; BraveKnight braveKnight = new BraveKnight(terrain); Assert.IsTrue(braveKnight.Princess.Row == 0 && braveKnight.Princess.Column == 0); Assert.IsTrue(braveKnight.Start.Row == 0 && braveKnight.Start.Column == 4); Assert.IsTrue(braveKnight.Destination.Row == 4 && braveKnight.Destination.Column == 4); Assert.IsTrue(braveKnight.graph[0].Keys.Count == 1); Assert.IsTrue(braveKnight.graph[0].ContainsKey(11)); Assert.IsTrue(braveKnight.graph[4].Keys.Count == 1); Assert.IsTrue(braveKnight.graph[4].ContainsKey(13)); Assert.IsTrue(braveKnight.graph[12].Keys.Count == 8); Assert.IsTrue(braveKnight.graph[12].ContainsKey(9)); Assert.IsTrue(braveKnight.graph[12].ContainsKey(19)); }
public void TestMethod5() { string[] terrain = new string[] { "P...S" , "..#.." , ".#..." , "....." , "....D" }; BraveKnight braveKnight = new BraveKnight(terrain); Assert.IsFalse(braveKnight.MinDistance(0, 24).HasValue); }
public void TestMethod4() { string[] terrain = new string[] { "P...S" , "..#.." , "....." , "....." , "....D" }; BraveKnight braveKnight = new BraveKnight(terrain); Assert.IsTrue(braveKnight.MinDistance(0, 4) == 4); }
public void TestMethod8() { string[] terrain = new string[] { "P...S" , "....." , "....." , "....." , "..*.D" }; BraveKnight braveKnight = new BraveKnight(terrain); Assert.IsTrue(braveKnight.MinDistance(24, 0) == 3); }
public void TestMethod6() { string[] terrain = new string[] { "P...S" , "....." , "....." , "....." , "....D" }; BraveKnight braveKnight = new BraveKnight(terrain); braveKnight.ExistShortestRescue(out int distance); Assert.IsTrue(distance == 6); }
/// <summary> /// Explanation : /// the BraveKnight class is the Main Class . /// The Idea is to make a Directed Graph with all the squares . /// Each square is a vertex . There would be an edge (v1,v2) if the vertex v1 could go to vertex v2 in just one step. /// Each edge has the same value, then we could use a Breadth-first search and Shortest-Distance algorithm /// when we want to calculate the shortest-path in steps from a vertex v to w .(Maybe it doesn't exist) /// The solution is calculating first the shortest-path from Start Position -> to Princess Position (if Exists) /// then From Princess Position to Destination Position ( if exists) the firstDistance + secondDistance is the answer. /// In other case is IMPOSSIBLE. /// graph G = (V = vertexs = N*M ,E = edges = O(V) ) /// Each vertex has at most 8 edges => E = O(V) /// this algorithm is O(V+E) = O(V) = O (N*M) because E = O(V) and BFS = O(V+E) /// The result is O(C*N*M) /// </summary> /// <param name="args"></param> public static void Main(string[] args) { try { int C = int.Parse(Console.ReadLine()); for (int i = 1; i <= C; i++) { try { var line = Console.ReadLine(); string[] splitParts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); int N = int.Parse(splitParts[0]); int M = int.Parse(splitParts[1]); string[] terrain = new string[N]; for (int j = 0; j < N; j++) { line = Console.ReadLine(); terrain[j] = line; } BraveKnight braveKnight = new BraveKnight(terrain); string prefixString = "Case #" + i + ": "; if (braveKnight.ExistShortestRescue(out int result)) { Console.WriteLine(prefixString + result); } else { Console.WriteLine(prefixString + "IMPOSSIBLE"); } } catch (Exception exception) { Console.WriteLine("Case #" + i + ": "); Console.WriteLine(exception); } } } catch (Exception exception) { Console.WriteLine(exception); } }