-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisole.cpp
118 lines (93 loc) · 2.16 KB
/
isole.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
class isola
{
public:
explicit isola( int spazio[5][11]){for(int x=0; x!=5; ++x) for(int j=0; j!=11; ++j) k[x][j]=spazio[x][j];}
void stampa(){for(int x=0; x!=5; ++x){std::cout<<std::endl; for(int j=0; j!=11; ++j) std::cout<<k[x][j]<<" ";}}
inline int get_val(int indice1, int indice2){return k[indice1][indice2];}
void set_val(int indice1, int indice2, int val){k[indice1][indice2]=val;}
bool controllo_2(int indice1, int indice2);
void controllo_1(int indice1, int indice2);
void setta(int indice1, int indice2);
void setta2(int indice1,int indice2);
void conta_1();
friend std::ostream& operator<<(std::ostream& os,const isola& a);
void trova(int zona[5][11]);
private:
int isole=0;
int k[5][11];
};
std::ostream& operator << (std::ostream& os,const isola& a){os<<a.isole; return os;}
bool isola::controllo_2(int x, int j)
{
if(get_val(x,j)==2){return true;}
return false;
}
void isola::controllo_1(int x, int j)
{
for(int x=0; x!=5; ++x)
for(int u=0; u!=11; ++u)
if(get_val(x,j)==1)
if(get_val(x,j+1)==3 && get_val(x-1,j+1)==1) set_val(x-1, j+1,0);
}
void isola::setta(int x, int j)
{
if(get_val(x,j+1)==1) set_val(x,j+1, 2);
if(get_val(x,j-1)==1) set_val(x,j-1, 2);
if(get_val(x+1,j)==1) set_val(x+1,j, 2);
if(get_val(x-1,j)==1) set_val(x-1,j, 2);
}
void isola::setta2(int x, int j)
{
set_val(x,j+1,3);
set_val(x,j,3);
set_val(x+1,j,3);
}
void isola::conta_1()
{
for(int x=0; x!=5; ++x)
for(int u=0; u!=11; ++u)
if(get_val(x,u)==1)
++isole;
}
void isola::trova(int zona[5][11])
{
for(int x=0; x!=5; ++x)
for(int u=0; u!=11; ++u)
{
if(zona[x][u]==1)
{
if (!(controllo_2(x,u)))
{
setta(x,u);
}
}
}
for(int x=0; x!=5; ++x)
for(int u=0; u!=11; ++u)
{
if (controllo_2(x,u))
{
setta2(x,u);
}
}
for(int x=0; x!=5; ++x)
for(int u=0; u!=11; ++u)
{
controllo_1(x,u);
}
conta_1();
}
int main()
{
int a[5][11]{{0,0,0,0,0,0,0,0,0,1,1},
{0,1,0,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,1,1,0,0},
{0,1,1,1,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,1,0,1}};
isola kl(a);
kl.stampa();
kl.trova(a);
std::cout<<"\n\n";
std::cout<<"le isole in totale sono: "<<kl;
}