Cod sursa(job #1074382)

Utilizator andreiiiiPopa Andrei andreiiii Data 7 ianuarie 2014 17:06:10
Problema Portal3 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <fstream>
#define PII pair<long long, long long>
#define x first
#define y second

using namespace std;

ifstream fin("portal3.in");
ofstream fout("portal3.out");

PII a[4], b[4], c[4];
long long cost, d[4], v[4], n, m, nr, sol;

long long modul(long long x)
{
    if(x<0) return -x;
    return x;
}

void bt(int k)
{
    cost+=n-c[nr].x+m-c[nr].y;
    if(cost<sol) sol=cost;
    cost-=n-c[nr].x+m-c[nr].y;
    if(k==4) return;
    int i;
    for(i=1;i<4;i++)
    {
        if(!v[i])
        {
            c[++nr]=a[i];
            v[i]=1;
            cost+=d[i];
            cost+=modul(b[i].x-c[nr-1].x)+modul(b[i].y)-c[nr-1].y;
            bt(k+1);
            cost-=modul(b[i].x-c[nr-1].x)+modul(b[i].y)-c[nr-1].y;
            c[nr]=b[i];
            cost+=modul(a[i].x-c[nr-1].x)+modul(a[i].y)-c[nr-1].y;
            bt(k+1);
            cost-=modul(a[i].x-c[nr-1].x)+modul(a[i].y)-c[nr-1].y;
            cost-=d[i];
            v[i]=0;
            --nr;
        }
    }
}

int main()
{
    int t, i;
    fin>>t;
    while(t--)
    {
        sol=0x3f3f3f3f;
        fin>>n>>m;
        for(i=1;i<4;i++)
        {
            fin>>a[i].x>>a[i].y>>b[i].x>>b[i].y>>d[i];
        }
        bt(1);
        fout<<sol<<"\n";
    }
    fin.close();
    fout.close();
}