using System.Text;
using System.Collections;
using System.IO;
class item
{
public int x;
public int y;
private item() { }
public item(int key, int value)
{
this.x = key;
this.y = value;
}
}
class Program
{
static char[][] ch;
static int dx = 0;
static int dy = 0;
static int cx = 0;
static int cy = 0;
static int w;
static int h;
static void Main(string[] args)
{
//StreamWriter sw = File.CreateText("c:\\a.txt");
//sw.WriteLine("1000 1000");
//for (int i = 0; i < 1000; i++)
//{
// for (int j = 0; j < 1000; j++)
// {
// if (i == 25 && j == 25)
// sw.Write("@");
// else if (i == 976 && j == 973)
// sw.Write("$");
// if (j % 10 == 0 && i % 10 == 0)
// sw.Write("D");
// else
// sw.Write(".");
// }
// sw.WriteLine();
//}
//sw.Close();
string[] ss = (Console.ReadLine().Trim()).Split(new char[] { ' ' });
//string[] ss = ("7 5".Split(new char[] { ' ' }));
h = int.Parse(ss[0]);
w = int.Parse(ss[ss.Length - 1]);
ch = new char[h][];
//string[] ty = new string[]{
// "....@",
// ".....",
// ".....",
// ".....",
// "D...D",
// ".....",
// "$...."
//};
for (int j = 0; j < h; j++)
{
string s = Console.ReadLine().Trim();
// string s = ty[j];
ch[j] = s.ToCharArray();
if (s.IndexOf('$') >= 0)
{
dx = s.IndexOf('$');
dy = j;
}
if (s.IndexOf('@') >= 0)
{
cx = s.IndexOf('@');
cy = j;
}
}
//bool ass= TryIt(copyof(), 10, w, h);
//Console.WriteLine(ass);
//int ere = 0;
int a = 0;
if (TryIt(copyof(), 0, w, h) == false)
a = 0;
else
{
a = 0;
int b = minDist();
while (b > (a + 1))
{
int mid = (a + b) / 2;
if (TryIt(copyof(), mid, w, h))
{
a = mid;
mid = (a + b) / 2;
}
else
{
b = mid;
mid = (a + b) / 2;
}
}
if (b > a)
{
if (TryIt(copyof(), b, w, h))
a = b;
else
{
if (!TryIt(copyof(), a, w, h))
a--;
//Console.WriteLine("here a= " + a);
}
}
}
Console.WriteLine(a);
}
static char[][] copyof()
{
char[][] ch1 = new char[h][];
for (int i = 0; i < h; i++)
{
ch1[i] = new char[w];
for (int j = 0; j < w; j++)
{
ch1[i][j] = ch[i][j];
}
}
return ch1;
}
static bool TryIt(char [][] arg,int dist, int w, int h)
{
// Console.WriteLine("tryeIt = " + dist);
item [] ars = new item[1000000];
int arrcnt = 0;
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
if (arg[j][i] == 'D')
ars[arrcnt++]=new item(i, j);
}
}
int cnt = 0;
while (arrcnt > 0 && cnt<(dist-1))
{
item[] temp = new item[1000000];
int tempcnt = 0;
while (arrcnt > 0)
{
item a = ars[arrcnt-1];
arrcnt--;
item[] nei = GetNeighbour(a.x, a.y);
foreach (item nn in nei)
{
if (nn.x >= 0 && nn.y >= 0 && nn.x < w && nn.y < h)
{
if (arg[nn.y][nn.x] == '@' || arg[nn.y][nn.x] == '$')
return false;
if (arg[nn.y][nn.x] != 'D')
{
temp[tempcnt++] = new item(nn.x, nn.y);
arg[nn.y][nn.x] = 'D';
}
}
}
}
ars = temp;
arrcnt = tempcnt;
//PrintArr(arg, cnt);
cnt++;
}
if (cnt < (dist-1))
return false;
//Console.WriteLine(" Again##########");
ars = new item[1000000];
arrcnt = 0;
ars[arrcnt++]=new item(cx, cy);
while (arrcnt > 0)
{
item a = ars[arrcnt-1];
arrcnt--;
item[] nei = GetNeighbour(a.x, a.y);
foreach (item nn in nei)
{
if (nn.x >= 0 && nn.y >= 0 && nn.x < w && nn.y < h)
{
if (arg[nn.y][nn.x] == '$' )
return true;
//arr.Add(new item(nn.x, nn.y));
if (arg[nn.y][nn.x] != 'D' && arg[nn.y][nn.x]!='@')
{
ars[arrcnt++] = new item(nn.x, nn.y);
arg[nn.y][nn.x] = '@';
}
}
}
//PrintArr(arg, 0);
}
return false;
}
static void PrintArr(char[][] c , int cnt)
{
Console.WriteLine("for cnt= " + cnt);
for (int j = 0; j < c.Length; j++)
{
for (int i = 0; i < c[j].Length; i++)
Console.Write(c[j][i]);
Console.WriteLine();
}
}
static item[] GetNeighbour(int x, int y)
{
item[] ret = new item[8];
ret[0] = new item(x - 1, y - 1);
ret[1] = new item(x , y - 1);
ret[2] = new item(x + 1, y - 1);
ret[3] = new item(x - 1, y );
ret[4] = new item(x + 1, y);
ret[5] = new item(x - 1, y + 1);
ret[6] = new item(x , y + 1);
ret[7] = new item(x + 1, y + 1);
return ret;
}
static int minDist()
{
int mindist = Math.Max(w, h);
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
if (ch[j][i] == 'D')
{
int aa = Math.Max(Math.Abs(cx - i), Math.Abs(cy - j));
int bb = Math.Max(Math.Abs(dx - i), Math.Abs(dy - j));
int cc = Math.Min(aa, bb);
if (cc < mindist)
mindist = cc;
}
}
}
return mindist;
}
}
Comments

