Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- using namespace std;
- FILE *fin, *fout;
- int n, m, ic, sc, M[1010][1010], M1[1010][1010], M2[1010][1010], M3[1010][1010], M4[1010][1010], p, q, r;
- int dl[4] = {-1, 0, 1, 0}, dc[4] = {0, 1, 0, -1};
- char c, c1, c2, c3, c4;
- struct coada
- {
- int l, c;
- };
- coada C[1000010], acum, urm;
- void citire();
- void bordare(int A[1010][1010]);
- void lee(int x, int y, int A[1010][1010]);
- int comparare(int a, int b, int c, int d);
- int main()
- {
- fin = fopen("imperii.in", "r");
- fout = fopen("imperii.out", "w");
- citire();
- bordare(M1);
- bordare(M2);
- bordare(M3);
- bordare(M4);
- lee(1, 1, M1);
- lee(1, m, M2);
- lee(n, 1, M3);
- lee(n, m, M4);
- for (p = 1; p <= n; p++)
- {
- for (q = 1; q <= m; q++)
- {
- if (M[p][q] == -1)
- {
- fprintf(fout, "#");
- continue;
- }
- r = comparare(M1[p][q], M2[p][q], M3[p][q], M4[p][q]);
- if (r == 1)
- fprintf(fout, "%c", c1);
- else if (r == 2)
- fprintf(fout, "%c", c2);
- else if (r == 3)
- fprintf(fout, "%c", c3);
- else if (r == 4)
- fprintf(fout, "%c", c4);
- else
- fprintf(fout, "-");
- }
- fprintf(fout, "\n");
- }
- return 0;
- }
- void citire()
- {
- int i, j;
- fscanf(fin, "%d%d\n", &n, &m);
- for (i = 1; i <= n; i++)
- {
- for (j = 1; j <= m; j++)
- {
- fscanf(fin, "%c", &c);
- if (c == '#')
- M[i][j] = -1;
- if (i == 1 && j == 1)
- c1 = c;
- else if (i == 1 && j == m)
- c2 = c;
- else if (i == n && j == 1)
- c3 = c;
- else if (i == n && j == m)
- c4 = c;
- }
- fscanf(fin, "\n");
- }
- }
- void bordare(int A[1010][1010])
- {
- int i, j;
- for (i = 0; i <= n + 1; i++)
- A[i][0] = A[i][m + 1] = -1;
- for (j = 0; j <= m + 1; j++)
- A[0][j] = A[n + 1][j] = -1;
- }
- void lee(int x, int y, int A[1010][1010])
- {
- int k;
- ic = 0;
- sc = -1;
- C[++sc].l = x;
- C[sc].c = y;
- A[x][y] = 1;
- while (ic <= sc)
- {
- acum = C[ic++];
- for (k = 0; k < 4; k++)
- {
- urm.l = acum.l + dl[k];
- urm.c = acum.c + dc[k];
- if (!M[urm.l][urm.c] && !A[urm.l][urm.c])
- {
- A[urm.l][urm.c] = A[acum.l][acum.c] + 1;
- C[++sc] = urm;
- }
- }
- }
- }
- int comparare(int a, int b, int c, int d)
- {
- if (a < b && a < c && a < d && a > 0)
- return 1;
- if (b < a && b < c && b < d && b > 0)
- return 2;
- if (c < a && c < b && c < d && c > 0)
- return 3;
- if (d < a && d < b && d < c && d > 0)
- return 4;
- return 0;
- }
Add Comment
Please, Sign In to add comment