Cod sursa(job #1126736)

Utilizator andreimdvMoldovan Andrei andreimdv Data 27 februarie 2014 09:30:23
Problema Castel Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include<fstream>
using namespace std;
ifstream fin("castel.in");
ofstream fout("castel.out");
bool cheia[22600],camera[22600];
struct str{int x,y;};
str q,w,coada[2000000];
int ma[155][155],a[155][155];
int n,m,k,l,r,i,poz,j,camere,chei;
int dl[]={-1,0,1,0};
int dc[]={0,1,0,-1};

int main()
{
    fin>>n>>m>>poz;
    for(i=1;i<=n;++i)
        for(j=1;j<=m;++j)
        {
            fin>>ma[i][j];
        }
    cheia[poz]=1;
    camera[poz]=1;
    chei=1;
    camere=1;
    coada[l].x=(poz-1)/m+1;
    coada[l].y=poz-(poz-1)/m*m;
    a[coada[l].x][coada[l].y]=1;
    while(l<=r)
    {
        w=coada[l++];
        for(k=0;k<4;++k)
        {
            q.x=w.x+dl[k]; q.y=w.y+dc[k];
            if(a[q.x][q.y]<chei&&ma[q.x][q.y]&&cheia[ma[q.x][q.y]]==1)
            {
                a[q.x][q.y]=chei;
                if(camera[(q.x-1)*m+q.y]==0)
                {
                    a[q.x][q.y]++;
                    chei++;
                    camere++;
                    camera[(q.x-1)*m+q.y]=1;
                }
                cheia[(q.x-1)*m+q.y]=1;
                coada[++r]=q;
            }
        }
    }
    fout<<chei;

    return 0;
}