Informatică, întrebare adresată de jut, 9 ani în urmă

Cerința
Se dă o matrice reprezentând o zonă dintr-un ocean ce conține un iceberg; valorile egale cu 1 fac parte din iceberg, iar cele egale cu 0 reprezintă apă.

Se știe că icebergul este înconjurat de apa (nu există nici o valoare de 1 pe marginea matricei) și că într-un interval de timp se topesc toate zonele icebergului care au cel puțin doua laturi vecine cu apa.

Determinați și afișați cate intervale de timp sunt necesare ca icebergul să se topească în întregime. De asemenea, afișați pentru fiecare interval de timp câte poziții de gheață are icebergul la începutul intervalului.
Exemplu
Intrare

7
0 0 0 0 0 0 0
0 1 1 1 1 1 0
0 0 1 1 1 0 0
0 0 1 1 1 0 0
0 1 1 1 1 1 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Ieșire

4
16
12
8
2
in c++ va rog cat mai rapid si atuncea dau si coroana ;)

Răspunsuri la întrebare

Răspuns de stassahul
7
#include <bits/stdc++.h>

using namespace std;

int n,a[32][32],c[101],nr;

int main()
{

    cin >> n;

    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            cin >> a[i][j];

    bool ok=true;

    while(ok)
    {
        int b[33][33]={
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
        C=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
                if(a[i][j]==1)
                {
                    int nr=0;
                    C++;
                    b[i][j]=1;
                    if(a[i][j+1]==0 and b[i][j+1]==0) nr++;
                    if(a[i][j-1]==0 and b[i][j-1]==0) nr++;
                    if(a[i+1][j]==0 and b[i+1][j]==0) nr++;
                    if(a[i-1][j]==0 and b[i-1][j]==0) nr++;
                    if(nr>1) a[i][j]=0;
                }
        }
        if(C==0) ok=false;
            else c[nr++]=C;
    }

    cout << nr << endl;

    for(int i=0;i<nr;i++)
        cout << c[i] << endl;

    return 0;

}

stassahul: Ok, in loc de b-ul cela mare se poate de facut for(int i=1;i<=33;i++) for(int j=1;j<=33;j++) b[i][j]=0. Si sa completezi fix astfel, insa vei avea limita cred, acush controlez.
Zlatan: Da. Nu e mai clar aşa?
Cactus: b[33][33]={0,0}; nu e bine?
stassahul: Pe pbinfo nu e bine sa initializezi asa
stassahul: Dupa cite stiu eu. In codeblocks se poate
stassahul: Totusi ai dreptate, chiar daca faci prin 2 foruri si il completezi nu primesti limita de timp
stassahul: Daca doresti poti sami raportezi raspunsul si il voi edita, punind solutia cu for(int i=1;i<=33;i++) for(int j=1;j<=33;j++) b[i][j]=0.
Zlatan: Nu-l raportez, nu ăsta e scopul . :D
Zlatan: Cât timp e corect, n-ai de ce să faci asta.
stassahul: Spun in caz ca autorul nu va vedea comentariile.
Alte întrebări interesante