Pagini recente » Cod sursa (job #582039) | Cod sursa (job #973680) | Cod sursa (job #781100) | Cod sursa (job #2587333) | Cod sursa (job #2793411)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("barbar.in");
ofstream fout ("barbar.out");
const int INF = (1<<30);
const int MAXN = 1005;
vector <pair <int, int>> dragons;
int closestDragon[MAXN][MAXN], freq[MAXN][MAXN], best[MAXN][MAXN];
char mat[MAXN][MAXN]; string row;
int n, m, sol;
void debug(int test[MAXN][MAXN]){
for(int i=1; i<=n; i++, cout<<"\n")
for(int j=1; j<=m; j++, cout<<" ")
if(mat[i][j] == '*')
cout<<"*";
else
cout<<test[i][j];
}
struct End{
int ii;
int jj;
}in, out;
struct cell{
int ii;
int jj;
int dd;
}aux, nod;
int di[] = {+1, -1, 0, 0};
int dj[] = { 0, 0, +1, -1};
void bfs1(){
queue <cell> q;
for(int i=0; i<dragons.size(); i++){
aux = {dragons[i].first, dragons[i].second, 0};
q.push(aux);
closestDragon[aux.ii][aux.jj] = 0;
}
while(!q.empty()){
nod = q.front();
q.pop();
if(nod.dd != closestDragon[nod.ii][nod.jj])
continue;
for(int dir=0; dir < 4; dir++){
int newi = nod.ii + di[dir];
int newj = nod.jj + dj[dir];
if(1 <= newi && newi <= n && 1 <= newj && newj <= m)
if(mat[newi][newj] != '*')
if(closestDragon[newi][newj] == INF){
closestDragon[newi][newj] = nod.dd + 1;
q.push({newi, newj, nod.dd+1});
}
}
}
}
void bfs2(int is, int js){
queue <cell> q;
best[is][js] = closestDragon[is][js];
aux = {is, js, closestDragon[is][js]};
q.push(aux);
while(!q.empty()){
nod = q.front();
q.pop();
if(nod.dd < sol)
continue;
if(nod.ii == out.ii && nod.jj == out.jj){
sol = max(sol, nod.dd);
continue;
}
for(int dir=0; dir < 4; dir++){
int newi = nod.ii + di[dir];
int newj = nod.jj + dj[dir];
if(1 <= newi && newi <= n && 1 <= newj && newj <= m)
if(mat[newi][newj] != '*')
if(best[newi][newj] < nod.dd){
best[newi][newj] = nod.dd;
q.push({newi, newj, min(nod.dd, closestDragon[newi][newj])});
}
}
}
}
int main (){
fin>>n>>m;
for(int i=1; i<=n; i++){
fin>>row;
for(int j=0; j<m; j++){
mat[i][j+1] = row[j];
closestDragon[i][j+1] = INF;
best[i][j+1] = -1;
}
}
int color;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++){
if(mat[i][j] == 'D')
dragons.push_back({i, j});
if(mat[i][j] == 'I'){
in.ii=i;
in.jj=j;
}
if(mat[i][j] == 'O'){
out.ii=i;
out.jj=j;
}
}
bfs1(); //debug(closestDragon);
sol=0;
bfs2(in.ii, in.jj);
fout<<sol;
return 0;
}