Pagini recente » Cod sursa (job #638033) | Cod sursa (job #1117328) | Cod sursa (job #2775307) | Cod sursa (job #3131409) | Cod sursa (job #3138117)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("barbar.in");
ofstream fout("barbar.out");
struct Coord{
int lin, col;
}dragon, start, finish;
queue<Coord>q;
int matDragon[1005][1005], matPaftenie[1005][1005], n, m;
int dir[4][2]={{-1, 0}, {0, +1}, {+1, 0}, {0, -1}};
bool Lee(int mat[][1005], int dist, Coord dest){
while(!q.empty()){
Coord curent=q.front();
q.pop();
for(int d=0; d<4; d++){
Coord vecin;
vecin.lin = curent.lin+dir[d][0];
vecin.col = curent.col+dir[d][1];
if(vecin.lin<=n && vecin.col<=m && vecin.lin>=1 && vecin.col>=1 && mat[vecin.lin][vecin.col]==0 && matDragon[vecin.lin][vecin.col]>=dist){
q.push(vecin);
mat[vecin.lin][vecin.col]=mat[curent.lin][curent.col]+1;
}
}
}
return mat[dest.lin][dest.col] > 0;
}
void InitMatPaftenie(Coord start) {
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
matPaftenie[i][j]=0;
matPaftenie[start.lin][start.col] = 1;
q.push(start);
}
int BinSearch(int st, int dr){
int ans=0;
while(st<=dr){
int med=(st+dr)/2;
InitMatPaftenie(start);
if(Lee(matPaftenie, med, finish)==true){
st=med+1;
ans=med;
}else{
dr=med-1;
}
}
return ans;
}
int main()
{ fin>>n>>m;
string line;
getline(fin, line);
for(int i=1; i<=n; i++) {
getline(fin, line);
for(int j=1; j<=m; j++){
char x = line[j - 1];
if(x=='*') {
matDragon[i][j]=-1;
matPaftenie[i][j]=-1;
}
if(x=='D'){
matDragon[i][j]=1;
dragon.lin=i;
dragon.col=j;
q.push(dragon);
}
if(x=='I'){
start.lin=i;
start.col=j;
}
if(x=='O'){
finish.lin=i;
finish.col=j;
}
}
}
Lee(matDragon, 0, finish);
InitMatPaftenie(start);
/*for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++)
fout<<matDragon[i][j]<<" ";
fout<<"\n";
}
fout<<"\n";
*/
//InitMatPaftenie(start);
//Lee(matPaftenie, 3, finish);
/*for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++)
fout<<matPaftenie[i][j]<<" ";
fout<<"\n";
}
fout<<"\n";
*/
fout<<BinSearch(1, n*m)-1;
return 0;
}