Pagini recente » Cod sursa (job #3365948) | Cod sursa (job #3365947) | Cod sursa (job #3365990) | Cod sursa (job #3365946) | Cod sursa (job #3365943)
#include <iostream>
#include <queue>
using namespace std;
//ifstream cin("barbar.in");
//ofstream cout("barbar.out");
const int dim= 1e3+ 5;
string s;
char a[dim][dim];
int viz[dim][dim];
int dist[dim][dim];
int xi, yi, xj, yj;
queue <pair<int, int>> q;
int dx[10]= {0, 0, 1, -1};
int dy[10]= {-1, 1, 0, 0};
int n, m;
bool in_mat(int x, int y){
return (1 <= x and 1 <= y and x <= n and y <= n);
}
void calc_dist(){
while(!q.empty()){
pair<int, int> nod= q.front();
q.pop();
for(int d= 0;d < 4;d++){
int x_nou= nod.first+ dx[d];
int y_nou= nod.second+ dy[d];
if(viz[x_nou][y_nou]== 0 and in_mat(x_nou, y_nou)== true and (a[x_nou][y_nou] != 'D' and a[x_nou][y_nou] != '*')){
dist[x_nou][y_nou]= 1+ dist[nod.first][nod.second];
q.push({x_nou, y_nou});
viz[x_nou][y_nou]= 1;
}
}
}
}
void lee(int mini){
for(int i= 1;i <= n;i++)
for(int j= 1;j <= m;j++)
viz[i][j]= 0;
viz[xi][yi]= 1;
q.push({xi, yi});
while(!q.empty()){
pair <int, int> nr= q.front();
q.pop();
for(int d= 0;d < 4;d++){
int x= dx[d]+ nr.first;
int y= dy[d]+ nr.second;
if(in_mat(x, y)== true and viz[x][y]== 0 and a[x][y] != 'D' and a[x][y] != '*' and dist[x][y] >= mini){
viz[x][y]= 1;
q.push({x, y});
}
}
}
}
int main()
{
int i, j, x1, y1, x2, y2, x, y;
cin >> n>> m;
for(i= 1;i <= n;i++){
cin >> s;
for(j= 1;j <= m;j++){
a[i][j]= s[j- 1];
if(a[i][j]== 'D')
q.push({i, j}), viz[i][j]= 1;
if(a[i][j]== 'I')
xi= i, yi= j;
if(a[i][j]== 'O')
xj= i, yj= j;
}
}
calc_dist();
for(i= 1;i <= n;i++){
for(j= 1;j <= m;j++)
cout << dist[i][j]<<" ";
cout << endl;
}
int st= 0, dr= n* 2, rez= 0;
while(st <= dr){
int mij= (st+ dr)/ 2;
lee(mij);
int ok= viz[xj][yj];
if(ok== 1)
rez= mij, st= mij+ 1;
else dr= mij- 1;
}
cout << rez;
return 0;
}