Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <set>
  6. #include <cmath>
  7. #include <map>
  8. #include <queue>
  9. #include <iomanip>
  10. #include <cstdlib>
  11. #include <stdio.h>
  12. #include <ctime>
  13. #include <stack>
  14. #include <cmath>
  15. #include <sstream>
  16. #include <iostream>
  17. #include <unordered_map>
  18. #include <functional>
  19.  
  20. #define X first
  21. #define Y second
  22. #define mp make_pair
  23. #define All(x) (x).begin(),(x).end()
  24. #define f_in freopen("input.txt", "r", stdin)
  25. #define f_out freopen("output.txt", "w", stdout)
  26. #define file_on f_in; f_out
  27.  
  28. typedef long long ll;
  29. typedef unsigned long long ull;
  30. typedef long double ld;
  31.  
  32. using namespace std;
  33.  
  34. const ll linf = 10000000000000043;
  35. const int inf = 1000000043;
  36.  
  37. using namespace std;
  38.  
  39. char a[1010][1010];
  40. int mas[1010][1010];
  41. /*
  42. < -1
  43. ^ 2
  44. > 1
  45. -2
  46. */
  47.  
  48. void rec(int q, int x, int y)
  49. {
  50. //cout << x << y << endl;
  51. if (a[x - 1][y] != '*')
  52. {
  53. int pre = mas[x - 1][y];
  54. if (q == 2) mas[x - 1][y] = min(mas[x][y],mas[x-1][y]);
  55. else mas[x - 1][y] = min(mas[x][y]+1, mas[x - 1][y]);
  56. if (pre > mas[x - 1][y]) rec(2, x - 1, y);
  57. }
  58. if (a[x + 1][y] != '*')
  59. {
  60. int pre = mas[x + 1][y];
  61. if (q == -2) mas[x + 1][y] = min(mas[x][y], mas[x + 1][y]);
  62. else mas[x + 1][y] = min(mas[x][y] + 1, mas[x + 1][y]);
  63. if (pre > mas[x + 1][y]) rec(-2, x + 1, y);
  64. }
  65. if (a[x][y-1] != '*')
  66. {
  67. int pre = mas[x][y-1];
  68. if (q == -1) mas[x][y-1] = min(mas[x][y], mas[x][y-1]);
  69. else mas[x][y-1] = min(mas[x][y] + 1, mas[x ][y-1]);
  70. if (pre > mas[x][y-1]) rec(-1, x, y - 1);
  71. }
  72. if (a[x][y + 1] != '*')
  73. {
  74. int pre = mas[x][y+1];
  75. if (q == 1) mas[x][y + 1] = min(mas[x][y], mas[x][y + 1]);
  76. else mas[x][y + 1] = min(mas[x][y] + 1, mas[x][y + 1]);
  77. if (pre > mas[x][y+1]) rec(1, x, y + 1);
  78. }
  79. }
  80.  
  81. int main()
  82. {
  83. for (int i = 0; i <= 1001; i++)
  84. for (int j = 0; j <= 1001; j++)
  85. a[i][j] = '*',mas[i][j]=inf;
  86. int n, m, x1, y1, x2, y2;
  87. cin >> n >> m;
  88. for (int i = 1; i <= n; i++)
  89. {
  90. for (int j = 1; j <= m; j++)
  91. {
  92. char c;
  93. cin >> c;
  94. if (c == 'T') x2 = i, y2 = j;
  95. if (c == 'S') x1 = i, y1 = j;
  96. a[i][j] = c;
  97. }
  98. }
  99. mas[x1][y1] = 0;
  100. if (a[x1 - 1][y1] != '*') rec(2, x1, y1);
  101. if (a[x1 + 1][y1] != '*') rec(-2, x1, y1);
  102. if (a[x1][y1+1] != '*') rec(1, x1, y1);
  103. if (a[x1][y1-1] != '*') rec(-1, x1, y1);
  104. for (int i = 1; i <= n; i++)
  105. {
  106. for (int j = 1; j <= m; j++)
  107. cout << mas[i][j] << ' ';
  108. cout << endl;
  109. }
  110. if (mas[x2][y2] > 2) cout << "NO" << endl;
  111. else cout << "YES" << endl;
  112. }
  113.  
  114.  
  115. /*
  116. 4 4
  117. S.*.
  118. ..*.
  119. ..*.
  120. *..T
  121. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement