Cod sursa(job #2305820)

Utilizator minecraft3Vintila Valentin Ioan minecraft3 Data 21 decembrie 2018 09:48:15
Problema Barbar Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.84 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())
    {
        for(i = 0; i < 4; ++i)
        {
            auxp.first = q.front().first + sj[i];
            auxp.second = q.front().second + sd[i];

            if(chk(auxp.first, auxp.second, a, -1))
            {
                a[auxp.first][auxp.second] = a[q.front().first][q.front().second] + 1;
                q.push(auxp);
            }
        }
        q.pop();
    }

    /*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 = -1;
    int med;

    while(i <= j)
    {
        med = (i + j) / 2;

        bool found = false;
        memset(mp, 0, sizeof(mp));

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

		while(!q.empty())
		{
		    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)
		        {
		            a[auxp.first][auxp.second] = true;
		            q.push(auxp);

		            if(auxp == e)
					{
						found = true;
						fa = min(fa, med);
						break;
					}
		        }
		    }
			q.pop();
		}

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

	fout << fa;
}