using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using MessageBoxUtils;
namespace CS_Console_Test.Contest
{
abstract class ContestProblems
{
// Constants
private const string EditorProgram = @"C:\Program Files\Sublime Text 2\sublime_text.exe";
private const string InputFile = @"Z:\Contest\Case Files\B-small-practice.in";
private const string OutputFile = @"Z:\Contest\Results\B-small-practice.txt";
public static void Run()
{
SolveProblem(InputFile, OutputFile);
File.Copy(@"C:\Users\AceInfinity\Documents\Visual Studio 2012\Projects\CS Console Test\CS Console Test\Contest\ContestProblems.cs", @"Z:\Contest\source.cs", true);
}
/// <summary>
/// Invokes the editor to display the contents of the results file input.
/// </summary>
/// <param name="resultFile">Results file to show in editor as the file argument.</param>
private static void ShowResultInEditor(string resultFile)
{
Process.Start(EditorProgram, resultFile);
}
/// <summary>
/// Main method in which calls to solve the cases from the input case file, saves, and displays the results.
/// </summary>
/// <param name="inputCaseFile">Input case file for case evaluation.</param>
/// <param name="outputResultsFile">Output file for each case result.</param>
private static void SolveProblem(string inputCaseFile, string outputResultsFile)
{
CaseResultCollection resultsCollection = new CaseResultCollection();
GetAnswers(inputCaseFile, ref resultsCollection);
resultsCollection.SaveResults(outputResultsFile);
ShowResultInEditor(outputResultsFile);
}
/// <summary>
/// Collects the list of answers for each case.
/// </summary>
/// <param name="inputCaseFile">Input case file for case evaluation.</param>
/// <param name="resultsCollection">Stores the result for each case from the input case file.</param>
private static void GetAnswers(string inputCaseFile, ref CaseResultCollection resultsCollection)
{
using (StreamReader sr = new StreamReader(inputCaseFile))
{
int T = int.Parse(sr.ReadLine());
for (int x = 1; x <= T; x++)
{
int[] dimensions = sr.ReadLine().Split(' ').Select(int.Parse).ToArray();
int rows = dimensions[0];
int columns = dimensions[1];
int[,] lawn = new int[rows, columns];
// Populate
for (int i = 0; i < rows; i++)
{
int[] row = sr.ReadLine().Split(' ').Select(int.Parse).ToArray();
for (int j = 0; j < columns; j++)
{
lawn[i, j] = row[j];
}
}
// Check
if (rows == 1 || columns == 1)
{
resultsCollection.Add(new CaseResult(x, "YES"));
continue;
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
int count = 0;
if (lawn[i, j > 0 ? j - 1 : j] > lawn[i, j] && lawn[i > 0 ? i - 1 : i, j] > lawn[i, j])
{
count++;
}
if (lawn[i, j < columns - 1 ? j + 1 : j] > lawn[i, j] && lawn[i > 0 ? i - 1 : i, j] > lawn[i, j])
{
count++;
}
if (lawn[i, j > 0 ? j - 1 : j] > lawn[i, j] && lawn[i < rows - 1 ? i + 1 : i, j] > lawn[i, j])
{
count++;
}
if (lawn[i, j < columns - 1 ? j + 1 : j] > lawn[i, j] && lawn[i < rows - 1 ? i + 1 : i, j] > lawn[i, j])
{
count++;
}
if (count > 0) goto NextCase;
}
}
resultsCollection.Add(new CaseResult(x, "YES"));
continue;
NextCase:
resultsCollection.Add(new CaseResult(x, "NO"));
}
}
}
}
#region Case Management
/// <summary>
/// Stores data for a single case result.
/// </summary>
internal class CaseResult
{
public CaseResult() : this(0, string.Empty) { }
public CaseResult(int caseNum, string result)
{
CaseNumber = caseNum;
Result = result;
}
public int CaseNumber { get; set; }
public string Result { get; set; }
public override string ToString()
{
return string.Format("Case #{0}: {1}", CaseNumber, Result);
}
}
/// <summary>
/// Stores a collection of case results.
/// </summary>
internal class CaseResultCollection : ICollection<CaseResult>
{
private readonly List<CaseResult> _caseResults = new List<CaseResult>();
public void SaveResults(string outputFile)
{
using (StreamWriter sw = new StreamWriter(outputFile))
{
foreach (var result in _caseResults)
{
sw.WriteLine(result.ToString());
}
}
}
public void Add(CaseResult item)
{
_caseResults.Add(item);
}
public void Clear()
{
_caseResults.Clear();
}
public bool Contains(CaseResult item)
{
return _caseResults.IndexOf(item) > -1;
}
public void CopyTo(CaseResult[] array, int arrayIndex)
{
_caseResults.CopyTo(array, 0);
}
public int Count
{
get { return _caseResults.Count; }
}
public bool IsReadOnly
{
get { return true; }
}
public bool Remove(CaseResult item)
{
return _caseResults.Remove(item);
}
public IEnumerator<CaseResult> GetEnumerator()
{
return _caseResults.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _caseResults.GetEnumerator();
}
}
#endregion
}