Cod sursa(job #3339809)

Utilizator Rose_MaryTrandafir Maria Rose_Mary Data 10 februarie 2026 11:21:55
Problema Castel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int DMAX=150;

struct pozitie
{
    short int x,y;
};

int m,n,k,nrcamere;

int h[DMAX+2][DMAX+2];

bool key[DMAX*DMAX+1];

bool viz[DMAX+1][DMAX+1];

pozitie q[DMAX*DMAX+1];

pozitie d[4]={ {0,-1}, {-1,0}, {0,1}, {1,0} };

void citire()
{
    f>>m>>n>>k;
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
        {
            f>>h[i][j];
        }
    }
}

pozitie pozcam(int nrcamera)
{
    pozitie poz;
    poz.x=nrcamera/n;
    if(nrcamera%n!=0)
        poz.x++;
    poz.y=nrcamera-(poz.x-1)*n;
    return poz;
}

inline int nrcam(pozitie poz)
{
    return (poz.x-1)*n+poz.y;
}

void lee()
{
    pozitie crt,vec;
    bool camnoua;
    int u=1;
    key[k]=1;
    q[1]=pozcam(k);

    do
    {
        camnoua=0;
        for(int i=1;i<=u;i++)
        {
            crt=q[i];
            for(int j=0;j<4;j++)
            {
                vec.x=crt.x+d[j].x;
                vec.y=crt.y+d[j].y;

                if(viz[vec.x][vec.y]==0 && key[ h[vec.x][vec.y] ]==1 )
                {
                    key[ nrcam(vec) ]=1;
                    viz[vec.x][vec.y]=1;
                    q[++u]=vec;
                    camnoua=1;
                    nrcamere++;
                }
            }
        }
    }while(camnoua!=0);
}

int main()
{
    citire();
    lee();
    g<<nrcamere;

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