Cod sursa(job #1053053)

Utilizator DragosGGeornoiu Dragos DragosG Data 12 decembrie 2013 05:41:04
Problema Barbar Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.33 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()
{
	

	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 j, int z)
{
	if (A[initial.x][initial.y] < z)
			return 0;

	if(path.empty())
	{
		t.x = initial.x;
		t.y = initial.y;
		path.push_back(t);
	}
	
	if (B[final.x][final.y] == z) 
		return 1;

	if(j<path.size())
	{
		for(int i=0;i<3;i++)
		{
			if ((path[j].x+dx[i] >= 1) && (path[j].x+dx[i] <= R) && (path[j].y+dy[i] >= 1) && (path[j].y+dy[i] <= C) && (A[path[j].x+dx[i]][path[j].y+dy[i]] >= z) && ( B[path[j].x + dx[i]][path[j].y + dy[i]] != z))
			{
				B[path[j].x+dx[i]][path[j].y+dy[i]] = z;
				t.x = path[j].x +dx[i];
				t.y = path[j].y +dy[i];
				path.push_back(t);
			}
		}
		
		j++;
		drum(j,z);
	}
	else
		return 0;
}

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

	  
}

int main()
{
	citire();
	umplere();
	
	calculare(1, 10);

	fout<<maxim;

	return 0;
}