Advertisement
VladNitu

RollTheBall

Dec 10th, 2022 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #pragma once
  2. #define NMAX 2023
  3. #define ll long long
  4. #define MOD (ll)(1e9 + 7)
  5.  
  6. #include <bits/stdc++.h>
  7.  
  8. using namespace std;
  9. ifstream f("02A.in");
  10. ofstream g("02A.out");
  11. int N, M, sz;
  12. const vector<int> dx{-1, 1, 0, 0};
  13. const vector<int> dy{0, 0, -1, 1};
  14. ll ans = 0;
  15. int m[2][NMAX];
  16. bool viz[2][NMAX];
  17. string word;
  18.  
  19.  
  20. // Roll the ball BUNA 11/20 Back
  21. bool inGrid(int x, int y) {
  22. return (x >= 0 && x < N && y >= 0 && y < M);
  23. }
  24.  
  25. void DFS(int x, int y, int p) {
  26. viz[x][y] = true;
  27. if (p == word.size())
  28. ans = (ans + 1) % MOD;
  29.  
  30. for (int i = 0; i < 4; ++i) {
  31. int xx = x + dx[i];
  32. int yy = y + dy[i];
  33. if (!viz[xx][yy] && inGrid(xx, yy) && m[xx][yy] == word[p])
  34. DFS(xx, yy, p + 1);
  35.  
  36. }
  37.  
  38. viz[x][y] = false;
  39.  
  40.  
  41. }
  42.  
  43.  
  44. void reset() {
  45. for (int i = 0; i < N; ++i)
  46. for (int j = 0; j < M; ++j)
  47. viz[i][j] = false;
  48. return;
  49. }
  50.  
  51. int main() {
  52.  
  53. string s1, s2;
  54. cin >> s1 >> s2;
  55. for (int j = 0; j < s1.size(); ++j)
  56. m[0][j] = s1[j];
  57.  
  58. for (int j = 0; j < s2.size(); ++j)
  59. m[1][j] = s2[j];
  60. cin >> word;
  61.  
  62. char pref = word[0];
  63. N = 2;
  64. M = s1.size();
  65. for (int i = 0; i < N; ++i)
  66. for (int j = 0; j < M; ++j)
  67. if (m[i][j] == pref) {
  68. DFS(i, j, 1);
  69. reset();
  70. }
  71.  
  72.  
  73. cout << ans << '\n';
  74. return 0;
  75. }
  76.  
  77.  
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement