Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<fstream>
- using namespace std;
- ifstream in("lac.in");
- ofstream out("lac.out");
- int n, m;
- int dx[4] = {0, 0, -1, 1}; // directii pe axa x
- int dy[4] = {1, -1, 0, 0}; // directii pe axa y
- int answer = 0;
- int a[1006][1006];
- bool vizitat[1006][1006];
- void umple(int x, int y) {
- vizitat[x][y] = 1;
- // marcam celula curenta ca vizitata
- for(int d = 0; d < 4; ++d) {
- int nx = x + dx[d];
- int ny = y + dy[d];
- // (nx, ny) noua celula
- if(nx >= 0 && ny >= 0 && nx < n && ny < m && vizitat[nx][ny] == 0 && a[nx][ny] == 1) {
- umple(nx, ny);
- }
- }
- }
- int main() {
- in >> n >> m;
- for(int i = 0; i < n; ++i) {
- for(int j = 0; j < m; ++j) {
- in >> a[i][j];
- }
- }
- // incercam sa efectuam "umplerea" incepand cu o celula de pe margine
- // in acest pas al algoritmul vom "umple" peninsulele
- int peninsule = 0;
- for(int i = 0; i < n; ++i) {
- for(int j = 0; j < m; ++j) {
- if( (i == 0 || j == 0 || i == n - 1 || j == m - 1) && a[i][j] == 1 && vizitat[i][j] == 0) {
- umple(i, j);
- ++peninsule;
- }
- }
- }
- int insule = 0;
- for(int i = 1; i < n - 1; ++i) {
- for(int j = 1; j < m - 1; ++j) {
- if(a[i][j] == 1 && vizitat[i][j] == 0) {
- umple(i, j);
- ++insule;
- }
- }
- }
- out << insule << ' ' << peninsule << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment