Cod sursa(job #186070)

Utilizator andrei-alphaAndrei-Bogdan Antonescu andrei-alpha Data 26 aprilie 2008 17:38:23
Problema Barbar Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.92 kb
#include <stdio.h>
#define SI 30000 
#define Nmax 1111
short int b[Nmax][Nmax],a[Nmax][Nmax];
struct punct{int c1,c2;};
punct plec,sos;
struct paznici{int a,b;};
paznici v[100000];

const int lin[]={0,1,0,-1};
const int col[]={1,0,-1,0};

int rez,R,C;
inline int minim(int x,int y)
{
	if(x<y)
		return x;
	return y;
}	
void read()
{
	char ch;
	freopen("barbar.in", "r",stdin);
	freopen("barbar.out", "w",stdout);
	scanf("%d%d", &R,&C);
	for(int i=1;i<=R;++i)
	{
		scanf("%c", &ch);
		for(int j=1;j<=C;++j)
		{
			scanf("%c", &ch);
			if(ch=='I')
			{
				a[i][j]=SI;
				plec.c1=i;
				plec.c2=j;
			}	
			if(ch=='O')
			{
				a[i][j]=SI;
				sos.c1=i;
				sos.c2=j;
			}	
			if(ch=='.')
				a[i][j]=SI;
			if(ch=='*')
				a[i][j]=-2;
			if(ch=='D')
			{
				a[i][j]=0;
				v[++v[0].a].a=i;
				v[v[0].a].b=j;
			}
		}	
	}	
}
void lee(int x,int y)
{
	for(int t=0;t<4;t++)
		if((a[x+lin[t]][y+col[t]]>a[x][y]+1))
		{
			a[x+lin[t]][y+col[t]]=a[x][y]+1;
			v[++v[0].a].a=x+lin[t];
			v[v[0].a].b=y+col[t];
		}
}	
void lee2(int x,int y)
{
	for(int t=0;t<4;t++)
		if((b[x+lin[t]][y+col[t]]<b[x][y]))
		{
			b[x+lin[t]][y+col[t]]=minim(a[x+lin[t]][y+col[t]],b[x][y]);
			v[++v[0].a].a=x+lin[t];
			v[v[0].a].b=y+col[t];
		}
}
void make_lee()
{
	for(int i=1;i<=v[0].a;i++)
		lee(v[i].a,v[i].b);
	b[plec.c1][plec.c2]=a[plec.c1][plec.c2];
	v[1].a=plec.c1;
	v[1].b=plec.c2;
	v[0].a=1;
	for(int i=1;i<=v[0].a;i++)
		lee2(v[i].a,v[i].b);
	rez=b[sos.c1][sos.c2];
}	
void print()
{
	//printf("%d %d %d %d\n",plec.c1,plec.c2,sos.c1,sos.c2);
	//for(int i=1;i<=R;++i)
	//{
	//	for(int j=1;j<=C;++j)
	//			printf("%d ",a[i][j]);
	//	printf("\n");
	//}	
	//printf("\n");
	//for(int i=1;i<=R;++i)
	//{
	//	for(int j=1;j<=C;++j)
	//			printf("%d ",b[i][j]);
	//	printf("\n");
	//}
	printf("%d\n",rez==0?-1 :rez);

}
int main()
{
	read();
	make_lee();
	print();
	return 0;
}