Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #define Nmax 102
- using namespace std;
- int a[Nmax][Nmax],b[Nmax][Nmax],n,m;
- void citire()
- {
- ifstream f("pereti.in");
- f>>n>>m;
- for(int i=1;i<=n;++i)
- for(int j=1;j<=m;++j)
- f>>a[i][j];
- f.close();
- }
- inline int limite(int i,int j)
- {
- if(i<0||i>n+1||j<0||j>m+1)
- return 0;
- return 1;
- }
- void umple(int i,int j)
- {
- b[i][j]=1;
- if(limite(i-1,j)&&b[i-1][j]==0&&((a[i][j]&8)==0))
- umple(i-1,j);
- if(limite(i+1,j)&&b[i+1][j]==0&&((a[i][j]&2)==0))
- umple(i+1,j);
- if(limite(i,j+1)&&b[i][j+1]==0&&((a[i][j]&4)==0))
- umple(i,j+1);
- if(limite(i,j-1)&&b[i][j-1]==0&&((a[i][j]&1)==0))
- umple(i,j-1);
- }
- void bordare()
- {
- for(int j=1;j<=m;++j) /// linia 0:
- if((a[1][j]&8)!=0)
- a[0][j]=2;
- for(int j=1;j<=m;++j) /// linia n+1
- if((a[n][j]&2)!=0)
- a[n+1][j]=8;
- for(int i=1;i<=n;++i) /// coloana 0
- if((a[i][1]&1)!=0)
- a[i][0]=4;
- for(int i=1;i<=n;++i) /// coloana m+1
- if((a[i][m]&4)!=0)
- a[i][m+1]=1;
- }
- int numarare()
- {
- int nr=0;
- umple(0,0);
- for(int i=0;i<=n+1;++i)
- for(int j=0;j<=m+1;++j)
- if(b[i][j]==1)
- {
- if((a[i][j]&8)!=0)
- ++nr;
- if((a[i][j]&4)!=0)
- ++nr;
- if((a[i][j]&2)!=0)
- ++nr;
- if((a[i][j]&1)!=0)
- ++nr;
- }
- return nr;
- }
- void afisare()
- {
- ofstream g("pereti.out");
- g<<numarare()<<'\n';
- g.close();
- }
- int main()
- {
- citire();
- bordare();
- afisare();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement