Cod sursa(job #2124183)

Utilizator tziplea_stefanTiplea Stefan tziplea_stefan Data 6 februarie 2018 23:26:51
Problema Castel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <fstream>
#include <vector>
#include <queue>
#define VAL 155

using namespace std;

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

struct celule
{
    int L, C;
};

celule X[VAL*VAL];

const int dl[]={-1, 0, 1, 0};
const int dc[]={0, -1, 0, 1};

int N, M, K, ANS, nr, NR;
int v[VAL][VAL], i, j;
bool viz[VAL][VAL], gas;
bool CH[VAL*VAL];

int Trans(int L, int C)
{
    return (L-1)*M+C;
}

void Check(int L, int C)
{
    int L2, C2;
    for (int j=0; j<4; j++)
    {
        L2=L+dl[j];
        C2=C+dc[j];
        if (L2==0 || L2==N+1 || C2==0 || C2==M+1)
          continue;
        if (viz[L2][C2]==false && CH[v[L2][C2]]==true)
        {
            viz[L2][C2]=true;
            X[++NR].L=L2;
            X[NR].C=C2;
            CH[Trans(L2, C2)]=true;
            gas=true;
        }
    }
}

int main()
{
    fin >> N >> M >> K;
    for (i=1; i<=N; i++)
    {
        for (j=1; j<=M; j++)
        {
            fin >> v[i][j];
            nr++;
            if (nr==K)
            {
                X[++NR].L=i;
                X[NR].C=j;
                viz[i][j]=1;
            }
        }
    }
    CH[K]=true;
    do
    {
        gas=false;
        for (i=1; i<=NR; i++)
          Check(X[i].L, X[i].C);
    }
    while (gas==true);
    for (i=1; i<=N; i++)
      for (j=1; j<=M; j++)
        if (viz[i][j]==true)
          ANS++;
    fout << ANS << '\n';
    fin.close();
    fout.close();
    return 0;
}