// Matrice bordată și scan line // Folosește 211 niveluri în stivă (pe world-map.in) #include #include #define MAX_N 5000 #define MAX_STACK 1000 #define NIL -1 typedef struct { int l, c1, c2; } stack; char a[MAX_N + 2][MAX_N + 2]; stack s[MAX_STACK]; int ss; int m, n; int maxSs; void push(int l, int c1, int c2) { s[ss++] = { l, c1, c2 }; if (ss > maxSs) { maxSs = ss; } } void pop(int *l, int *c1, int *c2) { ss--; *l = s[ss].l; *c1 = s[ss].c1; *c2 = s[ss].c2; } void fill(int l, int c) { int c1, c2; push(l, c, c); while (ss) { pop(&l, &c1, &c2); while (a[l][c1 - 1] == '.') { c1--; } while (a[l][c2 + 1] == '.') { c2++; } for (int i = c1; i <= c2; i++) { a[l][i] = 'o'; } for (int nl = l - 1; nl <= l + 1; nl += 2) { // liniile vecine int first = NIL; for (int i = c1; i <= c2; i++) { if (a[nl][i] == '.') { if (first == NIL) { first = i; } } else { if (first != NIL) { push(nl, first, i - 1); first = NIL; } } } if (first != NIL) { push(nl, first, c2); } } } } int main(void) { int l0, c0; FILE *f = fopen("fill.in", "r"); fscanf(f, "%d %d %d %d\n", &m, &n, &l0, &c0); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { a[i][j] = fgetc(f); } assert(fgetc(f) == '\n'); } fclose(f); fill(l0, c0); printf("Nivelul maxim: %d\n", maxSs); f = fopen("fill.out", "w"); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { fputc(a[i][j], f); } fputc('\n', f); } fclose(f); }