Cod sursa(job #2005052)

Utilizator AndreiSorin26012001Cirpici Andrei Sorin AndreiSorin26012001 Data 26 iulie 2017 14:20:33
Problema Barbar Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.01 kb
#include <bits/stdc++.h>
 
using namespace std;
 
int arr[1004][1004];
int aux[1004][1004];
int r, c;
 
int dx[4] = {-1, 0, 1,  0};
int dy[4] = { 0, 1, 0, -1};
 
bool OK(int x, int y){
    if(x < 0 || y < 0 || x >= r || y >= c){
        return false;
    }
 
    if(arr[x][y] == -1 || arr[x][y] >= 1){
        return false;
    }
 
    return true;
}
 
bool OK2(int x, int y){
    if(x < 0 || y < 0 || x >= r || y >= c){
        return false;
    }
 
    if(aux[x][y] == -1 || aux[x][y] > 0){
        return false;
    }
 
    return true;
}
 
void construireDrum(int t){
    for(int i = 0; i < r;i++){
        for(int j = 0; j < c; j++){
            if(arr[i][j] - 1 < t && arr[i][j] >= 0){
                aux[i][j] = -1;
            }
            if(arr[i][j] == -1){
                aux[i][j] = -1;
            }
        }
    }
}
 
void demolareDrum(){
    for(int i = 0; i < r;i++){
        for(int j = 0; j < c; j++){
            aux[i][j] = 0;
        }
    }
}
 
void lee(queue< pair<int, int> >& coada, int no){
 
    int x_actual, y_actual, x_urmator, y_urmator;
 
    while( !coada.empty() ){
 
        x_actual = coada.front().first;
        y_actual = coada.front().second;
        coada.pop();
 
        for(int d = 0; d < 4; d++){
 
            x_urmator = x_actual + dx[d];
            y_urmator = y_actual + dy[d];
 
            if(no == 1){
                if( OK(x_urmator, y_urmator) ){
                    coada.push( make_pair(x_urmator, y_urmator) );
                    arr[x_urmator][y_urmator] = arr[x_actual][y_actual] + 1;;
                }
            } else {
                if( OK2(x_urmator, y_urmator) ){
                    coada.push( make_pair(x_urmator, y_urmator) );
                    aux[x_urmator][y_urmator] = aux[x_actual][y_actual] + 1;;
                }
            }
        }
 
    }
 
 
}
 
int main()
{
    ifstream in("barbar.in");
    ofstream out("barbar.out");
 
 
    in>>r>>c;
    char ch;
    int sx, sy, fx, fy;
 
    queue< pair<int, int> > dragoni;
 
    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            in>>ch;
            if(ch == '.'){
                arr[i][j] = 0;
            } else if(ch == '*'){
                arr[i][j] = -1;
            } else if(ch == 'D'){
                arr[i][j] = 1;
                dragoni.push( make_pair(i, j) );
            } else if(ch == 'I'){
                sx = i; sy = j;
            } else {
                fx = i; fy = j;
            }
        }
    }
 
 
    lee(dragoni, 1);
 
    int t = arr[sx][sy];
    bool found = false;
 
    queue< pair<int, int> > drum;
 
    while( !found && t >= -1){
 
        construireDrum(t);
 
        drum.push( make_pair(sx, sy) );
        lee(drum, 2);
 
        if(aux[fx][fy] > 0){
            found = true;
 
        } else {
            t--;
            demolareDrum();
        }
    }
 
 
 
	if(t > 0){
		out<<t;
	} else {
		out<<-1;
	}
 
    return 0;
}