Skip to content

Instantly share code, notes, and snippets.

@basp1
Last active June 6, 2018 06:10
Show Gist options
  • Save basp1/54b114318c0841d883e647d298252dbc to your computer and use it in GitHub Desktop.
Save basp1/54b114318c0841d883e647d298252dbc to your computer and use it in GitHub Desktop.
Яндекс.Блиц. Финал. А. Сапер
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yandex.Blitz
{
class A_Miner
{
public static int solve(int n, int m, List<Tuple<int, int>> mines)
{
bool[][] x = new bool[n][];
for (int i = 0; i < n; i++)
{
x[i] = new bool[m];
}
foreach (var mine in mines)
{
x[mine.Item1][mine.Item2] = true;
}
return rec(x, 0);
}
private static int rec(bool[][] x, int nstep)
{
int n = x.Length;
int m = x[0].Length;
int firstEmptyI = -1, firstEmptyJ = -1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (!x[i][j])
{
firstEmptyI = i;
firstEmptyJ = j;
break;
}
}
if (-1 != firstEmptyI)
{
break;
}
}
if (-1 == firstEmptyI)
{
return nstep;
}
var next = new Stack<Tuple<int, int>>();
next.Push(new Tuple<int, int>(firstEmptyI, firstEmptyJ));
while (0 != next.Count)
{
var p = next.Pop();
if (!check(x, p.Item1, p.Item2)) continue;
x[p.Item1][p.Item2] = true;
if (check(x, p.Item1 - 1, p.Item2))
next.Push(new Tuple<int, int>(p.Item1 - 1, p.Item2));
if (check(x, p.Item1 + 1, p.Item2))
next.Push(new Tuple<int, int>(p.Item1 + 1, p.Item2));
if (check(x, p.Item1, p.Item2 - 1))
next.Push(new Tuple<int, int>(p.Item1, p.Item2 - 1));
if (check(x, p.Item1, p.Item2 + 1))
next.Push(new Tuple<int, int>(p.Item1, p.Item2 + 1));
}
return rec(x, nstep + 1);
}
private static bool check(bool[][] x, int i, int j)
{
int n = x.Length;
int m = x[0].Length;
return i < n && i >= 0 && j < m && j >= 0 && !x[i][j];
}
static void Main(string[] args)
{
var line = Console.ReadLine().Split(' ');
int n = int.Parse(line[0]);
int m = int.Parse(line[1]);
if (n <= 0 || m <= 0)
{
Console.WriteLine(0);
return;
}
int k = int.Parse(Console.ReadLine().Split(' ')[0]);
if (k <= 0)
{
Console.WriteLine(1);
return;
}
var mines = new List<Tuple<int, int>>();
for (int i = 0; i < k; i++)
{
line = Console.ReadLine().Split(' ');
mines.Add(new Tuple<int, int>(int.Parse(line[0]) - 1, int.Parse(line[1]) - 1));
}
int x = solve(n, m, mines);
Console.WriteLine(x);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment