Cod sursa(job #1025721)

Utilizator krissu93FMI Tiugan Cristiana Elena krissu93 Data 10 noiembrie 2013 14:50:24
Problema Barbar Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.92 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;


int n, m;
const int MAXN = 1001;
const int MAXM = 1001;
int dMax = 0;

int Ox[] = {0,0,1,-1};
int Oy[] = {1,-1,0,0};

char game[MAXN][MAXM];
int matrix[MAXN][MAXM];
bool viz[MAXN][MAXM];
queue < pair <int, int> > dragoni;
pair <int, int> pozInitiala;
pair <int, int> pozFinala;



void getData(){

ifstream in("barbar.in");

in>>n>>m;
for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++){
        char ch;
        in>>ch;

        switch(ch)
        {
            case '.' : game[i][j] = ch; break;
            case '*' : game[i][j] = ch; break;
            case 'D' : game[i][j] = ch; dragoni.push(pair <int, int> (i,j)); break;
            case 'I' : game[i][j] = ch; pozInitiala = pair<int, int> (i,j); break;
            case 'O' : game[i][j] = ch; pozFinala = pair<int, int> (i,j); break;
        }
    }

in.close();
}

bool in_Matrix(int i, int j){

if (i>=0 && i<n && j>=0 && j<m) return true;
else return false;

}

void LEE (){



 for(; !dragoni.empty();dragoni.pop()){

    pair<int, int> dragon = dragoni.front();

    for (int  i=0; i<4; i++){

        int x = dragon.first + Ox[i];
        int y = dragon.second + Oy[i];

        if (in_Matrix(x,y)==true){
            if (matrix[x][y] == 0){
                matrix[x][y] = matrix[dragon.first][dragon.second]+1;
                dragoni.push(pair<int, int>(x,y));
                dMax = max(dMax, matrix[x][y]);
            }
        }
    }
 }
}

void getMatrix(){

for (int i=0 ; i<n; ++i){
    for (int j=0; j<m; j++)
        cout<<matrix[i][j]<<" ";
    cout<<"\n";

}

}
void BFS(){

queue <pair<int, int> > used;
queue <pair<int, int> > q;

int x,y;

bool isUsed=true;

used.push(pozInitiala);

while (!used.empty() && dMax>=0){

    for(; !used.empty();used.pop()){
        q.push(pair<int,int> (used.front()));
    }

    while (!q.empty()){
        pair <int, int> cell = q.front();
        q.pop();
        isUsed = true;

        for (int i=0; i<4; ++i){
            x = cell.first + Ox[i];
            y = cell.second  + Oy[i];
            if ( in_Matrix(x,y)== true && viz[x][y]==false && matrix[x][y]>=dMax && game[x][y]!='*'){
                if (game[x][y]=='D') dMax = 0;
                q.push(pair<int, int>(x,y));
                viz[x][y]=true;
                isUsed = false;
            }
        }
        if (isUsed == true)
            used.push(pair<int, int>(cell.first,cell.second));
    }

    if (viz[pozFinala.first][pozFinala.second]==true)
        return;
    if (used.empty()==false)
        dMax--;

}

}



int main(){
    getData();
    cout<<dragoni.front().first<<" "<<dragoni.front().second<<"\n";
    LEE();
    getMatrix();
    BFS();
    ofstream out("barbar.out");
    out<<dMax;
    out.close();
    return 0;
}