Pagini recente » Cod sursa (job #1504527) | Cod sursa (job #570262) | Cod sursa (job #3265223) | Cod sursa (job #2519571) | Cod sursa (job #2958519)
#include <fstream>
#include <queue>
#include <climits>
#include <cstring>
using namespace std;
ifstream fin("barbar.in");
ofstream fout("barbar.out");
const short vec_len = 1005;
struct pos
{
short l;
short c;
};
short n, m;
short dmap[vec_len][vec_len];
short dirl[] = {-1, 0, 1, 0}, dirc[] = {0, 1, 0, -1};
pos tstart, texit;
queue<pos> drag;
queue<pos> path;
bool check(short l, short c)
{
if (l < 1 || l > n)
return false;
if (c < 1 || c > m)
return false;
if (dmap[l][c] == -1)
return false;
return true;
}
void dragLee()
{
bool vis[vec_len][vec_len] = {false};
while (!drag.empty())
{
pos curr = drag.front();
vis[curr.l][curr.c] = true;
drag.pop();
short nl = curr.l, nc = curr.c;
for (short i = 0; i < 4; i++)
{
nl = curr.l + dirl[i];
nc = curr.c + dirc[i];
if (check(nl, nc) && !vis[nl][nc])
{
dmap[nl][nc] = dmap[curr.l][curr.c] + 1;
drag.push(pos{nl, nc});
}
}
}
}
bool pathLee(short val)
{
bool vis[vec_len][vec_len] = {false};
while (!path.empty())
path.pop();
path.push(pos{tstart.l, tstart.c});
while (!path.empty())
{
pos curr = path.front();
path.pop();
short nl, nc;
for (short i = 0; i < 4; i++)
{
nl = curr.l + dirl[i];
nc = curr.c + dirc[i];
if (check(nl, nc) && !vis[nl][nc] && dmap[nl][nc] >= val)
{
if (nl == texit.l && nc == texit.c)
return true;
path.push(pos{nl, nc});
vis[nl][nc] = true;
}
}
}
return false;
}
int main()
{
fin.tie(NULL);
fout.tie(NULL);
ios::sync_with_stdio(false);
fin >> n >> m;
char x;
for (short i = 1; i <= n; i++)
for (short j = 1; j <= m; j++)
{
fin >> x;
if (x == 'D')
drag.push(pos{i, j}), dmap[i][j] = 0;
else if (x == 'I')
tstart = pos{i, j};
else if (x == '*')
dmap[i][j] = -1;
else if (x == 'O')
texit = pos{i, j};
}
dragLee();
if (dmap[tstart.l][tstart.c] == INT8_MAX)
{
fout << -1;
return 0;
}
short left = 1, right = max(n, m);
while (left < right)
{
short mid = (left + right) / 2;
bool res = pathLee(mid);
if (!res)
right = mid;
else
left = mid + 1;
}
if (pathLee(right))
fout << right;
else
fout << right - 1;
return 0;
}