Pagini recente » Cod sursa (job #2545812) | Cod sursa (job #1267570) | Cod sursa (job #2202406) | Cod sursa (job #719643) | Cod sursa (job #3237828)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("barbar.in");
ofstream fout("barbar.out");
const int NMAX=1000;
int a[NMAX+5][NMAX+5];
int n, m;
int lin[]={-1, 0 , 1, 0};
int col[]={0, 1, 0 , -1};
void lee(queue<pair<int,int>> q){
while(!q.empty()){
int nx=q.front().first, ny=q.front().second;
q.pop();
for(int i=0;i<=3;i++){
if(nx+lin[i]<=n and nx+lin[i]>=1 and ny+col[i]<=m and ny+col[i]>=1 and a[nx+lin[i]][ny+col[i]]==-1){
a[nx+lin[i]][ny+col[i]]=a[nx][ny]+1;
q.push({nx+lin[i], ny+col[i]});
}
}
}
}
bool lee2(int x1,int x2,int y1,int y2, int val){
int b[n+1][m+1];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
b[i][j]=0;
}
}
queue<pair<int,int> > q;
q.push({x1, y1});
b[x1][y1]=1;
while(!q.empty()){
int nx=q.front().first;
int ny=q.front().second;
q.pop();
if(nx==x2 and ny==y2){
return true;
}
for(int i=0;i<=3;i++){
if(nx+lin[i]<=n and nx+lin[i]>=1 and ny+col[i]<=m and ny+col[i]>=1 and a[nx+lin[i]][ny+col[i]]>=val and b[nx+lin[i]][ny+col[i]]==0){
b[nx+lin[i]][ny+col[i]]=1;
q.push({nx+lin[i], ny+col[i]});
}
}
}
return false;
}
int ans=0;
int main(){
int x1, x2, y1, y2;
fin>>n>>m;
queue<pair<int,int> > q;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
char val;
fin>>val;
if(val=='.'){
a[i][j]=-1;
}else if(val=='*'){
a[i][j]=-2;
}else if(val=='D'){
a[i][j]=0;
q.push({i,j});
}else if(val=='I'){
a[i][j]=-1;
x1=i, y1=j;
}else if(val=='O'){
a[i][j]=-1;
x2=i, y2=j;
}
}
}
lee(q);
int st=1, dr=1e6;
while(st<=dr){
int mij=(st+dr)/2;
if(lee2(x1,x2,y1,y2,mij)){
st=mij+1;
ans=mij;
}else{
dr=mij-1;
}
}
fout<<ans;
}