Cod sursa(job #1053057)

Utilizator DragosGGeornoiu Dragos DragosG Data 12 decembrie 2013 06:38:01
Problema Barbar Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.51 kb
#include<iostream>
#include<vector>
#include<fstream>

using namespace std;

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

struct punct
{
	int x,y;
};

punct initial,final,t;
int R,C;
int A[1002][1002],B[1002][1002];
vector<punct> dragon,path;
int dx[] = { 0, 1, 0, -1};
int dy[] = { 1, 0, -1, 0};
int z,abc;
int ok;
int maxim=-1;

void citire()
{
	char temp;
	

	fin>>R>>C;

	for(int i=1;i<=R;i++)
		for(int j=1;j<=C;j++)
		{
			fin>>temp;

			A[i][j] = 5000;


			if(temp=='I')
			{
				initial.x = i;
				initial.y = j;
			}
			else if(temp=='O')
			{
				final.x = i;
				final.y = j;
			}
			else if(temp=='D')
			{
				A[i][j] = 0;
				t.x = i;
				t.y = j;
				dragon.push_back(t);
			}
			else if(temp=='*')
			{
				A[i][j] = 0;
			}
			
		}
}

void umplere()
{
	

	unsigned int i=0;
	while(i<dragon.size())
	{
		for(int j=0;j<=3;j++)									
		{
			if((A[dragon[i].x + dx[j]][dragon[i].y + dy[j]] > A[dragon[i].x][dragon[i].y]+1) && (dragon[i].x <= R) && (dragon[i].y <=C) && (dragon[i].x>=1) && (dragon[i].y>=1))
			{
				A[dragon[i].x +dx[j]][dragon[i].y +dy[j]] = A[dragon[i].x][dragon[i].y]+1;
				t.x = dragon[i].x + dx[j];
				t.y = dragon[i].y + dy[j];
				dragon.push_back(t);
			}
		}

		i++;
	}
}


int drum( int z )
{
    if (A[initial.x][initial.y] < z || A[final.x][final.y] < z) 
        return 0;

	t.x = initial.x;
	t.y = initial.y;
	path.push_back(t);
	int sc=0;
  
    for ( int ic = 0; ic <= sc && B[final.x][final.y] != z; ic++)
    {
  
        for (int i = 0; i < 4; ++i)
			if (path[ic].x+dx[i] >= 1 && path[ic].x+dx[i] <= R && path[ic].y+dy[i] >= 1 && path[ic].y+dy[i] <= C && B[path[ic].x + dx[i]][path[ic].y + dy[i]] != z && A[path[ic].x+dx[i]][path[ic].y+dy[i]] >= z)
            {
                sc++;
				t.x = path[ic].x + dx[i];
				t.y = path[ic].y + dy[i];
                path.push_back(t);
                B[path[ic].x+dx[i]][path[ic].y+dy[i]] = z;
            }
    }
  
    if (B[final.x][final.y] == z) 
        return 1;
  
    return 0;
}



void calculare(int st, int dr)
{
	int z = (st + dr)/2;
	path.clear();
    if( st > dr )
        return;
    else
    {
		
		if(! drum(z)) 
            calculare(st, z-1);
        else
        {
            maxim = z;
            calculare(z+1, dr);
         
        }
    }

	  
}

int main()
{
	citire();
	umplere();
	
	int max = R;
    if( C>R )
        max=C;

	calculare(1, max);

	fout<<maxim;

	return 0;
}