Pagini recente » Cod sursa (job #3162358) | Cod sursa (job #3278360) | Cod sursa (job #143135) | Cod sursa (job #228358) | Cod sursa (job #3276939)
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
ifstream fin("barbar.in");
ofstream fout("barbar.out");
int n, m, tx, ty, sx, sy;
char mat[1001][1001];
int price[1001][1001];
queue<pair<int, int>> q, qq;
vector<pair<int, int>> v;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
bool cmp(pair<int, int> a, pair<int, int> b)
{
return price[a.first][a.second] < price[b.first][b.second];
}
void dragon()
{
while (!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < n && ny < m && mat[nx][ny] != '#' && mat[nx][ny] != 'D' && price[nx][ny] == 0)
{
q.push({nx, ny});
price[nx][ny] = price[x][y] + 1;
}
}
}
}
int lee(int ts)
{
int cost[1001][1001];
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 1000; j++)
{
cost[i][j] = 0;
}
}
while (!qq.empty())
{
qq.pop();
}
qq.push({sx, sy});
while (!qq.empty())
{
int x = qq.front().first;
int y = qq.front().second;
qq.pop();
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < n && ny < m && (mat[nx][ny] == '.' || mat[nx][ny] == 'O') && cost[nx][ny] == 0 && price[nx][ny] >= ts)
{
cost[nx][ny] = cost[x][y] + 1;
if (tx == nx && ty == ny)
{
// cout << tx << "x" << ty;
return 1;
}
qq.push({nx, ny});
}
}
}
return 0;
}
int main()
{
fin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
fin >> mat[i][j];
if (mat[i][j] == 'D')
q.push({i, j});
else if (mat[i][j] == 'O')
{
tx = i;
ty = j;
// price[i][j] = 1000000;
}
else if (mat[i][j] == 'I')
{
// price[i][j] = 1000000;
// v.push_back({i, j});
sx = i;
sy = j;
}
}
}
dragon();
int st = 0;
int dr = 1000000;
int mij = (st + dr) / 2;
while (st <= dr)
{
mij = (st + dr) / 2;
if (lee(mij))
{
// cout << mij << "<";
st = mij + 1;
}
else
{
dr = mij - 1;
}
// cout << mij << " ";
}
// cout << '\n'
// << dr;
// cout << lee(3) << '\n';
if (st == 0)
{
fout << -1;
}
else
fout << dr;
// for (int i = 0; i < n; i++)
// {
// for (int j = 0; j < m; j++)
// {
// fout << price[i][j] << " ";
// }
// fout << '\n';
// }
// fout << '\n';
// for (int i = 0; i < n; i++)
// {
// for (int j = 0; j < m; j++)
// {
// // fout << cost[i][j] << " ";
// }
// // fout << '\n';
// }
return 0;
}