Cod sursa(job #1804376)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 12 noiembrie 2016 15:10:49
Problema Castel Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <iostream>
#include <fstream>
#include <set>

using namespace std;

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

const int nMax = 155;
const int mMax = 155;

int n, m, k;
int a[nMax * mMax];
set<int> acc;
bool key[nMax * mMax];
bool viz[nMax * mMax];

void citire()
{
    in >> n >> m >> k;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
            in >> a[m * (i - 1) + j];
}

void found_key(int x)
{
    key[x] = true;
    if(x % m != 1)
        acc.insert(x - 1);
    if(x % m != 0)
        acc.insert(x + 1);
    if(x > m)
        acc.insert(x - m);
    if(x + m <= n * m)
        acc.insert(x + m);
}

void rezolvare()
{
    found_key(k);
    for(auto it = acc.begin(); it != acc.end(); ++it)
    {
        if(key[a[*it]] && key[*it] == false)
        {
            found_key(*it);
            it = acc.begin();
        }
    }
}

void afisare()
{
    int rasp = 0;
    for(int i = 1; i <= n*m; ++i)
    {
        if(key[i])
        {
            ++rasp;
        }
    }
    out << rasp;
}

int main()
{
    citire();
    rezolvare();
    afisare();
    return 0;
}