Pagini recente » Cod sursa (job #4654) | Cod sursa (job #196096) | Cod sursa (job #2545810) | Cod sursa (job #3189850) | Cod sursa (job #2177999)
#include <bits/stdc++.h>
using namespace std;
ifstream in("barbar.in");
ofstream out("barbar.out");
int n, m, sx, sy, st, dr, mid, fx, fy;
int harta[1005][1005], lee[1005][1005];
char ch;
queue< pair<int, int> > coada;
int x, y, x_next, y_next;
int dx[4] = { -1, 0, 0, 1 };
int dy[4] = { 0, -1, 1, 0 };
void golireDrum()
{
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= m; j++ )
if( lee[i][j] != -1 )
lee[i][j] = 0;
}
bool OK( int px, int py, int val )
{
if( px < 1 || py < 1 || px > n || py > m )
return false;
if( lee[px][py] != 0 )
return false;
if( harta[px][py] < val )
return false;
return true;
}
void Lee( int val )
{
queue< pair<int, int> > c;
c.push( make_pair( sx, sy ) );
lee[sx][sy] = 1;
while( !c.empty() )
{
x = c.front().first;
y = c.front().second;
c.pop();
for( int d = 0; d <= 3; d++ )
{
x_next = x + dx[d];
y_next = y + dy[d];
if( OK( x_next, y_next, val ) )
{
lee[x_next][y_next] = lee[x][y] + 1;
c.push( make_pair(x_next, y_next) );
}
}
}
}
int main(){
in>>n>>m;
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= m; j++ )
{
in>>ch;
switch( ch )
{
case '*': harta[i][j] = lee[i][j] = -1; break;
case 'I': sx = i;
sy = j;
break;
case 'D': harta[i][j] = 1;
coada.push( make_pair(i, j) );
break;
case 'O': fx = i;
fy = j;
break;
}
}
while( !coada.empty() )
{
x = coada.front().first;
y = coada.front().second;
coada.pop();
for( int d = 0; d <= 3; d++ )
{
x_next = x + dx[d];
y_next = y + dy[d];
if( x_next >= 1 && y_next >= 1 && x_next <= n && y_next <= m && harta[x_next][y_next] == 0 )
{
harta[x_next][y_next] = harta[x][y] + 1;
coada.push( make_pair(x_next, y_next) );
}
}
}
st = 1;
dr = max(n, m);
while( st <= dr )
{
mid = (st + dr)/2;
Lee( mid );
if( lee[n][m] > 0 )
st = mid + 1;
else
dr = mid - 1;
golireDrum();
}
out<<dr;
return 0;
}