Pagini recente » Cod sursa (job #2909407) | Cod sursa (job #2966181) | Cod sursa (job #1327674) | Cod sursa (job #2622987) | Cod sursa (job #3144348)
#include <fstream>
#include <queue>
using namespace std;
ifstream in;
ofstream out;
char ti[1005][1005], t[1005][1005]; bool aux[1005][1005];
int n, m;
pair<int, int> dir[] = { {-1,0},{1,0},{0,-1},{0,1} };
void set(char setme[1005][1005], char to[1005][1005])
{
for (int i = 0; i < 1005; i++)
{
for (int j = 0; j < 1005; j++)
{
setme[i][j] = to[i][j];
}
}
}
void set(bool setme[1005][1005], bool to)
{
for (int i = 0; i < 1005; i++)
{
for (int j = 0; j < 1005; j++)
{
setme[i][j] = to;
}
}
}
bool isInside(int l, int c)
{
return l >= 0 && c >= 0 && l < n && c < m;
}
bool isInside(pair<int,int> x)
{
return x.first >= 0 && x.second >= 0 && x.first < n && x.second < m;
}
void expand(int amount)
{
set(aux, 0);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (t[i][j] == 'D')
{
for (int l = i - amount; l < i + amount; l++)
{
for (int c = j - amount + abs(i - l); c < j + amount - abs(i - l); c++)
{
if (isInside(l, c))
{
aux[l][c] = 1;
}
}
}
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (aux[i][j] == 1)
t[i][j] = 'F';
}
}
}
pair<int, int> plu(pair<int, int> a, pair<int, int> b)
{
return { a.first + b.first, a.second + b.second };
}
bool canfindway(int startx, int starty)
{
set(aux, 0);
queue<pair<int, int>> q;
aux[startx][starty] = 1;
q.push({ startx, starty });
while (!q.empty())
{
auto x = q.front();
for (int i = 0; i < 4; i++)
{
auto c = plu(x, dir[i]);
if (isInside(c) && !aux[c.first][c.second])
{
if (t[c.first][c.second] == 'O')
return 1;
else if (t[c.first][c.second] == '.')
{
aux[c.first][c.second] = 1;
q.push(c);
}
}
}
q.pop();
}
return 0;
}
int main()
{
in.open("barbar.in");
out.open("barbar.out");
in >> n >> m;
int startx, starty;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
in >> ti[i][j];
if (ti[i][j] == 'I')
{
startx = i;
starty = j;
}
}
}
int st = 0, dr = 2005, mij, r = -1;
while (st <= dr)
{
mij = (st + dr) / 2;
set(t, ti);
expand(mij);
if (canfindway(startx, starty) && t[startx][starty] == 'I')
{
r = mij;
st = mij + 1;
}
else
dr = mij - 1;
}
out << r+1;
}