Cod sursa(job #2094809)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 26 decembrie 2017 16:40:12
Problema Car Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.26 kb
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

ofstream fout ("car.out");

class InputReader {
	public:
        InputReader() {}
        InputReader(const char *name) {
            fin = fopen(name, "r");
			buffpos = Size - 1;
        }
        inline InputReader &operator >>(int &n) {
			char ch = nextpos();
            while(ch < '0' || ch > '9') {
                ch = nextpos();
            }
            n = 0;
            while('0' <= ch && ch <= '9') {
                n = n * 10 + ch - '0';
                ch = nextpos();
            }
            return *this;
        }
    private:
        FILE *fin;
        static const int Size = 1 << 17;
        int buffpos;
        char buff[Size];

        inline char nextpos() {
            ++ buffpos;
            if(buffpos == Size) {
                buffpos = 0;
                fread(buff, Size, 1, fin);
            }
			return buff[ buffpos ];
        }
} fin ("car.in");

const int nmax = 500;
const int inf = 1 << 30;
const int dl[ 8 ] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dc[ 8 ] = {-1, 0, 1, 1, 1, 0, -1, -1};

int n, m, ram;
int v[nmax + 2][nmax + 2];

int stx, sty, fnx, fny;

int c[ 8 ][ 8 ];

int d[8 * nmax * nmax + 8 * nmax + 8];
bitset<8 * nmax * nmax + 8 * nmax + 8> gata;

struct str {
    int x, y, k;

    str () {}
    str (int _x, int _y, int _k) {
        x = _x, y = _y, k = _k;
    }
};

vector< str > q[ 5 ];

inline int cst (int x, int y) {
    if (x > y) swap(x, y);
    return min(y - x, 8 - y + x);
}

void extinde (str p, int ind) {
    int vc = 8 * m * p.x + 8 * p.y + p.k;

    for (int dir = 0; dir < 8; ++ dir) {
        int x = p.x + dl[ dir ], y = p.y + dc[ dir ];

        int ac = 8 * m * x + 8 * y + dir;
        if (gata[ ac ] == 1 || v[ x ][ y ] == 1)
            continue;

        int C = c[ p.k ][ dir ];
        if (d[ ac ] > d[ vc ] + C) {
            ++ ram;

            d[ ac ] = d[ vc ] + C;

            int ix = ind + C;
            if (ix >= 5) ix -= 5;
            q[ ix ].push_back(str(x, y, dir));
        }
    }
}

void bfs () {
    ram = 0;

    for (int i = 0; i < 8; ++ i) {
        d[8 * m * stx + 8 * sty + i] = 0;
        ++ ram;
        q[ 0 ].push_back(str(stx, sty, i));
    }

    int ind = 0;
    while (ram > 0) {
        while (q[ ind ].empty()) {
            ++ ind;
            if (ind == 5) ind = 0;
        }

        str p = q[ ind ].back(); q[ ind ].pop_back();
        -- ram;

        int vc = 8 * m * p.x + 8 * p.y + p.k;
        if (gata[ vc ] == 1)
            continue;
        gata[ vc ] = 1;

        extinde(p, ind);
    }
}

int main () {
    fin >> n >> m >> stx >> sty >> fnx >> fny;

    for (int i = 1; i <= n; ++ i) {
        for (int j = 1; j <= m; ++ j) {
            fin >> v[ i ][ j ];
        }
    }

    for (int i = 0; i < 8; ++ i)
        for (int j = 0; j < 8; ++ j)
            c[ i ][ j ] = cst(i, j);

    for (int i = 0; i <= n + 1; ++ i)
        v[ i ][ 0 ] = v[ i ][m + 1] = 1;
    for (int i = 0; i <= m + 1; ++ i)
        v[ 0 ][ i ] = v[n + 1][ i ] = 1;

    fill(d, d + 8 * n * m + 8 * m + 8, inf);
    bfs();

    int ans = inf;

    int x = 8 * m * fnx + 8 * fny;
    for (int i = 0; i < 8; ++ i)
        ans = min(ans, d[x + i]);

    if (ans == inf)
        ans = -1;

    fout << ans << "\n";

    fout.close();
    return 0;
}