Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 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. if (a[x - 1][y] != '*' && q!=-2)
  51. {
  52. if (q == 2) mas[x - 1][y] = min(mas[x][y],mas[x-1][y]);
  53. else mas[x - 1][y] = min(mas[x][y]+1, mas[x - 1][y]);
  54. rec(2, x - 1, y);
  55. }
  56. if (a[x + 1][y] != '*' && q!=2)
  57. {
  58. if (q == -2) mas[x + 1][y] = min(mas[x][y], mas[x + 1][y]);
  59. else mas[x + 1][y] = min(mas[x][y] + 1, mas[x + 1][y]);
  60. rec(-2, x + 1, y);
  61. }
  62. if (a[x][y-1] != '*' && q!=1)
  63. {
  64. if (q == -1) mas[x][y-1] = min(mas[x][y], mas[x][y-1]);
  65. else mas[x][y-1] = min(mas[x][y] + 1, mas[x ][y-1]);
  66. rec(-1, x, y - 1);
  67. }
  68. if (a[x][y + 1] != '*' && q!=-1)
  69. {
  70. if (q == 1) mas[x][y + 1] = min(mas[x][y], mas[x][y + 1]);
  71. else mas[x][y + 1] = min(mas[x][y] + 1, mas[x][y + 1]);
  72. rec(1, x, y + 1);
  73. }
  74. }
  75.  
  76. int main()
  77. {
  78. for (int i = 0; i <= 1001; i++)
  79. for (int j = 0; j <= 1001; j++)
  80. a[i][j] = '*',mas[i][j]=inf;
  81. int n, m, x1, y1, x2, y2;
  82. cin >> n >> m;
  83. for (int i = 1; i <= n; i++)
  84. {
  85. for (int j = 1; j <= m; j++)
  86. {
  87. char c;
  88. cin >> c;
  89. if (c == 'T') x2 = i, y2 = j;
  90. if (c == 'S') x1 = i, y1 = j;
  91. a[i][j] = c;
  92. }
  93. }
  94. mas[x1][y1] = 0;
  95. if (a[x1 - 1][y1] != '*') rec(2, x1, y1);
  96. if (a[x1 + 1][y1] != '*') rec(-2, x1, y1);
  97. if (a[x1][y1+1] != '*') rec(1, x1, y1);
  98. if (a[x1][y1-1] != '*') rec(-1, x1, y1);
  99. for (int i = 1; i <= n; i++)
  100. {
  101. for (int j = 1; j <= m; j++)
  102. cout << mas[i][j] << ' ';
  103. cout << endl;
  104. }
  105. if (mas[x2][y2] > 2) cout << "NO" << endl;
  106. else cout << "YES" << endl;
  107. }
  108.  
  109.  
  110. /*
  111. 4 4
  112. S.*.
  113. ..*.
  114. ..*.
  115. *..T
  116. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement