Cod sursa(job #1646555)

Utilizator GeanaVladGeana Vlad GeanaVlad Data 10 martie 2016 16:36:00
Problema Castel Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <iostream>
#include<fstream>
using namespace std;
ifstream f("castel.in");
ofstream g("castel.out");
typedef struct coord
{
    int x,y;
};
coord c[23500];
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1},n,m,c1,i,j,p[155][155],n1,k1,k;
bool x[155][155],coada[23500];
bool OK(int i,int j)
{
    if(i<=n && i>=1 && j<=m && j>=1 && x[i][j]==0) return 1;
    return 0;
}
void lee()
{
    bool ok=0;
    for(i=1;i<=n1;i++)
        for(k=0;k<4;k++)
        if(OK(c[i].x+dx[k],c[i].y+dy[k]) && coada[p[c[i].x+dx[k]][c[i].y+dy[k]]])
        {
            n1++;
            c[n1].x=c[i].x+dx[k];
            c[n1].y=c[i].y+dy[k];
            x[c[n1].x][c[n1].y]=1;
            k1++;
            coada[(c[n1].x-1)*m+c[n1].y]=1;
            ok=1;
        }
    if(ok) lee();
}
int main()
{
    f>>n>>m>>c1;
    for(i=1;i<=n;i++)
    for(j=1;j<=m;j++)
        f>>p[i][j];

    c[1].x=c1/m+1;
    if(c1%m!=0) c[1].y=c1%m;
    else c[1].y=m;
    n1=1;k1=1;coada[c1]=1;x[c[1].x][c[1].y]=1;
    lee();
    g<<k1;
}