Cod sursa(job #1053050)

Utilizator DragosGGeornoiu Dragos DragosG Data 12 decembrie 2013 04:28:18
Problema Barbar Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.95 kb
#include<iostream>
#include<vector>

using namespace std;

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;

void citire()
{
	char temp;
	

	cin>>R>>C;

	for(int i=1;i<=R;i++)
		for(int j=1;j<=C;j++)
		{
			cin>>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)
{
	if(path.empty())
	{
		t.x = initial.x;
		t.y = initial.y;
		path.push_back(t);
	}
	
	if (B[final.x][final.y] == z) 
	{
		abc = 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);
	}
	else
		return 0;
}

void main()
{
	citire();
	umplere();
	z=2;
	int ok = drum(0);
	if(ok==0)
		cout<<"-1";
	else if (ok==1)
	{
		cout<<abc;
	}

	system("pause");
}