Cod sursa(job #2144432)

Utilizator MoldovanAndrei1Moldovan Andrei MoldovanAndrei1 Data 26 februarie 2018 19:03:53
Problema Castel Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <cstdio>
#include <vector>
using namespace std;
struct md
{
    int x,y;
};
int a[152][152];
bool viz[225005];
int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
int main()
{
    freopen("castel.in","r",stdin);
    freopen("castel.out","w",stdout);
    int m,n,k,i,j;
    scanf("%d%d%d",&m,&n,&k);
    for(int i=1;i<=m;i++)
        for(j=1;j<=n;j++)
        scanf("%d",&a[i][j]);
        int x=(k+n-1)/n;
        int y=k%n;
        if(y==0)y=n;
    vector<md>q;
    md temp,temp1;
    temp.x=x;temp.y=y;
    q.push_back(temp);
    int cnt=0;
    viz[k]=1;
    cnt=1;
    int r=1;
    while(r)
    {
        r=0;
        vector<md>::iterator it;
        int l=q.size(),j;
        it=q.begin();
        for(j=1;j<=l;j++)
        {temp=(*it);
        int l=(temp.x-1)*n+temp.y;
        for(int i=0;i<4;i++)
        {
            temp1.x=temp.x+dx[i];
            temp1.y=temp.y+dy[i];
            if(a[temp1.x][temp1.y]==0)continue;
            int val=a[temp1.x][temp1.y];
                int t1=(temp1.x-1)*n+temp1.y;
                if(viz[t1])continue;
            if(viz[val])
                {
                    q.push_back(temp1);
                    viz[t1]=1;
                    r=1;
                }
        }
        it++;
        }
    }
    cnt=q.size();
    printf("%d\n",cnt);
    return 0;
}