Cod sursa(job #2366598)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 4 martie 2019 21:07:32
Problema Barbar Scor 0
Compilator cpp-64 Status done
Runda simulare04032019 Marime 2.17 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1005;

int n, m, sx, sy, fx, fy;
bool obs[MAXN][MAXN];

ifstream fin("barbar.in");
ofstream fout("barbar.out");

#define x first
#define y second

vector<pair<int, int> >drag;

void pars(){
    string s;
    getline(fin, s);
    for(int i = 1; i <= n; ++i){
        getline(fin, s);
        for(int j = 0; j <= m; ++j){
            if(s[j] == '*') obs[i][j + 1] = 1;
            if(s[j] == 'D')
                drag.push_back(make_pair(i, j + 1));
            if(s[j] == 'I'){
                sx = i;
                sy = j + 1;
            }
            if(s[j] == 'O'){
                fx = i;
                fy = j + 1;
            }
        }
    }
}

int harta[MAXN][MAXN];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
queue<pair<int, int> >q;

bool ok(int cx, int cy){
    if(cx > 0 && cx <= n && cy > 0 && cy <= m){
        if(!obs[cx][cy])
            return 1;
        return 0;
    }
    return 0;
}

void lee(){
    while(!q.empty()){
        pair<int, int> cel = q.front();
        q.pop();
        for(int d = 0; d < 4; ++d){
            int nx = cel.x + dx[d], ny = cel.y + dy[d];
            if(ok(nx, ny)){
                int npas = harta[cel.x][cel.y] + 1;
                if(npas < harta[nx][ny]){
                    harta[nx][ny] = npas;
                    q.push(make_pair(nx, ny));
                }
            }
        }
    }
}

int fr[1005];

int main()
{
    fin >> n >> m;
    pars();
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= m; ++j)
            harta[i][j] = 1e9;
    }
    for(int i = 0; i < int(drag.size()); ++i){
        q.push(drag[i]);
        harta[drag[i].x][drag[i].y] = 0;
    }
    lee();
    q.push(make_pair(sx, sy));
    harta[sx][sy] = 0;
    lee();
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= m; ++j)
            if(harta[i][j] < 1e9)
                fr[harta[i][j]]++;
    }
    int maxfr = 0, ans = 0;
    for(int i = 1; i <= 1000; ++i){
        if(fr[i] > maxfr){
            maxfr = fr[i];
            ans = i;
        }
    }
    fout << ans;
    return 0;
}