Pagini recente » Cod sursa (job #1817485) | Cod sursa (job #2641664) | Cod sursa (job #156092) | Cod sursa (job #1888918) | Cod sursa (job #657112)
Cod sursa(job #657112)
# include <fstream>
# include <queue>
using namespace std;
ifstream f ("barbar.in");
ofstream g ("barbar.out");
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int n, m, nr = 1;
char c;
short a[1010][1010], b[1010][1010];
queue < pair <int, int> > Q;
pair <int, int> start, finish;
void BFs ()
{
for (; !Q.empty (); Q.pop ())
{
pair <int, int> ret = Q.front ();
for (int k = 0; k < 4; ++k)
{
pair <int, int> acm = ret;
acm.first += dx[k];
acm.second += dy[k];
if (acm.first > 0 && acm.first <= n && acm.second > 0 && acm.second <= m)
{
if (!a[acm.first][acm.second])
{
a[acm.first][acm.second] = a[ret.first][ret.second] + 1;
Q.push (acm);
}
}
}
}
}
inline int ok (int X)
{
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
b[i][j] = a[i][j];
if (a[start.first][start.second] < X) return 0;
while (!Q.empty ()) Q.pop ();
Q.push (start);
for (; !Q.empty (); Q.pop ())
{
pair <int, int> ret = Q.front ();
for (int k = 0; k < 4; ++k)
{
pair <int, int> acm = ret;
acm.first += dx[k];
acm.second += dy[k];
if (acm.first > 0 && acm.first <= n && acm.second > 0 && acm.second <= m)
{
if (b[acm.first][acm.second] >= X && b[acm.first][acm.second] >= 0)
{
b[acm.first][acm.second] = -1;
Q.push (acm);
if (acm.first == finish.first && acm.second == finish.second)
{
nr = 0;
return 1;
}
}
}
}
}
return 0;
}
inline int bs ()
{
int i, cnt = 1 << 13;
for (i = 0; cnt > 0; cnt >>= 1)
if (ok (i + cnt)) i += cnt;
return i;
}
int main ()
{
f >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
{
f >> c;
if (c == 'D') Q.push (make_pair (i, j)), a[i][j] = 1;
else
if (c == 'I') start = make_pair (i, j);
else
if (c == 'O') finish = make_pair (i, j);
else
if (c == '*') a[i][j] = -1;
}
BFs ();
/*for (int i = 1; i <= n; ++i, g << '\n')
for (int j = 1; j <= m; ++j)
g << a[i][j] << ' ';*/
int val = bs () - 1;
g << val << '\n';
g.close ();
return 0;
}