Cod sursa(job #2214695)

Utilizator GiihuoTihufiNeacsu Stefan GiihuoTihufi Data 19 iunie 2018 17:45:38
Problema Castel Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <bits/stdc++.h>

using namespace std;

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

#define getKey(l,c) Key[(l-1)*M+c]=1,A[l][c]=0,nrCam++

int main()
{
    int N,M,K;
    int A[152][152]={0};
    map<int,bool> Key;
    Key.clear();
    Key[0]=0;

    f>>N>>M>>K;
    for(int i=1;i<=N;i++)
        for(int j=1;j<=M;j++)
            f>>A[i][j];
    vector<int> L={K/M+1-(K%M==0)},C={(K%M==0)? M : K%M};
    int nrCam=0;
    getKey(L[0],C[0]);

    bool moved=1;
    auto add = [=,&A,&Key,&moved,&nrCam](vector<int> &L0,vector<int> &C0,int l,int c)
    {
        if(A[l][c]!=0 && Key[A[l][c]])
        {
            L0.push_back(l),C0.push_back(c);
            getKey(l,c);
            moved=1;
        }
    };

    while(moved)
    {
        moved=0;
        vector<int> L1,C1;
        for(uint32_t i=0;i<L.size();i++)
            add(L,C,L[i]-1,C[i]  ),
            add(L,C,L[i]+1,C[i]  ),
            add(L,C,L[i]  ,C[i]-1),
            add(L,C,L[i]  ,C[i]+1);

        for(uint32_t i=0;i<L.size();i++)
            if(L[i]>0 && L[i]<=N && C[i]>0 && C[i]<=M)
                if (A[L[i]-1][C[i]]==0 && A[L[i]+1][C[i]]==0 &&
                    A[L[i]][C[i]-1]==0 && A[L[i]][C[i]+1]==0) continue;
                    else L1.push_back(L[i]),C1.push_back(C[i]);
        L=L1;
        C=C1;
    }

    g<<nrCam;

    f.close(),g.close();
    return 0;
}