Pagini recente » Cod sursa (job #1652352) | Cod sursa (job #1566671) | Cod sursa (job #36418) | Cod sursa (job #296823) | Cod sursa (job #2940396)
#include <bits/stdc++.h>
using namespace std;
ifstream in("barbar.in");
ofstream out("barbar.out");
int n,m;
char a[1005][1005];
queue<pair<int,int>>q;
int dl[] = {-1,0,1,0};
int dc[] = {0,1,0,-1};
bool viz[1005][1005];
int d[1005][1005];
bool mat[1005][1005];
int d2[1005][1005];
int fl,fc,il,ic;
void BFS()
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
d[i][j] = 1e9;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == 'D')
q.push({i,j}),d[i][j] = 0,viz[i][j] = true;
while (!q.empty())
{
int lin = q.front().first;
int col = q.front().second;
q.pop();
for (int i = 0; i < 4; i++)
{
int vecl = lin + dl[i];
int vecc = col + dc[i];
if (!viz[vecl][vecc])
{
d[vecl][vecc] = d[lin][col] + 1;
q.push({vecl,vecc});
viz[vecl][vecc] = true;
}
}
}
}
bool ok(int dist)
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
if (d[i][j] >= dist)
mat[i][j] = true;
else
mat[i][j] = false;
}
q.push({il,ic});
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
viz[i][j] = false,d2[i][j] = 1e9;
viz[il][ic] = true;
d2[il][ic] = 0;
while (!q.empty())
{
int lin = q.front().first;
int col = q.front().second;
q.pop();
for (int i = 0; i < 4; i++)
{
int vecl = lin + dl[i];
int vecc = col + dc[i];
if (!viz[vecl][vecc] and mat[vecl][vecc])
{
d2[vecl][vecc] = 1 + d2[lin][col];
viz[vecl][vecc] = true;
q.push({vecl,vecc});
}
}
}
if (d2[fl][fc] == 1e9)
return false;
else
return true;
}
int main()
{
cout << 1;
in >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
in >> a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == 'I')
il = i,ic = j;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == 'O')
fl = i,fc = j;
BFS();
int st = -1,pas = 1 << 10;
while (pas != 0)
{
if (ok(st + pas) == true)
st += pas;
pas /= 2;
}
out << st;
return 0;
}