Cod sursa(job #1724936)

Utilizator Theodor1000Cristea Theodor Stefan Theodor1000 Data 4 iulie 2016 16:40:32
Problema Castel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>

#define l(x) (((x)-1)/(m)+1)
#define c(x) (((x)-1)%(m)+1)
#define cl(x,y) (((x)-1)*(m)+(y))
#define pii pair <int, int>
#define f first
#define s second

using namespace std;

int a[160][160];
int dx[] = {0,  0, 1, -1};
int dy[] = {1, -1, 0,  0};

queue <pii> q;
bool ap[23000], use[160][160];
vector <int> v[23000];

inline void del (vector <int> a)
{
    vector <int> aaa;
    a.swap (aaa);
}

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

    int n, m, p;
    scanf ("%d %d %d", &n, &m, &p);

    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            scanf ("%d", &a[i][j]);

    q.push ({l(p), c(p)});
    use[l(p)][c(p)] = true;

    int rez = 0;
    while (!q.empty ())
    {
        ++rez;
        pii x = q.front ();
        q.pop ();

        int ch = cl (x.f, x.s);
        ap[ch] = true;

        for (auto &it : v[ch])
            q.push ({l(it), c(it)});

        del (v[ch]);

        for (int i = 0; i < 4; ++i)
        {
            pii xx = x;
            xx.f += dx[i];
            xx.s += dy[i];

            if (!a[xx.f][xx.s] || use[xx.f][xx.s]) continue;

            if (ap[a[xx.f][xx.s]])
                q.push (xx), use[xx.f][xx.s] = true;

            else use[xx.f][xx.s] = true, v[a[xx.f][xx.s]].push_back (cl(xx.f, xx.s));
        }
    }

    printf ("%d\n", rez);

    return 0;
}