//[Route("FindFertileLand/{coordinates}")] public ActionResult FindFertileLand(LandInformation coordinates) { LandInformation landInformation = new LandInformation(); //ViewBag.Submitted = false; if (HttpContext.Request.RequestType == "POST") { // Request is Post type; must be a submit var barrenLand = Request.Form["BarrenLand"]; landInformation.BarrenLandInputString = barrenLand == null ? String.Empty : barrenLand; landInformation = BarrenLandDataController.GetAllFertileLand(landInformation); ViewBag.Submitted = true; ViewBag.Message = "Fertile land area search successfully completed."; } else { ViewBag.Message = "Enter barren land coordinates to find the fertile land area."; } return(View(landInformation)); }
//private static String[] STDIN = { "0 292 399 307" }; //private static String[] STDIN = {"48 192 351 207", "48 392 351 407", "120 52 135 547", "260 52 275 547"}; /// <summary> /// Find total fertile land in grid based on String array of rectangle endpoints /// </summary> /// <returns>The fertile land areas' square unit count array</returns> public static LandInformation GetAllFertileLand(LandInformation landInfo) { try { LandInformation updatedLandInfo = new LandInformation(landInfo.BarrenLandInputString); List <Int32> fertileLand = new List <Int32>(); String[] barrenLandStrings = CreateBarrenLandStrings(updatedLandInfo.BarrenLandInputString); List <Int32[]> barrenLandEndPoints = ConvertBarrenLandCoordinates(barrenLandStrings); List <GridUnit> allBarrenLand = new List <GridUnit>(); //Fill/create all barrenLand individual grid units foreach (Int32[] rectangle in barrenLandEndPoints) { allBarrenLand.AddRange(FillIndividualBarrenLandGridUnits(rectangle)); } //Loop through bounds of the entire 400 x 600 grid //create grid units for the entire 400 x 600 grid //filling a multidimentional array with the grid units for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { GridUnit unit = new GridUnit(x, y); //check if grid unit is present in the BarrenLand list if (allBarrenLand.Exists(xy => xy.X == x && xy.Y == y)) { //mark that grid unit as barren and accounted for unit.IsBarren = true; unit.AccountedFor = true; } grid[x, y] = unit; } } fertileLand = CheckForUnaccountedAreasAndCountFertileLand(fertileLand, 0, 0); fertileLand.Sort(); updatedLandInfo.FertileLandOutputString = ""; if (fertileLand.Count > 0) { foreach (Int32 land in fertileLand) { updatedLandInfo.FertileLandOutputString += land.ToString() + " "; } } else { updatedLandInfo.FertileLandOutputString = "No fertile land available."; } return(updatedLandInfo); } catch (Exception e) { //Log.Error(e, "GetAllFertileLand()"); return(null); } }