Pagini recente » Cod sursa (job #2863036) | Cod sursa (job #1469688) | Cod sursa (job #2113787) | Cod sursa (job #2898042) | Cod sursa (job #2552252)
#include <fstream>
#include <deque>
using namespace std;
ifstream cin("barbar.in");
ofstream cout("barbar.out");
int n,m,xs,ys,xf,yf,d[1005][1005];
string s[1005];
deque<int>ql,qc;
const int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
bool a[1005][1005];
bool Lee(int mij){
int i,j,ii,jj;
ql.push_front(xs);
qc.push_front(ys);
a[xs][ys]=true;
while(!ql.empty()){
i=ql.front();
j=qc.front();
/*if(i==xf && j==yf){
while(!ql.empty()){
ql.pop_front();
qc.pop_front();
}
return true;
}
*/
ql.pop_front();
qc.pop_front();
for(int k=0;k<4;k++){
ii=i+dx[k];
jj=j+dy[k];
if(ii>=0 && ii<n && jj>=0 && jj<m ){
if(d[ii][jj]>=mij && s[ii][jj]!='*' && a[ii][jj]==false){
ql.push_front(ii);
qc.push_front(jj);
a[ii][jj]=true;
}
}
}
}
return a[xf][yf];
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
d[i][j]=(1<<20);
}
}
for(int i=0;i<n;i++){
cin>>s[i];
for(int j=0;j<m;j++){
if(s[i][j]=='D'){
ql.push_back(i);
qc.push_back(j);
d[i][j]=0;
}
if(s[i][j]=='I'){
xs=i;
ys=j;
}
if(s[i][j]=='O'){
xf=i;
yf=j;
}
}
}
int i,j,ii,jj;
while(!ql.empty()){
i=ql.front();
j=qc.front();
ql.pop_front();
qc.pop_front();
for(int k=0;k<4;k++){
ii=i+dx[k];
jj=j+dy[k];
if(ii>=0 && ii<n && jj>=0 && jj<m ){
if(d[ii][jj]>d[i][j]+1 && s[ii][jj]!='*'){
d[ii][jj]=d[i][j]+1;
ql.push_front(ii);
qc.push_front(jj);
}
}
}
}
/* for(int i=0;i<n;i++,cout<<'\n'){
for(int j=0;j<m;j++,cout<<" "){
cout<<d[i][j];
}
}
*/
bool ok=false;
int sol=0;
int st=0,dr=n+m;
while(st<=dr){
int mj=(st+dr)/2;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
a[i][j]=false;
}
}
if(Lee(mj)==true) {
dr=mj-1;
}
else{
ok=true;
st=mj+1;
sol=mj;
}
}
if(ok==false){
cout<<-1;
return 0;
}
cout<<sol/10;
return 0;
}