Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 100 puncte
- Implementare -Dan Pracsiu
- Complexitate O(mxn)
- */
- #include<fstream>
- #define InFile "tsunami.in"
- #define OutFile "tsunami.out"
- using namespace std;
- struct coord
- {
- short x, y;
- };
- short a[1009][1009], b[1009][1009],L,C,h;
- coord q[1000001];
- int top,sol;
- int main()
- {
- int i,j;
- ifstream fin(InFile);
- fin>>L>>C>>h;
- for(i=1;i<=L;++i)
- for(j=1;j<=C;++j)
- fin>>a[i][j];
- fin.close();
- for(i=0;i<=C+1;++i) /// Bordare
- a[0][i]=a[L +1][i]=1001;
- for(i=0;i<=L+1;++i)
- a[i][0]=a[i][C+1]=1001;
- sol=0; ///init fill
- top=-1;
- for(i=1;i<=C;++i)
- if(a[1][i]==0)
- b[1][i]=1,q[++top].x=1,q[top].y=i;
- for(i=1;i<=C;++i)
- if(a[L][i]==0)
- b[L][i]=1,q[++top].x=L,q[top].y=i;
- for(i=1;i<=L;++i)
- if(a[i][1]==0)
- b[i][1]=1,q[++top].x=i,q[top].y=1;
- for(i=1;i<=L;++i)
- if (a[i][C]==0)
- b[i][C]=1,q[++top].x=i,q[top].y=C;
- while(top>=0) /// fill
- {
- i=q[top].x;
- j=q[top].y;
- --top;
- if(b[i-1][j]!=1&&a[i-1][j]<h)
- {
- b[i-1][j]=1;
- ++top;
- q[top].x=i-1;
- q[top].y=j;
- if(a[i-1][j]>0)
- ++sol;
- }
- if(b[i+1][j]!=1&&a[i+1][j]<h)
- {
- b[i+1][j]=1;
- ++top;
- q[top].x =i+1;
- q[top].y=j;
- if(a[i+1][j]>0)
- ++sol;
- }
- if(b[i][j-1]!=1&&a[i][j-1]<h)
- {
- b[i][j-1]=1;
- ++top;
- q[top].x=i;
- q[top].y=j-1;
- if(a[i][j-1]>0)
- ++sol;
- }
- if(b[i][j+1]!=1&&a[i][j+1]<h)
- {
- b[i][j+1]=1;
- ++top;
- q[top].x=i;
- q[top].y=j+1;
- if(a[i][j+1]>0)
- ++sol;
- }
- }
- ofstream fout(OutFile);
- fout<<sol<<'\n';
- fout.close();
- return 0;
- }
Add Comment
Please, Sign In to add comment