Advertisement
a53

Labirint2_Of

a53
Jan 13th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include <fstream>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. ifstream fin ("labirint2.in");
  6. ofstream fout ("labirint2.out");
  7.  
  8. const int N = 405;
  9.  
  10. int dx[10][10], dy[10][10], n, b, s, k, a[N][N];
  11. short d[N][N];
  12. queue <pair <int, int> > Q;
  13.  
  14. int main() {
  15. fin >> n >> b >> s >> k;
  16. for (int x, y, i = 0; i < b; ++i) {
  17. fin >> x >> y;
  18. a[x-1][y-1]++;
  19. }
  20. for (int i = 0; i < n; ++i)
  21. for (int j = 0; j < n; ++j)
  22. if (a[i][j] != 1)
  23. a[i][j] = 0;
  24. for (int i = 0; i < s; ++i)
  25. for (int j = 0; j < k; ++j) {
  26. string s;
  27. fin >> s;
  28. if (s == "L")
  29. dx[i][j] = 0, dy[i][j] = -1;
  30. if (s == "R")
  31. dx[i][j] = 0, dy[i][j] = 1;
  32. if (s == "U")
  33. dx[i][j] = -1, dy[i][j] = 0;
  34. if (s == "D")
  35. dx[i][j] = 1, dy[i][j] = 0;
  36. }
  37. Q.push(make_pair (0, 0));
  38. d[0][0] = 1;
  39. while (Q.size()) {
  40. int x = Q.front().first;
  41. int y = Q.front().second;
  42. Q.pop();
  43. int xx = x;
  44. int yy = y;
  45. for (int i = 0; i < s; ++i) {
  46. xx = x;
  47. yy = y;
  48. bool ok = 1;
  49. for (int j = 0; j < k; ++j) {
  50. xx += dx[i][j];
  51. yy += dy[i][j];
  52. if (xx < 0 || yy < 0 || xx >= n || yy >= n || a[xx][yy]) {
  53. ok = 0;
  54. break;
  55. }
  56. }
  57. if (ok && !d[xx][yy]) {
  58. d[xx][yy] = d[x][y] + 1;
  59. Q.push(make_pair (xx, yy));
  60. }
  61. }
  62. }
  63. /*for (int i = 0; i < n; ++i) {
  64. for (int j = 0; j < n; ++j)
  65. fout << a[i][j] << " ";
  66. fout << "\n";
  67. }
  68. fout << "\n";*/
  69. /*for (int i = 0; i < n; ++i) {
  70. for (int j = 0; j < n; ++j)
  71. fout << d[i][j] << " ";
  72. fout << "\n";
  73. }*/
  74. if (!d[n-1][n-1])
  75. fout << "Imposibil!";
  76. else
  77. fout << d[n-1][n-1] - 1;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement