Pagini recente » Cod sursa (job #490118) | Cod sursa (job #45956) | Cod sursa (job #528959) | Cod sursa (job #183501) | Cod sursa (job #504325)
Cod sursa(job #504325)
#include<cstdio>
struct PUNCT { int x , y ; } ;
int dx [] = { 1 , 0 , -1 , 0 } ;
int dy [] = { 0 , 1 , 0 , -1 } ;
const int NMAX = 1<<10;
int n , m , p , u ;
int mat [ NMAX ] [ NMAX ] ;
bool matq [NMAX] [NMAX] ;
PUNCT inceput , sfarsit ;
PUNCT q [ NMAX*NMAX ] ;
void lee_dragon ( )
{
int c , xn , yn ;
// printf ( "LEE DRAGON : p - %d ; u - %d \n" , p , u ) ;
while ( p <= u )
{
for ( c = 0 ; c < 4 ; ++ c )
{
xn = q[p].x + dx[c] ;
yn = q[p].y + dy[c] ;
if ( mat[xn][yn] != -1 && ( mat[xn][yn] == 0 ) )
{
mat[xn][yn]=mat[q[p].x][q[p].y]+1;
q[++u].x = xn ;
q[u].y = yn ;
}
}
++p;
}
}
void init ( )
{
int i , j ;
for ( i = 1 ; i <= n ; ++ i )
for ( j = 1 ; j <= m ; ++ j )
matq[i][j]=false;
}
bool drum ( int damage ) //lee
{
int xn,yn,c;
p=u=1;
q[1] = inceput ;
bool ajuns = false;
init ( ) ;
while ( p <= u )
{
for ( c = 0 ; c < 4 ; ++ c )
{
xn = q[p].x + dx[c] ;
yn = q[p].y + dy[c] ;
if ( mat[xn][yn] >= damage )
if ( mat[xn][yn] != -1 && !matq[xn][yn] )
{
q[++u].x = xn ;
q[u].y = yn ;
if ( q[u].x == sfarsit.x && q[u].y == sfarsit.y )
return true ;
matq[xn][yn]=true;
}
}
++p;
//printf ( "DRUM ( damage - %d ) : p - %d , u - %d\n", damage , p , u ) ;
}
return false;
}
void afisare ( )
{
int i , j ;
for ( i = 1 ; i <= n ; ++ i )
{
for ( j = 1 ; j <= m ; ++ j )
printf ( "%2d ", mat[i][j] ) ;
printf ( "\n" ) ;
}
}
void caut ( )
{
int pas = 1 << 12 , i ;
for ( i = 0 ; pas != 0 ; pas >>= 1 )
if ( drum (i+pas) )
i+=pas;
if ( i == 0 )
printf ( "-1" ) ;
else
printf ( "%d ", i - 1) ;
}
void bordare ( )
{
int i , j ;
for ( i = 0 ; i <= n + 1 ; ++ i )
mat[i][0] = mat[i][m+1] = -1 ;
for ( j = 0 ; j <= m + 1 ; ++ j )
mat[0][j] = mat[n+1][j] = -1 ;
}
int main ( )
{
freopen ( "barbar.in", "r", stdin ) ;
//freopen ( "barbar.out", "w", stdout ) ;
int i , j ;
char s[NMAX];
scanf ( "%d%d\n", &n,&m ) ;
p=1;
u=0;
for ( i = 1; i <= n ; ++ i )
{
gets ( s ) ;
for ( j = 0 ; s[j] ; ++ j )
if ( s[j]=='D' )
{
q[++u].x = i ;
q[u].y = j+1;
mat[i][j+1]=1;
}
else
if ( s[j] == '*' )
{
mat[i][j+1]=-1;
}
else
if ( s[j] == 'I' )
{
inceput.x = i;
inceput.y = j+1;
}
else
if ( s[j] == 'O' )
{
sfarsit.x = i ;
sfarsit.y = j+1 ;
}
}
/*printf ( "%d\n", u ) ;
printf ( "%d %d\n" , sfarsit.x , sfarsit.y ) ;*/
bordare ( ) ;
lee_dragon ( ) ;
//afisare ( ) ;
caut ( ) ;
return 0;
}