Pagini recente » Cod sursa (job #196321) | Cod sursa (job #205398) | Cod sursa (job #1016745) | Cod sursa (job #835701) | Cod sursa (job #2951553)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("barbar.in");
ofstream fout("barbar.out");
const int Nmax=1005;
char mat[Nmax][Nmax];
int distDrag[Nmax][Nmax],distOm[Nmax][Nmax];
queue<pair<int,int>> q;
int n,m;
int isGoodDrag(int a,int b)
{
return(a>=1 && b>=1 && a<=n && b<=m && distDrag[a][b]==-1 && mat[a][b]!='*');
}
int isGoodOm(int a,int b,int x)
{
return(a>=1 && b>=1 && a<=n && b<=m && distOm[a][b]==-1 && mat[a][b]!='*' && distDrag[a][b]>=x);
}
void LeeDragons()
{
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
while(!q.empty())
{
int i=q.front().first,j=q.front().second;
q.pop();
for(int k=0;k<4;k++)
{
///cout<<1;
if(isGoodDrag(i+dx[k],j+dy[k]))
{
q.push({i+dx[k],j+dy[k]});
distDrag[i+dx[k]][j+dy[k]]=distDrag[i][j]+1;
}
}
}
}
void LeeOm(int x)
{
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
while(!q.empty())
{
int i=q.front().first,j=q.front().second;
q.pop();
for(int k=0;k<4;k++)
{
///cout<<1;
if(isGoodOm(i+dx[k],j+dy[k],x))
{
q.push({i+dx[k],j+dy[k]});
distOm[i+dx[k]][j+dy[k]]=distOm[i][j]+1;
}
}
}
}
int main()
{
pair<int,int> start;
pair<int,int> finish;
fin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
fin>>mat[i][j];
distDrag[i][j]=-1;
distOm[i][j]=-1;
if(mat[i][j]=='I')
{
start={i,j};
}
if(mat[i][j]=='D')
{
q.push({i,j});
distDrag[i][j]=0;
}
if(mat[i][j]=='O')
{
finish={i,j};
}
}
}
LeeDragons();
/*for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
fout<<distDrag[i][j]<<' ';
}
fout<<'\n';
}*/
int l=1,r,mid,sol;
r=min(distDrag[finish.first][finish.second],distDrag[start.first][start.second]);
while(l<r)
{
mid=(l+r)/2;
q.push(start);
distOm[start.first][start.second]=0;
LeeOm(mid);
if(distOm[finish.first][finish.second]==-1)
{
r=mid-1;
}
else
{
l=mid+1;
sol=mid;
}
}
fout<<sol;
return 0;
}