Pagini recente » Cod sursa (job #1562739) | Cod sursa (job #2133620) | Cod sursa (job #2555497) | Cod sursa (job #2533000) | Cod sursa (job #2908699)
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("barbar.in");
ofstream fout("barbar.out");
int dirlin[]={0, 1, 0, -1};
int dircol[]={-1, 0, 1, 0};
int mat[1005][1005], mat_dr[1005][1005];
int n, m;
bool verif(int x, int y)
{
return x>=1 && x<=n && y>=1 && y<=m;
}
struct Coord
{
int row;
int col;
};
Coord plecare, sfarsit;
queue <Coord> leeQueue, drQueue;
bool lee(int xinceput, int yinceput, int k, int xfin, int yfin)
{
Coord start;
start.row=xinceput;
start.col=yinceput;
mat[start.row][start.col]=1;
leeQueue.push(start);
while(!leeQueue.empty())
{
Coord frn;
frn=leeQueue.front();
leeQueue.pop();
for(int i=0; i<4; i++)
{
Coord ngb;
ngb.row=frn.row+dirlin[i];
ngb.col=frn.col+dircol[i];
if(verif(ngb.row, ngb.col) && mat[ngb.row][ngb.col]==0 && mat_dr[ngb.row][ngb.col]>k)
{
leeQueue.push(ngb);
mat[ngb.row][ngb.col]=mat[frn.row][frn.col]+1;
}
}
}
if(mat[xfin][yfin]==0)
return false;
else
return true;
}
int cautbin(int st, int dr)
{
int ans=-1;
while(st<=dr)
{
int mij=(st+dr)/2;
if(lee(plecare.row, plecare.col, mij, sfarsit.row, sfarsit.col))
{
st=mij+1;
ans=mij;
}
else
dr=mij-1;
}
return ans;
}
void lee_dragoni()
{
while(!drQueue.empty())
{
Coord frn;
frn=drQueue.front();
drQueue.pop();
for(int i=0; i<4; i++)
{
Coord ngb;
ngb.row=frn.row+dirlin[i];
ngb.col=frn.col+dircol[i];
if(verif(ngb.row, ngb.col) && mat[ngb.row][ngb.col]==0 && mat_dr[ngb.row][ngb.col]==0)
{
drQueue.push(ngb);
mat_dr[ngb.row][ngb.col]=mat_dr[frn.row][frn.col]+1;
}
}
}
}
int main()
{
fin>>n>>m;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
char c;
fin>>c;
if(c=='*')
{
mat[i][j]=-1;
}
else if(c=='D')
{
mat[i][j]=-1;
mat_dr[i][j]=1;
Coord dragon;
dragon.row=i;
dragon.col=j;
drQueue.push(dragon);
}
else if(c=='I')
{
mat[i][j]=1;
plecare.row=i;
plecare.col=j;
}
else if(c=='O')
{
sfarsit.row=i;
sfarsit.col=j;
}
}
lee_dragoni();
/*for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
fout<<mat_dr[i][j]<<" ";
fout<<'\n';
}*/
fout<<cautbin(1, n*m);
return 0;
}