Pagini recente » Cod sursa (job #1750632) | Cod sursa (job #3194711) | Cod sursa (job #2080058) | Cod sursa (job #2934661) | Cod sursa (job #1350127)
#include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;
#define Nmax 1002
#define inf 0x3f3f3f3f
#define mp make_pair
#define x first
#define y second
FILE *f = fopen ( "barbar.in", "r" );
FILE *g = fopen ( "barbar.out", "w" );
queue < pair < int , int > > Q;
pair < int , int > start, finish ,aux;
int N, M, cost[Nmax][Nmax];
const int d[4][2] = { {-1,0}, {0,1}, {1,0}, {0,-1} };
bool inside ( int x, int y ){
if ( x > 0 && y > 0 && x <= N && y <= M )
return 1;
return 0;
}
void BFS (){
while ( !Q.empty() ){
aux = Q.front ();
Q.pop();
for ( int i = 0; i < 4; ++i ){
int xx = aux.x + d[i][0];
int yy = aux.y + d[i][1];
if ( inside ( xx, yy ) && cost[xx][yy] > cost[aux.x][aux.y] + 1 ){
Q.push ( mp ( xx, yy ) );
cost[xx][yy] = cost[aux.x][aux.y] + 1;
}
}
}
}
bool viz[Nmax][Nmax];
bool isRoad ( int k ){
Q.push ( start );
memset ( viz, 0, sizeof ( viz ) );
while ( !Q.empty() ){
aux = Q.front ();
Q.pop();
for ( int i = 0; i < 4; ++i ){
int xx = aux.x + d[i][0];
int yy = aux.y + d[i][1];
if ( !viz[xx][yy] && inside ( xx, yy ) && cost[xx][yy] >= k ){
Q.push ( mp ( xx, yy ) );
viz[xx][yy] = 1;
}
}
}
return viz[finish.x][finish.y];
}
int main (){
char c;
fscanf ( f, "%d%d%*c", &N, &M );
for ( int i = 1; i <= N; ++i ){
for ( int j = 1; j <= M; ++j ){
fscanf ( f, "%c", &c );
switch ( c ){
case '.':
cost[i][j] = inf;
break;
case '*':
cost[i][j] = -1;
break;
case 'D':
cost[i][j] = 0;
Q.push ( mp ( i, j ) );
break;
case 'I':
start.x = i;
start.y = j;
break;
case 'O':
cost[i][j] = inf;
finish.x = i;
finish.y = j;
break;
}
}
fscanf ( f, "%*c" );
}
BFS ();
int st = 1, dr = max ( cost[start.x][start.y], cost[finish.x][finish.y] );
int mid, sol;
bool exist = 0;
while ( st <= dr ){
mid = ( st + dr ) >> 1;
if ( isRoad ( mid ) ){
sol = mid;
exist = 1;
st = mid + 1;
}
else
dr = mid - 1;
}
if ( !exist )
fprintf ( g, "-1" );
else
fprintf ( g, "%d", sol );
return 0;
}