Cod sursa(job #636039)

Utilizator titusuTitus C titusu Data 19 noiembrie 2011 16:31:25
Problema Portal3 Scor 0
Compilator cpp Status done
Runda .com 2011 Marime 1.15 kb
using namespace std;
#include <fstream>
#include <iostream>
#include <climits>
#include <cstdlib>

int x[4],sol,n,m,uz[4];
int T,px[4][2],py[4][2],c[4];

void Back(int k){
	for(int i=1;i<=3;++i)
		if(uz[i]==0){
			uz[i]=1;
			for(int a=-1 ; a<=1 ; a++){
				x[k] = a*i;
				if(k==3){
					//for(int j=1;j<=3;++j)
					//	cout << x[j] <<" ";
					//cout << endl;
					int S = 0;
					int ii=0,jj=0, start, stop;
					for(int j=1 ; j<=3 ; j++)
					if(x[j])
					{
						start=0,stop=1;
						if(x[j]<0)
							start = 1, stop = 0;
						int xj = abs(x[j]);
						S += abs(ii-px[xj][start]) + abs(jj-py[xj][start])+c[xj];
						ii = px[xj][stop], jj = py[xj][stop];
					}
					S+= abs(ii-n)+ abs(jj-m);
					if(S<sol)
						sol=S;
				}
				else
					Back(k+1);
			}
			uz[i]=0;
		}
}

int main(){
	ifstream fin("portal3.in");
	ofstream fout("portal3.out");
	fin>>T;
	for(; T ;--T){
		fin>>n>>m;
		fin >> px[1][0] >> py[1][0] >> px[1][1] >> py[1][1] >> c[1];
		fin >> px[2][0] >> py[2][0] >> px[2][1] >> py[2][1] >> c[2];
		fin >> px[3][0] >> py[3][0] >> px[3][1] >> py[3][1] >> c[3];
		uz[1] = uz[2] = uz[3] = 0;
		sol = INT_MAX;
		Back(1);
		fout << sol << "\n";
	}
	return 0;
}