Cod sursa(job #3233995)

Utilizator iulia_morariuIulia Morariu iulia_morariu Data 5 iunie 2024 18:38:21
Problema Barbar Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.44 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
//#include <bits/stdc++.h>

// Marcus Miller - Detroit

using namespace std;

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

int r, c; 
char v[1001][1001];
bool vis[1001][1001]; // ne intereseaza doar daca e posibil

bool lee(int x1, int y1, int x2, int y2){
    queue< pair<int, int> > q;

    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++) vis[i][j] = 0;
    }

    q.push( make_pair(x1, y1) );
    vis[x1][y1] = 1;

    int ox[] = {0, 1, 0, -1};
    int oy[] = {1, 0, -1, 0};

    while(!q.empty()){
        pair<int, int> p = q.front(); q.pop();
        if(vis[ x2 ][ y2 ] == 1) return 1;
        for(int i = 0; i < 4; i++){
            if(p.first + ox[i] < 0 || p.first + ox[i] == r) continue;
            if(p.second + oy[i] < 0 || p.second + oy[i] == c) continue;

            if(vis[ p.first + ox[i] ][ p.second + oy[i] ] == 1) continue;
            if(v[ p.first + ox[i] ][ p.second + oy[i] ] == 'D' ||
               v[ p.first + ox[i] ][ p.second + oy[i] ] == 'F' ||
               v[ p.first + ox[i] ][ p.second + oy[i] ] == 'F') continue;

            q.push( make_pair( p.first + ox[i], p.second + oy[i] ) );
            vis[ p.first + ox[i] ][ p.second + oy[i] ] = 1;
            if(vis[ x2 ][ y2 ] == 1) return 1;
        }
    }
    return 0;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    fin >> r >> c;
    vector< pair<int, int> > dr;
    int x1 = 0, y1 = 0, x2 = 0, y2 = 0;

    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            fin >> v[i][j];
            if(v[i][j] == 'D') dr.push_back( make_pair(i, j) );
            if(v[i][j] == 'I'){ x1 = i; y1 = j; }
            if(v[i][j] == 'O'){ x2 = i; y2 = j; }
        }
    }
    //cout << "r = " << r << " c = " << c << '\n';

    int ll = 0, rr = 100000;
    int sol = -1;

    //cout << "I = ( " << x1 << " , " << y1 << " ) O = ( " << x2 << " , " <<  y2 << " ) \n"; 
    //cout << "l = " << ll << " r = " << rr << '\n';

    while(ll <= rr){
        int m = (ll + rr) / 2;

        bool bun = 1;

        //cout << "m = " << m << '\n';

        for(int i = 0; i < dr.size(); i++){
            int len = m;
            for(int d1 = 0; d1 <= m; d1++){ // distata coloanei fata de col initiala
                int l1 = dr[i].first - len;
                int r1 = dr[i].first + len;

                if(dr[i].second >= d1 && v[ dr[i].first ][ dr[i].second - d1 ] == '.') v[ dr[i].first ][ dr[i].second - d1 ] = 'F';
                if(dr[i].second >= d1 && v[ dr[i].first ][ dr[i].second - d1 ] == 'I'){ bun = 0; break; }
                if(dr[i].second >= d1 && v[ dr[i].first ][ dr[i].second - d1 ] == 'O'){ bun = 0; break; }

                if(dr[i].second + d1 < c && v[ dr[i].first ][ dr[i].second + d1 ] == '.') v[ dr[i].first ][ dr[i].second + d1 ] = 'F';
                if(dr[i].second + d1 < c && v[ dr[i].first ][ dr[i].second + d1 ] == 'I'){ bun = 0; break; }
                if(dr[i].second + d1 < c && v[ dr[i].first ][ dr[i].second + d1 ] == 'O'){ bun = 0; break; }


                for(int j = max(l1, 0); j <= min(r, r1); j++){
                    if(d1 == 0 && j == dr[i].first) continue;
                    if(dr[i].second >= d1 && v[ j ][ dr[i].second - d1 ] == '.') v[ j ][ dr[i].second - d1 ] = 'F';
                    if(dr[i].second >= d1 && v[ j ][ dr[i].second - d1 ] == 'I'){ bun = 0; break; }
                    if(dr[i].second >= d1 && v[ j ][ dr[i].second - d1 ] == 'O'){ bun = 0; break; }

                    if(dr[i].second + d1 < c && v[ j ][ dr[i].second + d1 ] == '.') v[ j ][ dr[i].second + d1 ] = 'F';
                    if(dr[i].second + d1 < c && v[ j ][ dr[i].second + d1 ] == 'I'){ bun = 0; break; }
                    if(dr[i].second + d1 < c && v[ j ][ dr[i].second + d1 ] == 'O'){ bun = 0; break; }
                }
                len--;
            }
            if(!bun) break;
        }

        /*cout << "m = " << m << " v : " << '\n';
        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                cout << v[i][j] << " ";
            }
            cout << '\n';
        }
        cout << '\n';*/

        bool ok = lee(x1, y1, x2, y2);
        if(ok && bun){
            ll = m + 1;
            sol = m;
        }else rr = m - 1;

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                if(v[i][j] == 'F') v[i][j] = '.';
            }
        }
    }

    fout << sol + 1 << '\n';

    return 0;
}