Cod sursa(job #971618)

Utilizator Stefex09Stefan Teodorescu Stefex09 Data 9 iulie 2013 19:02:17
Problema Castel Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <iostream>
#include <fstream>
#include <queue>

using namespace std;

ifstream in ("castel.in");
ofstream out ("castel.out");

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

int N, M;
queue <int> Q;
int A[MAXN][MAXN], X[MAXN][MAXN];
bool Pos[MAXN];
bool Viz[MAXN][MAXN];

bool ok (int x, int y)
{
    return (x >= 1 && x <= N && y >= 1 && y <= M);
}

int main()
{
    int K, i, j, now, nowx, nowy, vx, vy, v, Ans = 1;

    in >> N >> M >> K;
    now = 1;

    for (i = 1; i <= N; i ++)
        for (j = 1; j <= M; j ++)
            in >> A[i][j], X[i][j] = now ++;

    Viz[K / N + 1][K % M == 0 ? M : K % M] = 1;
    Pos[K] = 1;
    Q.push (K);

    int nrpasi = 1;

    while (!Q.empty () && nrpasi <= N * M){
        ++ nrpasi;
        now = Q.front ();
        Q.pop ();
        nowx = now / N + 1;
        if (now % M == 0)
            nowy = M;
        else
            nowy = now % M;

        for (i = 0; i < 4; i ++){
            vx = nowx + dx[i];
            vy = nowy + dy[i];

            if (!ok (vx, vy))
                continue;

            v = X[vx][vy];

            if (Pos[ A[vx][vy] ]){
                if (!Viz[vx][vy])
                    Ans ++;

                Pos[v] = 1, Viz[vx][vy] = 1, Q.push (v);
            }
        }
    }

    out << Ans;

    return 0;
}