From 0c76bf44c3fadc242a76213a4ee9038f3832efd3 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Thu, 17 Feb 2022 15:31:43 +0200 Subject: [PATCH] feat: complete Lab1 --- Recursion/LD_24/App_Data/Rezultatai.txt | 12 ++ Recursion/LD_24/App_Data/U3.txt | 6 + Recursion/LD_24/Code/BestPizzeriaResult.cs | 21 ++++ Recursion/LD_24/Code/InOutUtils.cs | 99 +++++++++++++++ Recursion/LD_24/Code/Map.cs | 52 ++++++++ Recursion/LD_24/Code/MapTile.cs | 16 +++ Recursion/LD_24/Code/Point.cs | 35 ++++++ Recursion/LD_24/Code/TaskUtils.cs | 115 +++++++++++++++++ Recursion/LD_24/Forma1.aspx | 26 ++++ Recursion/LD_24/Forma1.aspx.cs | 109 ++++++++++++++++ Recursion/LD_24/Forma1.aspx.designer.cs | 80 ++++++++++++ Recursion/LD_24/LD_24.csproj | 140 +++++++++++++++++++++ Recursion/LD_24/Properties/AssemblyInfo.cs | 35 ++++++ Recursion/LD_24/Web.Debug.config | 30 +++++ Recursion/LD_24/Web.Release.config | 31 +++++ Recursion/LD_24/Web.config | 18 +++ Recursion/LD_24/packages.config | 4 + Recursion/Recursion.sln | 25 ++++ 18 files changed, 854 insertions(+) create mode 100644 Recursion/LD_24/App_Data/Rezultatai.txt create mode 100644 Recursion/LD_24/App_Data/U3.txt create mode 100644 Recursion/LD_24/Code/BestPizzeriaResult.cs create mode 100644 Recursion/LD_24/Code/InOutUtils.cs create mode 100644 Recursion/LD_24/Code/Map.cs create mode 100644 Recursion/LD_24/Code/MapTile.cs create mode 100644 Recursion/LD_24/Code/Point.cs create mode 100644 Recursion/LD_24/Code/TaskUtils.cs create mode 100644 Recursion/LD_24/Forma1.aspx create mode 100644 Recursion/LD_24/Forma1.aspx.cs create mode 100644 Recursion/LD_24/Forma1.aspx.designer.cs create mode 100644 Recursion/LD_24/LD_24.csproj create mode 100644 Recursion/LD_24/Properties/AssemblyInfo.cs create mode 100644 Recursion/LD_24/Web.Debug.config create mode 100644 Recursion/LD_24/Web.Release.config create mode 100644 Recursion/LD_24/Web.config create mode 100644 Recursion/LD_24/packages.config create mode 100644 Recursion/Recursion.sln diff --git a/Recursion/LD_24/App_Data/Rezultatai.txt b/Recursion/LD_24/App_Data/Rezultatai.txt new file mode 100644 index 0000000..99fa4d6 --- /dev/null +++ b/Recursion/LD_24/App_Data/Rezultatai.txt @@ -0,0 +1,12 @@ +D...P +XX... +D.S.P +XX... +D.S.. + +1 1 +1 3 +1 5 +Susitikimo vieta 3 3 +Picerija 5 3 +Nueita 32 diff --git a/Recursion/LD_24/App_Data/U3.txt b/Recursion/LD_24/App_Data/U3.txt new file mode 100644 index 0000000..cfce054 --- /dev/null +++ b/Recursion/LD_24/App_Data/U3.txt @@ -0,0 +1,6 @@ +5 5 +D...P +XX... +D.S.P +XX... +D.S.. \ No newline at end of file diff --git a/Recursion/LD_24/Code/BestPizzeriaResult.cs b/Recursion/LD_24/Code/BestPizzeriaResult.cs new file mode 100644 index 0000000..254c811 --- /dev/null +++ b/Recursion/LD_24/Code/BestPizzeriaResult.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace LD_24.Code +{ + public class BestPizzeriaResult + { + public Point Pizzeria { get; private set; } + public Point MeetingSpot { get; private set; } + public int Cost { get; private set; } + + public BestPizzeriaResult(Point pizzeria, Point meetingSpot, int cost) + { + Pizzeria = pizzeria; + MeetingSpot = meetingSpot; + Cost = cost; + } + } +} \ No newline at end of file diff --git a/Recursion/LD_24/Code/InOutUtils.cs b/Recursion/LD_24/Code/InOutUtils.cs new file mode 100644 index 0000000..9e4afcb --- /dev/null +++ b/Recursion/LD_24/Code/InOutUtils.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Web; + +namespace LD_24.Code +{ + public class InOutUtils + { + public static Map ReadMap(string filename) + { + string[] lines = File.ReadAllLines(filename); + string[] height_width = lines[0].Split(' '); + int height = int.Parse(height_width[0]); + int width = int.Parse(height_width[1]); + + Map map = new Map(width, height); + for (int i = 1; i < height+1; i++) + { + int x = 0; + foreach (char c in lines[i]) + { + MapTile tile; + if (c == '.') { + tile = MapTile.Empty; + } else if (c == 'X') { + tile = MapTile.Wall; + } else if (c == 'P') { + tile = MapTile.Pizzeria; + } else if (c == 'D') { + tile = MapTile.Friend; + } else if (c == 'S') { + tile = MapTile.MeetingSpot; + } else { + throw new Exception($"Invalid tile '{c}'"); + } + map.Set(x, i - 1, tile); + x++; + } + } + return map; + } + + public static void WriteMap(StreamWriter writer, Map map) + { + for (int y = 0; y < map.Height; y++) + { + for (int x = 0; x < map.Width; x++) + { + switch (map.Get(x, y)) + { + case MapTile.Empty: + writer.Write('.'); + break; + case MapTile.Pizzeria: + writer.Write('P'); + break; + case MapTile.Friend: + writer.Write('D'); + break; + case MapTile.MeetingSpot: + writer.Write('S'); + break; + case MapTile.Wall: + writer.Write('X'); + break; + default: + writer.Write('?'); + break; + } + } + writer.Write('\n'); + } + } + + public static void WriteBestPizzeriaResult(StreamWriter writer, BestPizzeriaResult result) + { + if (result == null) + { + writer.WriteLine("Neįmanoma"); + } + else + { + writer.WriteLine("Susitikimo vieta {0} {1}", result.MeetingSpot.X + 1, result.MeetingSpot.Y + 1); + writer.WriteLine("Picerija {0} {1}", result.Pizzeria.X + 1, result.Pizzeria.Y + 1); + writer.WriteLine("Nueita {0}", result.Cost); + } + } + + public static void WriteFriendPositions(StreamWriter writer, List friends) + { + foreach (var friend in friends) + { + writer.WriteLine("{0} {1}", friend.X + 1, friend.Y + 1); + } + } + } +} \ No newline at end of file diff --git a/Recursion/LD_24/Code/Map.cs b/Recursion/LD_24/Code/Map.cs new file mode 100644 index 0000000..4736468 --- /dev/null +++ b/Recursion/LD_24/Code/Map.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace LD_24.Code +{ + public class Map + { + MapTile[,] data; + public int Width { get; set; } + public int Height { get; set; } + + public Map(int width, int height) + { + data = new MapTile[width, height]; + Width = width; + Height = height; + } + + public void Set(int x, int y, MapTile tile) + { + data[x, y] = tile; + } + + public MapTile Get(int x, int y) + { + return data[x, y]; + } + + public bool IsInBounds(int x, int y) + { + return x >= 0 && x < Width && y >= 0 && y < Height; + } + + public List FindAll(MapTile tile) + { + List points = new List(); + for (int i = 0; i < Width; i++) + { + for (int j = 0; j < Height; j++) + { + if (data[i, j] == tile) + { + points.Add(new Point(i, j)); + } + } + } + return points; + } + } +} \ No newline at end of file diff --git a/Recursion/LD_24/Code/MapTile.cs b/Recursion/LD_24/Code/MapTile.cs new file mode 100644 index 0000000..1cb54bb --- /dev/null +++ b/Recursion/LD_24/Code/MapTile.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace LD_24.Code +{ + public enum MapTile + { + Empty, + Wall, + Friend, + Pizzeria, + MeetingSpot + } +} \ No newline at end of file diff --git a/Recursion/LD_24/Code/Point.cs b/Recursion/LD_24/Code/Point.cs new file mode 100644 index 0000000..d9e22f0 --- /dev/null +++ b/Recursion/LD_24/Code/Point.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace LD_24.Code +{ + public class Point + { + public int X { get; set; } + public int Y { get; set; } + + public Point(int x, int y) + { + X = x; + Y = y; + } + + public override bool Equals(object obj) + { + return obj is Point point && + X == point.X && + Y == point.Y; + } + + public override int GetHashCode() + { + int hashCode = 1861411795; + hashCode = hashCode * -1521134295 + X.GetHashCode(); + hashCode = hashCode * -1521134295 + Y.GetHashCode(); + return hashCode; + } + + } +} \ No newline at end of file diff --git a/Recursion/LD_24/Code/TaskUtils.cs b/Recursion/LD_24/Code/TaskUtils.cs new file mode 100644 index 0000000..65c6e88 --- /dev/null +++ b/Recursion/LD_24/Code/TaskUtils.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace LD_24.Code +{ + + public class TaskUtils + { + public static BestPizzeriaResult FindBestPizzeria(Map map) + { + int lowestCost = int.MaxValue; + Point bestMeetingSpot = null; + Point bestPizzeria = null; + + List friends = map.FindAll(MapTile.Friend); + List pizzerias = map.FindAll(MapTile.Pizzeria); + List meetingSpots = map.FindAll(MapTile.MeetingSpot); + + foreach (var meetingSpot in meetingSpots) + { + foreach (var pizzeria in pizzerias) + { + int totalCost = 0; + + int toPizzeriaCostPerFriend = FindBestPath(map, meetingSpot, pizzeria); + if (toPizzeriaCostPerFriend < 0) { break; } + totalCost += toPizzeriaCostPerFriend * friends.Count; + + bool failed = false; + foreach (var friend in friends) + { + int toMeetingCost = FindBestPath(map, friend, meetingSpot); + if (toMeetingCost < 0) { failed = true; break; } + totalCost += toMeetingCost; + + int toHomeCost = FindBestPath(map, pizzeria, friend); + if (toHomeCost < 0) { failed = true; break; } + totalCost += toHomeCost; + } + if (failed) { break; } + + if (totalCost < lowestCost) + { + lowestCost = totalCost; + bestMeetingSpot = meetingSpot; + bestPizzeria = pizzeria; + } + } + } + + if (bestMeetingSpot != null) + { + return new BestPizzeriaResult(bestPizzeria, bestMeetingSpot, lowestCost); + } + else + { + return null; + } + } + + public static int FindBestPath(Map map, Point from, Point to) + { + return FindBestPath(map, from, to, new Stack()); + } + + private static int FindBestPath(Map map, Point from, Point to, Stack exploredPoints) + { + if (from.Equals(to)) { return 0; } + + int minCost = -1; + exploredPoints.Push(from); + foreach (var neighbour in GetNeighbours(map, from.X, from.Y)) + { + if (!exploredPoints.Contains(neighbour)) + { + int cost = FindBestPath(map, neighbour, to, exploredPoints); + if (cost >= 0) + { + if (minCost < 0) + { + minCost = cost + 1; + } else + { + minCost = Math.Min(minCost, cost + 1); + } + } + } + } + exploredPoints.Pop(); + return minCost; + } + + private static IEnumerable GetNeighbours(Map map, int x, int y) + { + if (map.IsInBounds(x + 1, y) && map.Get(x + 1, y) != MapTile.Wall) + { + yield return new Point(x + 1, y); + } + if (map.IsInBounds(x, y + 1) && map.Get(x, y + 1) != MapTile.Wall) + { + yield return new Point(x, y + 1); + } + if (map.IsInBounds(x - 1, y) && map.Get(x - 1, y) != MapTile.Wall) + { + yield return new Point(x - 1, y); + } + if (map.IsInBounds(x, y - 1) && map.Get(x, y - 1) != MapTile.Wall) + { + yield return new Point(x, y - 1); + } + } + } +} \ No newline at end of file diff --git a/Recursion/LD_24/Forma1.aspx b/Recursion/LD_24/Forma1.aspx new file mode 100644 index 0000000..cd26f1d --- /dev/null +++ b/Recursion/LD_24/Forma1.aspx @@ -0,0 +1,26 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Forma1.aspx.cs" Inherits="LD_24.Forma1" %> + + + + + + + + +
+ + +
+
+ +
+
+ + + + +
+ + + + diff --git a/Recursion/LD_24/Forma1.aspx.cs b/Recursion/LD_24/Forma1.aspx.cs new file mode 100644 index 0000000..83413a2 --- /dev/null +++ b/Recursion/LD_24/Forma1.aspx.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Web; +using System.Web.UI; +using System.Web.UI.WebControls; + +namespace LD_24 +{ + public partial class Forma1 : System.Web.UI.Page + { + private string inputFilename; + private string outputFilename; + private Code.Map map; + + protected void Page_Load(object sender, EventArgs e) + { + inputFilename = Server.MapPath(@"App_Data/U3.txt"); + outputFilename = Server.MapPath(@"App_Data/Rezultatai.txt"); + + map = Code.InOutUtils.ReadMap(inputFilename); + ShowMap(Table1, map); + } + + protected void Button1_Click(object sender, EventArgs e) + { + List friends = map.FindAll(Code.MapTile.Friend); + Code.BestPizzeriaResult result = Code.TaskUtils.FindBestPizzeria(map); + + Label1.Visible = true; + Label2.Visible = true; + Label3.Visible = true; + BulletedList1.Visible = true; + ShowFriends(BulletedList1, friends); + ShowBestPizzeriaResult(Label3, result); + + using (StreamWriter writer = new StreamWriter(outputFilename)) + { + Code.InOutUtils.WriteMap(writer, map); + writer.Write('\n'); + Code.InOutUtils.WriteFriendPositions(writer, friends); + Code.InOutUtils.WriteBestPizzeriaResult(writer, result); + } + } + + private void ShowFriends(BulletedList list, List friends) + { + list.Items.Clear(); + foreach (var friend in friends) + { + list.Items.Add(String.Format("X = {0}, Y = {1}", friend.X + 1, friend.Y + 1)); + } + } + + private void ShowBestPizzeriaResult(Label label, Code.BestPizzeriaResult result) + { + if (result == null) + { + label.Text = "Neįmanoma"; + return; + } + + label.Text = String.Format("Susitikimo vieta (X = {0}, Y = {1})", result.MeetingSpot.X + 1, result.MeetingSpot.Y + 1); + label.Text += "
"; + label.Text += String.Format("Picerija (X = {0}, Y = {1})", result.Pizzeria.X + 1, result.Pizzeria.Y + 1); + label.Text += "
"; + label.Text += String.Format("Nueita {0}", result.Cost); + } + + private void ShowMap(Table table, Code.Map map) + { + table.Rows.Clear(); + for (int y = 0; y < map.Height; y++) + { + TableRow row = new TableRow(); + for (int x = 0; x < map.Width; x++) + { + TableCell cell = new TableCell(); + cell.Width = 20; + cell.Height = 20; + switch (map.Get(x, y)) + { + case Code.MapTile.Empty: + cell.Text = "."; + break; + case Code.MapTile.Pizzeria: + cell.Text = "P"; + break; + case Code.MapTile.Friend: + cell.Text = "D"; + break; + case Code.MapTile.MeetingSpot: + cell.Text = "S"; + break; + case Code.MapTile.Wall: + cell.Text = "X"; + break; + default: + cell.Text = "?"; + break; + } + row.Cells.Add(cell); + } + table.Rows.Add(row); + } + } + } +} \ No newline at end of file diff --git a/Recursion/LD_24/Forma1.aspx.designer.cs b/Recursion/LD_24/Forma1.aspx.designer.cs new file mode 100644 index 0000000..2b8a812 --- /dev/null +++ b/Recursion/LD_24/Forma1.aspx.designer.cs @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace LD_24 +{ + + + public partial class Forma1 + { + + /// + /// form1 control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.HtmlControls.HtmlForm form1; + + /// + /// Table1 control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Table Table1; + + /// + /// Button1 control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Button Button1; + + /// + /// Label1 control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Label Label1; + + /// + /// BulletedList1 control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.BulletedList BulletedList1; + + /// + /// Label2 control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Label Label2; + + /// + /// Label3 control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Label Label3; + } +} diff --git a/Recursion/LD_24/LD_24.csproj b/Recursion/LD_24/LD_24.csproj new file mode 100644 index 0000000..cd05188 --- /dev/null +++ b/Recursion/LD_24/LD_24.csproj @@ -0,0 +1,140 @@ + + + + + Debug + AnyCPU + + + 2.0 + {6AAB1A29-81E1-4A08-A8C3-9C203C684B8F} + {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} + Library + Properties + LD_24 + LD_24 + v4.7.2 + true + + + + + + + + + + + + true + full + false + bin\ + DEBUG;TRACE + prompt + 4 + + + true + pdbonly + true + bin\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + ..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll + + + + + + + + + + + + + + + + + Forma1.aspx + ASPXCodeBehind + + + Forma1.aspx + + + + + + + Web.config + + + Web.config + + + + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + + + + + True + True + 57614 + / + http://localhost:44363/ + False + False + + + False + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + \ No newline at end of file diff --git a/Recursion/LD_24/Properties/AssemblyInfo.cs b/Recursion/LD_24/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..bf445ad --- /dev/null +++ b/Recursion/LD_24/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("LD_24")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("LD_24")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("6aab1a29-81e1-4a08-a8c3-9c203c684b8f")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Revision and Build Numbers +// by using the '*' as shown below: +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Recursion/LD_24/Web.Debug.config b/Recursion/LD_24/Web.Debug.config new file mode 100644 index 0000000..fae9cfe --- /dev/null +++ b/Recursion/LD_24/Web.Debug.config @@ -0,0 +1,30 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Recursion/LD_24/Web.Release.config b/Recursion/LD_24/Web.Release.config new file mode 100644 index 0000000..da6e960 --- /dev/null +++ b/Recursion/LD_24/Web.Release.config @@ -0,0 +1,31 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Recursion/LD_24/Web.config b/Recursion/LD_24/Web.config new file mode 100644 index 0000000..2d1c85e --- /dev/null +++ b/Recursion/LD_24/Web.config @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Recursion/LD_24/packages.config b/Recursion/LD_24/packages.config new file mode 100644 index 0000000..55d586f --- /dev/null +++ b/Recursion/LD_24/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Recursion/Recursion.sln b/Recursion/Recursion.sln new file mode 100644 index 0000000..138946f --- /dev/null +++ b/Recursion/Recursion.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32210.238 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LD_24", "LD_24\LD_24.csproj", "{6AAB1A29-81E1-4A08-A8C3-9C203C684B8F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6AAB1A29-81E1-4A08-A8C3-9C203C684B8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6AAB1A29-81E1-4A08-A8C3-9C203C684B8F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6AAB1A29-81E1-4A08-A8C3-9C203C684B8F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6AAB1A29-81E1-4A08-A8C3-9C203C684B8F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {285CDE53-D9E3-4FC7-A17F-A5702B740D70} + EndGlobalSection +EndGlobal