Cod sursa(job #637620)

Utilizator Eugen01Vasilescu Eugen Eugen01 Data 20 noiembrie 2011 15:37:00
Problema Portal3 Scor 0
Compilator cpp Status done
Runda .com 2011 Marime 1.51 kb
#include<stdio.h>

struct X
{
	int x1,y1,x2,y2,cost;
};

using namespace std;

int nr,k,i,t,j,N,M,sol[10],SOL[10],pus[10];
long long D,min;
X P[10];

long long abs(int x)
{
	return ((x>0) ? x:-x);
}

long long dist(int x1,int y1,int x2,int y2)
{
	return (abs(x2-x1)+abs(y2-y1));
}

void verif()
{
	int i;
	X last;
	
	last.x1=0;
	last.y1=0;
	D=0;
	
	for (i=1;i<=nr;i++)
	{
		D=D+dist(last.x1,last.y1,P[SOL[i]].x1,P[SOL[i]].x1)+P[SOL[i]].cost;
		last.x1=P[SOL[i]].x2;
		last.y1=P[SOL[i]].y2;
	}	
	D=D+dist(last.x1,last.y1,N,M);
	
	if (D<min) min=D;
}

void back(int k)
{
	int i;
	
	if (k==nr+1)
	{
		verif();
		return;
	}
	
	for (i=1;i<=nr;i++)
		if (pus[i]==0)
		{
			SOL[k]=sol[i];
			pus[i]=1;
			back(k+1);
			pus[i]=0;
		}
}

void solve(int x)
{
	int i;
	
	if ((1&x)&&((1<<3)&x)) return;
	if ((2&x)&&((1<<4)&x)) return;
	if ((4&x)&&((1<<5)&x)) return;
	
	nr=0;
	for (i=0;i<6;i++)
		if ((1<<i)&x)
			sol[++nr]=i+1;
		
	for (i=1;i<=6;i++) pus[i]=0;
	back(1);
}

int main()
{
	freopen("portal3.in","r",stdin);
	freopen("portal3.out","w",stdout);
	

	scanf("%d",&t);
	
	for (i=1;i<=t;i++)
	{
		scanf("%d%d",&N,&M);
		for (k=1;k<=3;k++)
			scanf("%d%d%d%d%d",&P[k].x1,&P[k].y1,&P[k].x2,&P[k].y2,&P[k].cost);
		
		for (k=4;k<=6;k++)
		{
			P[k].x1=P[k-3].x2;
			P[k].y1=P[k-3].y1;
			P[k].x2=P[k-3].x1;
			P[k].y2=P[k-3].y1;
			P[k].cost=P[k-3].cost;
		}
		
		
		min=N+M-1;
		for (j=1;j<=63;j++)
			solve(j);
		
		printf("%lld\n",min);
	}
}