a53

Imperii

a53
May 21st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4.  
  5. FILE *fin, *fout;
  6.  
  7. int n, m, ic, sc, M[1010][1010], M1[1010][1010], M2[1010][1010], M3[1010][1010], M4[1010][1010], p, q, r;
  8. int dl[4] = {-1, 0, 1, 0}, dc[4] = {0, 1, 0, -1};
  9. char c, c1, c2, c3, c4;
  10.  
  11. struct coada
  12. {
  13. int l, c;
  14. };
  15. coada C[1000010], acum, urm;
  16.  
  17. void citire();
  18. void bordare(int A[1010][1010]);
  19. void lee(int x, int y, int A[1010][1010]);
  20. int comparare(int a, int b, int c, int d);
  21.  
  22. int main()
  23. {
  24. fin = fopen("imperii.in", "r");
  25. fout = fopen("imperii.out", "w");
  26. citire();
  27. bordare(M1);
  28. bordare(M2);
  29. bordare(M3);
  30. bordare(M4);
  31. lee(1, 1, M1);
  32. lee(1, m, M2);
  33. lee(n, 1, M3);
  34. lee(n, m, M4);
  35. for (p = 1; p <= n; p++)
  36. {
  37. for (q = 1; q <= m; q++)
  38. {
  39. if (M[p][q] == -1)
  40. {
  41. fprintf(fout, "#");
  42. continue;
  43. }
  44. r = comparare(M1[p][q], M2[p][q], M3[p][q], M4[p][q]);
  45. if (r == 1)
  46. fprintf(fout, "%c", c1);
  47. else if (r == 2)
  48. fprintf(fout, "%c", c2);
  49. else if (r == 3)
  50. fprintf(fout, "%c", c3);
  51. else if (r == 4)
  52. fprintf(fout, "%c", c4);
  53. else
  54. fprintf(fout, "-");
  55. }
  56. fprintf(fout, "\n");
  57. }
  58. return 0;
  59. }
  60. void citire()
  61. {
  62. int i, j;
  63. fscanf(fin, "%d%d\n", &n, &m);
  64. for (i = 1; i <= n; i++)
  65. {
  66. for (j = 1; j <= m; j++)
  67. {
  68. fscanf(fin, "%c", &c);
  69. if (c == '#')
  70. M[i][j] = -1;
  71. if (i == 1 && j == 1)
  72. c1 = c;
  73. else if (i == 1 && j == m)
  74. c2 = c;
  75. else if (i == n && j == 1)
  76. c3 = c;
  77. else if (i == n && j == m)
  78. c4 = c;
  79. }
  80. fscanf(fin, "\n");
  81. }
  82. }
  83. void bordare(int A[1010][1010])
  84. {
  85. int i, j;
  86. for (i = 0; i <= n + 1; i++)
  87. A[i][0] = A[i][m + 1] = -1;
  88. for (j = 0; j <= m + 1; j++)
  89. A[0][j] = A[n + 1][j] = -1;
  90. }
  91. void lee(int x, int y, int A[1010][1010])
  92. {
  93. int k;
  94. ic = 0;
  95. sc = -1;
  96. C[++sc].l = x;
  97. C[sc].c = y;
  98. A[x][y] = 1;
  99. while (ic <= sc)
  100. {
  101. acum = C[ic++];
  102. for (k = 0; k < 4; k++)
  103. {
  104. urm.l = acum.l + dl[k];
  105. urm.c = acum.c + dc[k];
  106. if (!M[urm.l][urm.c] && !A[urm.l][urm.c])
  107. {
  108. A[urm.l][urm.c] = A[acum.l][acum.c] + 1;
  109. C[++sc] = urm;
  110. }
  111. }
  112. }
  113. }
  114. int comparare(int a, int b, int c, int d)
  115. {
  116. if (a < b && a < c && a < d && a > 0)
  117. return 1;
  118. if (b < a && b < c && b < d && b > 0)
  119. return 2;
  120. if (c < a && c < b && c < d && c > 0)
  121. return 3;
  122. if (d < a && d < b && d < c && d > 0)
  123. return 4;
  124. return 0;
  125. }
Add Comment
Please, Sign In to add comment