Cod sursa(job #2305831)

Utilizator minecraft3Vintila Valentin Ioan minecraft3 Data 21 decembrie 2018 10:40:08
Problema Barbar Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.08 kb
#include <bits/stdc++.h>



using namespace std;



int r, c;

int a[1005][1005];

bool mp[1005][1005];



pair<int, int> b, e, auxp;



int sj[] = {0, 1, 0, -1};

int sd[] = {1, 0, -1, 0};



#define chk(v1, v2, v3, v4) (v1 && v2 && v1 <= r && v2 <= c && v3[v1][v2] == v4)



int main()

{

    ifstream fin("barbar.in");

    ofstream fout("barbar.out");

    int i, j, fa;

    char chr;



    queue< pair<int, int> > q;

    fin >> r >> c;



    for(i = 1; i <= r; ++i)

    {

        j = 1; fin >> chr;

        if(chr == 'I')

        {

            b.first = i;

            b.second = j;

            a[i][j] = -1;

        }

        else if(chr == 'O')

        {

            e.first = i;

            e.second = j;

            a[i][j] = -1;

        }

        else if(chr == 'D')

        {

            q.push(make_pair(i, j));

            a[i][j] = 0;

        }

        else if(chr == '*')

        {

            a[i][j] = -2;

        }

        else

            a[i][j] = -1;

        for(j = 2; j <= c; ++j)

        {

            fin.get(chr);



            if(chr == 'I')

            {

                b.first = i;

                b.second = j;

                a[i][j] = -1;

            }

            else if(chr == 'O')

            {

                e.first = i;

                e.second = j;

                a[i][j] = -1;

            }

            else if(chr == 'D')

            {

                q.push(make_pair(i, j));

                a[i][j] = 0;

            }

            else if(chr == '*')

            {

                a[i][j] = -2;

            }

            else

                a[i][j] = -1;

        }

    }




    while(!q.empty())

    {

        auto alpha = q.front();

        q.pop();///hehe

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

        {

            auxp.first = alpha.first + sj[i];

            auxp.second = alpha.second + sd[i];



            if(chk(auxp.first, auxp.second, a, -1))

            {

                a[auxp.first][auxp.second] = a[alpha.first][alpha.second] + 1;

                q.push(auxp);

            }

        }

    }



    /*for(i = 1; i <= r; ++i)

    {

        for(j = 1; j <= c; ++j)

            fout << a[i][j] << ' ';

        fout << endl;

    }*/



    i = 1; j = max(r, c) - 1; fa = 1e9;
    int med;

    bool mom = false;

    while(i <= j)
    {
        med = (i + j) / 2;
        bool found = false;
        memset(mp, 0, sizeof(mp));

		mp[b.first][b.second] = 1;
        q.push(b);

		while(!q.empty() && !found)
		{
		    for(int k = 0; k < 4; ++k)
		    {
		        auxp.first = q.front().first + sj[k];
		        auxp.second = q.front().second + sd[k];

		        if(chk(auxp.first, auxp.second, mp, 0) && a[auxp.first][auxp.second] >= med)
		        {
		            mp[auxp.first][auxp.second] = true;
		            q.push(auxp);

		            if(auxp == e)
					{
                        mom = true;
						found = true;
						fa = min(fa, med);
						break;

					}
		        }
		    }
			q.pop();

		}
		if(found)
			j = med-1;
		else
			i = med+1;

    }


    if(mom)
        fout << fa;
    else
        fout << -1;
}