Cod sursa(job #2587753)

Utilizator stefantagaTaga Stefan stefantaga Data 23 martie 2020 15:15:27
Problema Castel Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.91 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("castel.in");
ofstream g("castel.out");
int dl[]={0,1,0,-1};
int dc[]={1,0,-1,0};
int n,m,k,v[155][155];
vector <pair <int,int> > v1[24030];
int fr[24030],i,j,val[155][155],pozx,pozy,numar,cheie;
queue <pair <int,int > > q;
pair <int,int > pozitie,poz,b;
bool ok[155][155];
int main()
{
    f>>n>>m>>k;
    for (i=1;i<=n;i++)
    {
        for (j=1;j<=m;j++)
        {
            f>>v[i][j];
            v1[v[i][j]].push_back({i,j});
        }
    }
    for (i=0;i<=n+1;i++)
    {
        for (j=0;j<=m+1;j++)
        {
            val[i][j]=-1;
        }
    }
    pozx=k/(m+1)+1;
    pozy=k%(m+1);
    val[pozx][pozy]=0;
    ok[b.first][b.second]=1;
    q.push({pozx,pozy});
    while (!q.empty())
    {
        poz=q.front();
        q.pop();
        cheie=(poz.first-1)*m+poz.second;
        if (fr[cheie]==0)
        {
            for (i=0;i<v1[cheie].size();i++)
        {
            pozitie=v1[cheie][i];
            val[pozitie.first][pozitie.second]=0;
            for (j=0;j<4;j++)
            {
                b.first=pozitie.first+dl[j];
                b.second=pozitie.second+dc[j];
                if (val[b.first][b.second]==0)
                {
                    q.push(pozitie);
                    ok[b.first][b.second]=1;
                    break;
                }
            }
        }
        }
        for (i=0;i<4;i++)
        {
            b.first=poz.first+dl[i];
            b.second=poz.second+dc[i];
            if (val[b.first][b.second]==0&&ok[b.first][b.second]==0)
            {
                q.push(b);
                ok[b.first][b.second]=1;
            }
        }
    }
    for (i=1;i<=n;i++)
    {
        for (j=1;j<=m;j++)
        {
            if (val[i][j]==0)
            {
                numar++;
            }
        }
    }
    g<<numar;
    return 0;
}