Pagini recente » Cod sursa (job #3192419) | Cod sursa (job #1401042) | Cod sursa (job #2359631) | Monitorul de evaluare | Cod sursa (job #2956612)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("barbar.in");
ofstream fout("barbar.out");
const int vec_len = 1001;
struct pos
{
int l;
int c;
};
int n, m;
vector<vector<int>> dmap(vec_len, vector<int>(vec_len, INT_MAX));
int dirl[] = {-1, 0, 1, 0}, dirc[] = {0, 1, 0, -1};
pos tstart, texit;
queue<pos> drag;
bool check(int l, int 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()
{
vector<vector<bool>> vis(vec_len, vector<bool>(vec_len, false));
while (!drag.empty())
{
pos curr = drag.front();
vis[curr.l][curr.c] = true;
drag.pop();
int nl = curr.l, nc = curr.c;
for (int 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] = min(dmap[nl][nc], dmap[curr.l][curr.c] + 1);
drag.push(pos{nl, nc});
}
}
}
}
void pathLee()
{
vector<vector<bool>> vis(vec_len, vector<bool>(vec_len, false));
int maxx = INT_MAX;
priority_queue<tuple<int, int, int>> path;
path.push(make_tuple(dmap[tstart.l][tstart.c], tstart.l, tstart.c));
while (!path.empty())
{
tuple<int, int, int> curr = path.top();
path.pop();
maxx = min(maxx, get<0>(curr));
if (get<1>(curr) == texit.l && get<2>(curr) == texit.c)
break;
int nl, nc;
for (int i = 0; i < 4; i++)
{
nl = get<1>(curr) + dirl[i];
nc = get<2>(curr) + dirc[i];
if (check(nl, nc) && !vis[nl][nc])
{
path.push(make_tuple(dmap[nl][nc], nl, nc));
vis[nl][nc] = true;
}
}
}
fout << (maxx == INT_MAX ? -1 : maxx);
}
void print()
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
cout << dmap[i][j] << " ";
cout << "\n";
}
}
int main()
{
fin >> n >> m;
char x;
for (int i = 1; i <= n; i++)
for (int 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();
pathLee();
return 0;
}