Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<fstream>
- using namespace std;
- int a[106][106];
- bool vizitat[106][106];
- int n, m;
- ifstream in("fill.in");
- ofstream out("fill.out");
- // vizitat[i][j] = 1 -> deja "umplut" celula (i,j)
- // vizitat[i][j] = 0 -> inca nu am "umplut" celula (i,j)
- void umple(int x, int y) {
- // ne aflam acum la celula (x,y)
- // efectuam procedeul dorit pe ea : in cazul nostru o marcam ca vizitata
- vizitat[x][y] = 1;
- // acum "propagam" functia peste vecinii lui (x,y) (daca se afla in matrice, nu i-am umplut deja si sunt zone de uscat)
- if(x + 1 < n && !vizitat[x + 1][y] && a[x + 1][y] == 1) umple(x + 1, y);
- if(x - 1 >= 0 && !vizitat[x - 1][y] && a[x - 1][y] == 1) umple(x - 1, y);
- if(y + 1 < m && !vizitat[x][y + 1] && a[x][y + 1] == 1) umple(x, y + 1);
- if(y - 1 >= 0 && !vizitat[x][y - 1] && a[x][y - 1] == 1) umple(x, y - 1);
- // aici se incheie functia
- }
- int main() {
- in >> n >> m;
- for(int i = 0; i < n; ++i) {
- for(int j = 0; j < m; ++j) {
- in >> a[i][j];
- }
- }
- int answer = 0;
- for(int i = 0; i < n; ++i) {
- for(int j = 0; j < m; ++j) {
- if(!vizitat[i][j] && a[i][j] == 1) {
- ++answer;
- umple(i, j);
- }
- }
- }
- out << answer << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment