Cod sursa(job #2469078)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 6 octombrie 2019 14:59:44
Problema Barbar Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.86 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <cmath>
#define INF 0x3f3f3f3f
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

using namespace std;

const int N = 1000;

char a[5 + N][5 + N];
bool viz[5 + N][5 + N];
int dist[5 + N][5 + N];

struct ura{
    int l, c, ds;
};

queue <ura> q;

int n, m, xst, yst, xfn, yfn, Min = INF;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

void Lee(){
    while(!q.empty()){
        int l, c, di;
        l = q.front().l;
        c = q.front().c;
        di = q.front().ds;
        for(int k = 0; k < 4; k++){
            ura nexpos;
            nexpos.l = l + dx[k];
            nexpos.c = c + dy[k];
            nexpos.ds = di + 1;
            if(a[nexpos.l][nexpos.c] != '*' && !viz[nexpos.l][nexpos.c]){
                q.push(nexpos);
                viz[nexpos.l][nexpos.c] = true;
            }
        }
        viz[l][c] = true;
        dist[l][c] = MIN(dist[l][c], di);
        q.pop();
    }
}


void DFS(int dmin){
    q.push({xst, yst});
    while(!q.empty()){
        pair <int,int> pos, nextpos;
        pos = {q.front().l, q.front().c};
        for(int k = 0; k < 4; k++){
            nextpos.first = pos.first + dx[k];
            nextpos.second = pos.second + dy[k];
            if(a[nextpos.first][nextpos.second] != '*' && dist[nextpos.first][nextpos.second] >= dmin && viz[nextpos.first][nextpos.second] == false){
                q.push({nextpos.first, nextpos.second});
                viz[nextpos.first][nextpos.second] = true;
            }
        }
        viz[pos.first][pos.second] = true;
        q.pop();
    }
}

bool ok(int x){
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            viz[i][j] = false;
    while(q.size()) q.pop();
    if(dist[xst][yst] < x) return false;
    DFS(x);
    return viz[xfn][yfn];
}

void Solve(){
    int st, dr, mid, rez = -1;
    st = 1;
    dr = dist[xfn][yfn];
    while(st <= dr){
        mid = (st + dr) >> 1;
        if(ok(mid)){
            st = mid + 1;
            rez = mid;
        } else dr = mid - 1;
    }
    printf("%d\n", rez);
}

int main()
{
    freopen("barbar.in","r",stdin);
    freopen("barbar.out","w",stdout);
    int i, j;
    scanf("%d%d ", &n, &m);
    for(i = 1; i <= n; i++){
        for(j = 1; j <= m; j++){
            scanf("%c", &a[i][j]);
            if(a[i][j] == 'D') q.push({i,j});
            else if(a[i][j] == 'I'){
                xst = i;
                yst = j;
            }
            else if(a[i][j] == 'O'){
                xfn = i;
                yfn = j;
            }
            dist[i][j] = INF;
        }
        scanf(" ");
    }
    for(i = 0; i <= n + 1; i++) a[i][m + 1] = a[i][0] = '*';
    for(i = 0; i <= m + 1; i++) a[0][i] = a[n + 1][i] = '*';
    Lee();
    for(i = 1; i <= n; i++){
        for(j = 1; j <= m; j++)
            printf("%d ", dist[i][j]);
        printf("\n");
    }
    Solve();
    return 0;
}