/// <summary> /// Baby Height solver /// </summary> /// <param name="option">Options</param> /// <returns>Output list</returns> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Data parse && solve List<double> babyHeightInInches = this.GetBabyHeightInInches(option.Input.Content[0]); // Baby height parse option.Output.Content.Add(new List<string>(babyHeightInInches.Count)); for (int i = 0; i < babyHeightInInches.Count; i++) { option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + this.ParseBabyHeight(babyHeightInInches[i]) ); } }
/// <summary> /// Ocean view Solver /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add( this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Data parse List<List<Int32>> inputData = this.GetTask(ProblemSolverHelper.ConvertToDataLists<Int32>(option.Input.Content[0])); // Solve the problem List<Int32> houseNeedToDestroy = this.SolveCore(inputData); // Parse result to string lists option.Output.Content.Add(new List<string>(houseNeedToDestroy.Count)); for (int i = 0; i < houseNeedToDestroy.Count; i++) { option.Output.Content[0].Add( "Case #" + (i+1) + ": " + houseNeedToDestroy[i] //new List<string>(1) { "Case #" + (i+1) + ": " + houseNeedToDestroy[i] + "/" + inputData[i].Count } //debug ); } }
/// <summary> /// Problem solve method /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Get input List<Task> tasks = this.GetTasks(option.Input.Content[0]); // Result List<double> results = new List<double>(tasks.Count); // Solver foreach (var task in tasks) { throw (new NotImplementedException()); } // Output option.Output.Content.Add(new List<string>(results.Count)); for (int i = 0; i < results.Count; i++) { option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + results[i] ); } }
/// <summary> /// Problem solve method /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Get input List<Task> tasks = this.GetTask(option.Input.Content[0]); // Result List<Int64> results = new List<Int64>(tasks.Count); // Solver int count = 0; foreach (var task in tasks) { count++; Int64 low = 1; Int64 high = (Int64)Math.Sqrt(task.Ink / 2); // 2*n^2 + (2r-1)n > 2*n^2 while (low < high) { Int64 mid = (low + high) / 2; Int64 inkNeeded = (2 * mid * mid + (2 * task.Radius - 1) * mid); if (inkNeeded > task.Ink || inkNeeded < 0) { high = mid - 1; } else { low = mid + 1; } } results.Add( ((2 * low * low + (2 * task.Radius - 1) * low) > task.Ink) ? low - 1 : low ); } // Output option.Output.Content.Add(new List<string>(results.Count)); for (int i = 0; i < results.Count; i++) { option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + results[i] ); } }
/// <summary> /// Problem solve method /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Get input List<Task> tasks = this.GetTasks(option.Input.Content[0]); // Result List<List<int>> results = new List<List<int>>(tasks.Count); // Solver foreach (var task in tasks) { int[] factors = new int[] { 2, 3, 5, 7 }; for (int i = 0; i < task.R; i++) { List<int> productNums = task.Products[i]; List<Product> products = new List<Product>(productNums.Count); for (int j = 0; j < productNums.Count; j++) { products.Add( new Product(this.GetFactors(productNums[j], factors)) ); } } } // Output option.Output.Content.Add(new List<string>(results.Count)); option.Output.Content[0].Add("Case #1:"); for (int i = 0; i < results.Count; i++) { string guess = ""; for (int j = 0; j < results[i].Count; j++) { guess += results[i][j]; } option.Output.Content[0].Add(guess); } }
/// <summary> /// Problem solve method /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Get input List<Lawn> tasks = this.GetLawns(option.Input.Content[0]); // Result List<bool> results = new List<bool>(tasks.Count); // Solver foreach (var task in tasks) { MaxHeights maxHs = this.GetMaxHeightPerRow(task); bool canAchieve = true; for (int i = 0; i < task.Height; i++) { for (int j = 0; j < task.Width; j++) { if (task.Layout[i][j] < maxHs.Row[i] && task.Layout[i][j] < maxHs.Column[j]) { canAchieve = false; break; } } } results.Add(canAchieve); } // Output option.Output.Content.Add(new List<string>(results.Count)); for (int i = 0; i < results.Count; i++) { option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + (results[i] ? "YES" : "NO") ); } }
/// <summary> /// Hedgemony solver /// </summary> /// <param name="option">Options</param> /// <returns>Output list</returns> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Data parse List<List<Int32>> inputData = this.GetTask(ProblemSolverHelper.ConvertToDataLists<Int32>(option.Input.Content[0])); List<double> bushesHeights = new List<double>(inputData.Count); // Solve the problem foreach (List<Int32> heights in inputData) { double newBushHeight = 0.0; for (int i = 0; i < heights.Count - 1; i++) { if (i == 0) { newBushHeight = heights[0]; } else { newBushHeight = (newBushHeight + heights[i + 1]) / 2.0; if (newBushHeight > heights[i]) { newBushHeight = heights[i]; } } } bushesHeights.Add(newBushHeight); } // Parse result to string lists option.Output.Content.Add(new List<string>(bushesHeights.Count)); for (int i = 0; i < bushesHeights.Count; i++) { option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + bushesHeights[i] ); } }
/// <summary> /// Line Switch Solver /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Get input List<List<CarState>> ccTasks = this.GetRoadStates(option.Input.Content[0]); // Result List<double> maxSafeSeconds = new List<double>(ccTasks.Count); // Solve foreach (var task in ccTasks) { RoadEnding ending = this.GetEndingOfRoad(task, new CollideTimeState(0,-1,-1)); maxSafeSeconds.Add( ending.CanSafeDriveForever ? -1 : ending.TimeBeforeCollide ); } // Output option.Output.Content.Add(new List<string>(maxSafeSeconds.Count)); for (int i = 0; i < maxSafeSeconds.Count; i++) { option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + (maxSafeSeconds[i] < 0 ? "Possible" : maxSafeSeconds[i].ToString()) ); } }
/// <summary> /// Problem solve method /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Get input int arrSize = int.Parse(option.Input.Content[0][0]); //int cycle = 100000; int sizeForQuick = arrSize * arrSize; int sizeForBubble = arrSize / (int)Math.Log(arrSize, 2) * 30; // Result List<string> results = new List<string>(3); // Compare int[] arr0 = GetRandomArr(sizeForBubble); int[] arr1 = GetRandomArr(sizeForQuick); //int[] arr0 = GetSequenceArr(sizeForBubble, -1); //int[] arr1 = GetSequenceArr(sizeForQuick, -1); //ByzodToolkit.CodeTimer.Initialize(); //ByzodToolkit.CodeTimer.Time("Empty loop: (" + cycle + " cycles)", cycle, () => { int[] arrResult = arr0; }); //ByzodToolkit.CodeTimer.Time("Bubble sort(" + sizeForBubble + " x " + cycle + " cycles): ", cycle, () => { BubbleSort(arr0); }); //ByzodToolkit.CodeTimer.Time("Quick sort(" + sizeForQuick + " x " + cycle + " cycles): ", cycle, () => { QuickSort(arr1, 0, arrSize - 1); }); }
/// <summary> /// Problem solve method /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Get input switch (option.Problem.Args[0]) { case "S": // Result List<Int32> results = new List<Int32>(200); List<Endpoint> tasks = new List<Endpoint>(100); tasks = this.GetEndpoints(option.Input.Content[0]); // Solver foreach (var task in tasks) { Int32 count = 0; // Search directly Int64 root = (int)Math.Sqrt(task.A); // validate while (root * root < task.A) { root++; } while (root * root <= task.B) { if (this.IsPalindrome(root.ToString()) && this.IsPalindrome((root * root).ToString())) { count++; } root++; } results.Add(count); } // Output option.Output.Content.Add(new List<string>(results.Count)); for (int i = 0; i < results.Count; i++) { option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + results[i] ); } break; case "L1": //dead break; case "L2": // Result List<BigInteger> bigResults = new List<BigInteger>(1000); List<BigEndpoint> bigTasks = this.GetBigEndpoints(option.Input.Content[0]); foreach (var task in bigTasks) { BigInteger count = 0; BigInteger rootA = BigIntegerSqrt(task.A); BigInteger rootB = BigIntegerSqrt(task.B); // crop if (BigInteger.Multiply(rootA, rootA) < task.A) { rootA += 1; } if (BigInteger.Multiply(rootB, rootB) > task.B) { rootB -= 1; } if (rootA <= rootB) { string rootAStr = rootA.ToString(); string rootBStr = rootB.ToString(); int firstNumOfA = (rootAStr[0] - '0'); int lastNumOfA = (rootAStr[rootAStr.Length - 1] - '0'); int firstNumOfB = (rootBStr[0] - '0'); int lastNumOfB = (rootBStr[rootBStr.Length - 1] - '0'); if (rootAStr.Length == rootBStr.Length) { // Add length same as A, A to B count += GetPalindromeCount( rootAStr.Length, firstNumOfA, firstNumOfB, lastNumOfA, lastNumOfB ); } else // root a < root b { for (int numLength = rootAStr.Length + 1; numLength < rootBStr.Length; numLength++) { // Add length same as A, A to B count += GetPalindromeCount( numLength, 1, 9, 0, 9 ); } // Add length with A count += GetPalindromeCount( rootAStr.Length, firstNumOfA, 9, lastNumOfA, 9 ); // Add length with B count += GetPalindromeCount( rootBStr.Length, 1, firstNumOfB, 0, lastNumOfB ); } } bigResults.Add(count); } // Output option.Output.Content.Add(new List<string>(bigResults.Count)); for (int i = 0; i < bigResults.Count; i++) { option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + bigResults[i] ); } break; default: break; } }
/// <summary> /// Zombie smasher /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Data parse List<List<List<Int32>>> zombieList = this.GetZombieList(option.Input.Content[0]); // Sort by zombie appear time foreach (var zombies in zombieList) { zombies.Sort((listX, listY) => { return (listX[2] == listY[2]) ? (Math.Abs(listX[0]) + Math.Abs(listX[1]) - Math.Abs(listY[0]) - Math.Abs(listY[1])) : listX[2] - listY[2]; }); } // Solve List<Int32> smashCount = new List<int>(zombieList.Count); for (int i = 0; i < zombieList.Count; i++) { Int32 t = 0; Int32 x = 0; Int32 y = 0; Int32 count = 0; for (int j = 0; j < zombieList[i].Count; j++) { Int32 td = GetMoveTime(x, y, zombieList[i][j][0], zombieList[i][j][1]); td = (td > 750) || (j == 0) ? td : 750; if (t + td < zombieList[i][j][2] + 1000) { t = t + td; x = zombieList[i][j][0]; y = zombieList[i][j][1]; count += 1; } } smashCount.Add(count); } // Parse result to string lists option.Output.Content.Add(new List<string>(smashCount.Count)); for (int i = 0; i < smashCount.Count; i++) { option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + smashCount[i] ); } }
/// <summary> /// Problem solve method /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Get input List<Task> tasks = this.GetTasks(option.Input.Content[0]); // Result List<Int64> results = new List<Int64>(tasks.Count); // Solver foreach (var task in tasks) { List<int> spanArray = this.GetSpanArray(task.Vs); int recoverSteps = task.E % task.R == 0 ? task.E / task.R : (task.E / task.R) + 1; Int64 maxJoy = 0; Int64 energy = task.E; for (int i = 0; i < task.Vs.Count; i++) { if (spanArray[i] >= recoverSteps || spanArray[i] == 0) { // use all maxJoy += energy * task.Vs[i]; energy = 0; } else { // use as much maxJoy += (energy + spanArray[i] * task.R - task.E) * task.Vs[i]; energy = task.E - spanArray[i] * task.R; } // Recover energy = energy + task.R > task.E ? task.E : energy + task.R; } results.Add(maxJoy); } // Output option.Output.Content.Add(new List<string>(results.Count)); for (int i = 0; i < results.Count; i++) { option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + results[i] ); } }
/// <summary> /// Kindom Rush Solver /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Get input List<List<List<Int32>>> krTasks = this.GetTask(ProblemSolverHelper.ConvertToDataLists<Int32>(option.Input.Content[0])); // Result List<Int32> playLevelsCount = new List<int>(krTasks.Count); // Store levels and sort them by level 2 hardness foreach (var task in krTasks) { List<List<KRLevel>> SortedLevels = new List<List<KRLevel>>(2002); // Limit Int32 hardest = 0; // Initialize sorted levels for (int i = 0; i < SortedLevels.Capacity; i++) { // Assume the size of levels of same hard star requirement SortedLevels.Add(new List<KRLevel>(200)); } for (int i = 0; i < task.Count; i++) { // SortedLevels[k] are all levels with hard=k SortedLevels[task[i][1]] .Add(new KRLevel(task[i][0], task[i][1])); hardest = task[i][1] > hardest ? task[i][1] : hardest; } foreach (var levelWithSameHard in SortedLevels) { // Decrease sort by level.Easy levelWithSameHard.Sort((x, y) => { return y.Easy - x.Easy; }); } // Simulate // Stars got Int32 stars = 0; // Levels beaten Int32 levelBeaten = 0; // Levels played Int32 levelPlayed = 0; // All levels beaten bool AllStars = false; while (true) { // 0 = none can beat, 1 = beat a easy, 2 = beat a hard Int32 beatState = 0; // We (can) beat all levels if (stars >= hardest || levelBeaten >= task.Count) { // All left levels can be beaten in one run levelPlayed += task.Count - levelBeaten; AllStars = true; break; } // Try beat hard for (int i = stars; i >= 0; i--) { if (SortedLevels[i].Count > 0) { KRLevel hardestLevel = SortedLevels[i].First(); stars += 2 - hardestLevel.Progress; // 2 or 1 start got levelBeaten++; levelPlayed++; beatState = 2; SortedLevels[i].Remove(hardestLevel); break; } } if (beatState < 2) { // Try beat easy for (int i = hardest; i >= 0; i--) { if (SortedLevels[i].Count > 0) { for (int j = 0; j < SortedLevels[i].Count; j++) { KRLevel hardestLevel = SortedLevels[i][j]; if (stars >= hardestLevel.Easy) // 1 start got { stars++; //levelBeaten++; No this line cuz' it's not beaten levelPlayed++; beatState = 1; hardestLevel.Progress = 1; break; } else { continue; } } } if (beatState > 0) { break; } } } // You Can (Not) Advance if (beatState == 0) { AllStars = false; break; } } if (AllStars) { playLevelsCount.Add(levelPlayed); } else { playLevelsCount.Add(0); } } // Output option.Output.Content.Add(new List<string>(playLevelsCount.Count)); for (int i = 0; i < playLevelsCount.Count; i++) { option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + (playLevelsCount[i] == 0 ? "Too Bad" : playLevelsCount[i].ToString()) ); } }
/// <summary> /// Dummy Solver, simply returns the input. /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); option.Output.Content = option.Input.Content; }
/// <summary> /// Paths smasher /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Data parse List<List<List<Int32>>> pathList = this.GetPathList(option.Input.Content[0]); // Solve List<Int32> pathsCount = new List<int>(pathList.Count); foreach (var paths in pathList) { bool infinity = false; Int32 position = 1; string pathFollowed = ""; while (!infinity) { if (position > paths.Count) // We're out { pathsCount.Add(pathFollowed.Length); break; } else { pathFollowed += position; paths[position-1][2] = (paths[position-1][2] + 1) % 2; if (Regex.IsMatch(pathFollowed, "((\\d)\\d+)\\1\\2")) { infinity = true; pathsCount.Add(0); } else { position = paths[position-1][2] == 1 ? paths[position-1][0] : paths[position-1][1]; } } } } // Parse result to string lists option.Output.Content.Add(new List<string>(pathsCount.Count)); for (int i = 0; i < pathsCount.Count; i++) { option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + (pathsCount[i] == 0 ? "Infinity" : pathsCount[i].ToString()) ); } }
/// <summary> /// Problem solve method /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Get input List<int[][]> tasks = this.GetBoard(option.Input.Content[0]); // Result List<double> results = new List<double>(tasks.Count); // Solver foreach (var task in tasks) { int winState = 0; // diag winState = this.GetLineState(task[0][0], task[1][1], task[2][2], task[3][3]); if (winState != 0) { results.Add(winState); continue; } // diag winState = this.GetLineState(task[0][3], task[1][2], task[2][1], task[3][0]); if (winState != 0) { results.Add(winState); continue; } //v for (int i = 0; i < 4; i++) { winState = this.GetLineState(task[0][i], task[1][i], task[2][i], task[3][i]); if (winState != 0) { results.Add(winState); break; } } if (winState != 0) continue; //h for (int i = 0; i < 4; i++) { winState = this.GetLineState(task[i][0], task[i][1], task[i][2], task[i][3]); if (winState != 0) { results.Add(winState); break; } } if (winState != 0) continue; if (winState == 0) { for (int i = 0; i < 4; i++) { if (task[i].Contains(0)) { winState = 0; // incomplete break; } else { winState = 3; // draw } } results.Add(winState); } } // Output option.Output.Content.Add(new List<string>(results.Count)); for (int i = 0; i < results.Count; i++) { string resultStr = ""; if (results[i] == 1) { resultStr = "X won"; } else if (results[i] == -1) { resultStr = "O won"; } else if (results[i] == 3) { resultStr = "Draw"; } else { resultStr = "Game has not completed"; } option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + resultStr ); } }
/// <summary> /// Problem solve method /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Get input List<List<int>> tasks = this.GetShows(option.Input.Content[0]); // Result List<List<double>> results = new List<List<double>>(tasks.Count); // Solver foreach (var task in tasks) { double scoreSum = task.Sum(); double safeScore = 0; List<double> minPercentage = new List<double>(task.Count); safeScore = scoreSum * 2.0 / (double)task.Count; int safeCount = 0; double overSafeSum = 0; for (int i = 0; i < task.Count; i++) { if (task[i] > safeScore) { safeCount++; overSafeSum += task[i] - safeScore; } } safeScore -= overSafeSum / (task.Count - safeCount); for (int i = 0; i < task.Count; i++) { minPercentage.Add( task[i] > safeScore ? 0 : ((safeScore - task[i]) / scoreSum) * 100 ); } results.Add(minPercentage); } // Output option.Output.Content.Add(new List<string>(results.Count)); for (int i = 0; i < results.Count; i++) { option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + string.Join( " ", (from min in results[i] select min.ToString()) .ToArray() ) ); } }
/// <summary> /// Problem solve method /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Get input List<List<int>> tasks = this.GetSets(option.Input.Content[0]); // Result List<List<int>> results = new List<List<int>>(tasks.Count); // Solver foreach (var task in tasks) { List<Limit> sumLimits = new List<Limit>(task.Count); for (int i = 1; i < task.Count; i++) { int max = 0; int min = 0; for (int j = 0; j < i; j++) { min += task[j]; max += task[task.Count - j - 1]; } sumLimits.Add(new Limit(min, max)); } for (int i = 0; i < task.Count / 2; i++) { // find a sum of array int sum = 0; for (int j = 0; j < task.Count; j++) { if (sum > sumLimits[j].Min && sum < sumLimits[j].Max) { // check all array with length j+1 } } } } // Output option.Output.Content.Add(new List<string>(results.Count)); for (int i = 0; i < results.Count; i++) { string result = "Case #" + (i + 1) + ": "; if (results[i].Count > 0) { result += "\n"; result += string.Join( " ", (from num in results[i] select num.ToString()) .ToArray() ); } if (results[i + 1].Count > 0) { result += "\n"; result += string.Join( " ", (from num in results[i + 1] select num.ToString()) .ToArray() ); } option.Output.Content[0].Add(result); } }
/// <summary> /// Password Solver /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Get input List<PasswordTask> pwTasks = this.GetProbList(option.Input.Content[0]); // Result List<double> expectTries = new List<double>(pwTasks.Count); // Solver foreach (var task in pwTasks) { // Prob partial multiple cache, probMul[k] means at least k character is right List<double> probMul = new List<double>(task.A + 1); // E cache, A times hit backspace, 1 time for enter directly List<double> E = new List<double>(task.A + 2); // probMul[0] = 1 probMul.Add(1.0); // probMul[k] = p[0]*...p[k-1] for (int i = 1; i < task.A + 1; i++) { if (i == 1) { // probMul[0] = P[0] probMul.Add(task.ProbabilitiesList[0]); } else { // probMul[i] = P[i-1]*...*P[0] probMul.Add(task.ProbabilitiesList[i - 1] * probMul[i - 1]); } } // A's backspace for (int i = 0; i < task.A + 1; i++) { E.Add( ((task.B - task.A + 1 + 2 * i) * probMul[task.A - i]) + (2 * task.B - task.A + 2 + 2 * i) * (1 - probMul[task.A - i]) ); } // 1 enter case E.Add(task.B + 2); expectTries.Add(E.Min()); } // Output option.Output.Content.Add(new List<string>(expectTries.Count)); for (int i = 0; i < expectTries.Count; i++) { option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + expectTries[i] ); } }
/// <summary> /// Problem solve method /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Get input List<Task> tasks =this.GetTask(option.Input.Content[0]); // Result List<int> results = new List<int>(tasks.Count); // Solver foreach (var task in tasks) { int addOp = 0; int minOp = 0; int size = task.StartSize; task.Motes.Sort(); int maxSize = task.Motes.Last(); minOp = task.Motes.Count; if (task.StartSize > 1) { for (int i = 0; i < task.Motes.Count; i++) { if (size <= task.Motes[i]) { while (size <= task.Motes[i]) { size += size - 1; addOp++; } } size += task.Motes[i]; minOp = Math.Min(minOp, (task.Motes.Count - i - 1) + addOp); } } results.Add(minOp); } // Output option.Output.Content.Add(new List<string>(results.Count)); for (int i = 0; i < results.Count; i++) { option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + results[i] ); } }
/// <summary> /// Problem solve method /// </summary> /// <param name="option">Options</param> public void Solve(ref Options option) { // Pre-process option.IsBinaryLoad = false; option.IsBinaryWrite = false; option.LoadContent(); // Can We handle that? if (option.Input.Content.Count > this.MaxFiles) { option.Warnings.Add(this.ID + " can only process " + this.MaxFiles + " files a time, the files after the first one will be ignored."); } // Get input List<Task> tasks = this.GetTasks(option.Input.Content[0]); // Result List<List<int>> results = new List<List<int>>(tasks.Count); // Solver foreach (var task in tasks) { List<int> trace = new List<int>(task.Chests.Count); int[] chestCount = new int[201]; //int[][] chestMatrix = new int[201][]; //for (int i = 0; i < chestMatrix.Length; i++) //{ // chestMatrix[i] = new int[201]; //} // Check possibility first bool impossibleToLootAll = false; int[] allKeys = new int[201]; foreach (var chest in task.Chests) { chestCount[chest.LockType] += 1; // Initialize chest count BTW this.LootAllKeys(allKeys, chest.Keys); } foreach (var chest in task.Chests) { // A chest that no key or not enough keys to open if (this.TryOpen(chest, allKeys) == false) { impossibleToLootAll = true; }; } // solve only if there's a chance if (impossibleToLootAll == false) { int[] keysNeeded = new int[201]; for (int i = 0; i < task.Keys.Length; i++) { keysNeeded[i] = chestCount[i] > task.Keys[i] ? chestCount[i] - task.Keys[i] : 0; } } results.Add(trace); } // Output option.Output.Content.Add(new List<string>(results.Count)); for (int i = 0; i < results.Count; i++) { string resultString = ""; if (results[i].Count > 0) { resultString = string.Join( " ", (from str in results[i] select str.ToString()) .ToArray() ); } else { resultString = "IMPOSSIBLE"; } option.Output.Content[0].Add( "Case #" + (i + 1) + ": " + resultString ); } }
static void Main(string[] args) { Options options = new Options(); IOHandler io = new IOHandler(); ProblemSolverHelper solverHelper = new ProblemSolverHelper(); // Try parse options from args try { options.Parse(args.ToList()); options.Validate(); } catch (Exception exception) { options.Errors.Add(exception.Message); } // Treat empty args call as help request if (args.Length <= 0) { options.ShouldShowHelp = true; } // Continue only if no errors or show help instead if it's a help request if (options.Errors.Count == 0 && !(options.ShouldShowHelp || options.ShouldListProblemIDs)) { // Main process ProblemSolverHelper.SolveState solveState = solverHelper.CallSolver( options.Problem.ID.ToLower(), ref options ); if (solveState == ProblemSolverHelper.SolveState.SolverNotExist) { options.Errors.Add("Unrecognized problem ID: " + options.Problem.ID); } // Result output: console display if (options.Output.Mode == Options.OutputMode.ConsoleOnly || options.Output.Mode == Options.OutputMode.FileAndConsole) { if (options.IsBinaryWrite) { io.Show(new string[] { "[Binary content]" }); } else { foreach (var output in options.Output.Content) { io.Show(output); } } } // Result output: file output if (options.Output.Mode == Options.OutputMode.FileAndConsole || options.Output.Mode == Options.OutputMode.FileOnly) { if (options.IsBinaryWrite) { for (int i = 0; i < options.Output.BinaryContent.Count; i++) { try { io.Write(options.Output.Paths[i], options.Output.BinaryContent[i]); } catch (Exception exception) { options.Errors.Add(exception.Message); } } } else { for (int i = 0; i < options.Output.Content.Count; i++) { try { io.Write(options.Output.Paths[i], options.Output.Content[i]); } catch (Exception exception) { options.Errors.Add(exception.Message); } } } } } // Show help in console if (options.ShouldShowHelp) { io.Show(Options.Help); } // Show problem IDs list in console if (options.ShouldListProblemIDs) { io.Show(solverHelper.ProblemSolvers.Keys); } if (options.Errors.Count > 0 && !(options.ShouldShowHelp || options.ShouldListProblemIDs) && options.ConsoleMode != Options.ConsoleDisplayMode.WarningsOnly && options.ConsoleMode != Options.ConsoleDisplayMode.None) { // Show errors Console.WriteLine("Process aborted"); Console.WriteLine("Error" + (options.Errors.Count > 1 ? "s" : "") + ":"); foreach (string error in options.Errors) { Console.WriteLine(" " + error); } } if (options.Warnings.Count > 0 && !(options.ShouldShowHelp || options.ShouldListProblemIDs) && options.ConsoleMode != Options.ConsoleDisplayMode.ErrorsOnly && options.ConsoleMode != Options.ConsoleDisplayMode.None) { // Show warnings Console.WriteLine("Warning" + (options.Warnings.Count > 1 ? "s" : "") + ":"); foreach (string waning in options.Warnings) { Console.WriteLine(" " + waning); } } }