Pagini recente » Cod sursa (job #3236061) | Cod sursa (job #1304459) | Cod sursa (job #387193) | Cod sursa (job #295210) | Cod sursa (job #3180403)
#include <bits/stdc++.h>
using namespace std;
ifstream in ("barbar.in");
ofstream out ("barbar.out");
queue <pair<int,int>> q;
const int NMAX=255;
int n,m;
int ys,xs,xf,yf;
int dx[]= {1,0,-1,0},dy[]= {0,1,0,-1};
char ch[NMAX];
int a[NMAX][NMAX],leel[NMAX][NMAX];
bool inbound(int i, int j)
{
return i>=1 && j>=1 && i<=n && j<=n;
}
void lee()
{
while(!q.empty())
{
int x=q.front().first;
int y=q.front().second;
q.pop();
for(int i=0; i<4; i++)
{
int l=x+dx[i];
int c=y+dy[i];
if(inbound(l,c) && a[l][c]!=-2 && (a[l][c]==-1 || a[l][c]>a[x][y]+1))
{
q.push(make_pair(l,c));
a[l][c]=a[x][y]+1;
}
}
}
}
bool check(int minim)
{
if(a[xs][ys]>minim)
return false;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
leel[i][j]=0;
queue <pair<int,int>> q;
q.push(make_pair(xs,ys));
leel[xs][ys]=1;
while(!q.empty())
{
int x=q.front().first;
int y=q.front().second;
q.pop();
for(int i=0; i<4; i++)
{
int l=x+dx[i];
int c=y+dy[i];
if(inbound(l,c) && a[l][c]<=minim && leel[l][c]==0)
leel[l][c]=leel[x][y]+1,q.push(make_pair(l,c));
}
}
if(leel[xf][yf])
return true;
return false;
}
int cautarebinara()
{
int st=1,dr=100000;
while(st<=dr)
{
int mij=(st+dr)/2;
if(check(mij))
{
dr=mij-1;
}
else
st=mij+1;
}
return st;
}
int main()
{
in>>n>>m;
for(int i=1; i<=n; i++)
{
in>>ch;
for(int j=0; j<strlen(ch); j++)
{
if(ch[j]=='.')
a[i][j+1]=-1;
else if(ch[j]=='*')
a[i][j+1]=-2;
else if(ch[j]=='I')
{
xs=i,ys=j+1;
}
else if(ch[j]=='O')
{
xf=i,yf=j+1;
}
else
{
a[i][j+1]=0;
q.push(make_pair(i,j+1));
}
}
}
lee();
out<<cautarebinara();
return 0;
}