Pagini recente » Cod sursa (job #3172697) | Cod sursa (job #2967430) | Cod sursa (job #1938266) | Cod sursa (job #2406203) | Cod sursa (job #2332929)
#include <iostream>
#include <fstream>
#include <queue>
#define fi first
#define se second
using namespace std;
const int Maxx=1e3+1;
const int dx[]={-1,0,1,0};
const int dy[]={0,1,0,-1};
struct POINT{
int x,y;
}nw,ac,str,stp;
queue <POINT> Q;
ifstream fin("barbar.in");
ofstream fout("barbar.out");
int n,m,i,j,val=-1,ans=0;
int dist[Maxx][Maxx];
bool A[Maxx][Maxx];
char a;
bool test(POINT a);
bool lee(int val),ok=false;
int main() {
fin>>n>>m;
for (i=1;i<=n;++i){
for (j=1;j<=m;++j){
fin>>a;
if (a=='I'){
str.x=i;
str.y=j;
}
if (a=='O'){
stp.x=i;
stp.y=j;
}
if (a=='*'){
dist[i][j]=-1;
}
if (a=='D'){
A[i][j]=-1;
Q.push({i,j});
dist[i][j]=1;
}
}
}
int d;
while (!Q.empty()){
ac=Q.front();
Q.pop();
for (d=0;d<4;++d){
nw.x=ac.x+dx[d];
nw.y=ac.y+dy[d];
if (test(nw) && dist[nw.x][nw.y]==0){
dist[nw.x][nw.y]=dist[ac.x][ac.y]+1;
val=max(val,dist[nw.x][nw.y]);
Q.push(nw);
}
}
}
for (i=1;i<=n;++i) for (j=1;j<=m;++j) if (dist[i][j]!=-1) --dist[i][j];
for (i=19;i>=0;--i){
if (ans+(1<<i)<=10000 && lee(ans+(1<<i))){
ans+=(1<<i);
}
}
fout<<ans;
return 0;
}
bool test1(pair <int,int> f){
return f.fi>0 && f.fi<=n && f.se>0 && f.se<=m;
}
bool test(POINT a){
return a.x>0 && a.x<=n && a.y>0 && a.y<=m;
}
bool lee(int val){
if (dist[str.x][str.y]<val) return 0;
for (int i=1;i<=n;++i) for (int j=1;j<=m;++j) A[i][j]=0;
Q.push(str);
A[str.x][str.y]=1;
int d;
while (!Q.empty()){
ac=Q.front();
Q.pop();
//fout<<ac.x<<" "<<ac.y<<"\n";
for (d=0;d<4;++d){
nw.x=ac.x+dx[d];
nw.y=ac.y+dy[d];
if (test(nw) && dist[nw.x][nw.y]>=val && !A[nw.x][nw.y] && dist[nw.x][nw.y]!=-1){
if (nw.x==stp.x && nw.y==stp.y){
while (!Q.empty()) Q.pop();
return 1;
}
A[nw.x][nw.y]=1;
Q.push(nw);
}
}
}
return 0;
}