Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define sfi(arg) scanf("%d", &arg)
  5. #define sfl(arg) scanf("%lld", &arg)
  6. #define pf printf
  7.  
  8. using namespace std;
  9.  
  10. ll key = 0;
  11. vector<vector<int>> field(9);
  12. map<pair<ll, pair<int, int>>, int> mp;
  13. int best = 0;
  14. string abc;
  15.  
  16. int fu( int cur, int x, int y ) {
  17.     if( mp.count(make_pair(key, make_pair(x, y))) == 1 ) {
  18.         best += mp[make_pair(key, make_pair(x, y))];
  19.         return mp[make_pair(key, make_pair(x, y))];
  20.     }
  21.  
  22.     if( cur == 48 && x == 7 && y == 1 ) {
  23.         best++;
  24.         return 1;
  25.     }
  26.  
  27.     if( x == 7 && y == 1 )
  28.         return 0;
  29.  
  30.     int total = 0;
  31.     if( abc[cur] == '?') {
  32.         if( field[x][y + 1] == 0 ) {
  33.             field[x][y + 1] = 1;
  34.             key ^= 1LL << ((x - 1) * 7 + y);
  35.             total += fu( cur + 1, x, y + 1);
  36.             field[x][y + 1] = 0;
  37.             key ^= 1LL << ((x - 1) * 7 + y);
  38.         }
  39.  
  40.         if( field[x][y - 1] == 0 ) {
  41.             field[x][y - 1] = 1;
  42.             key ^= 1LL << ((x - 1) * 7 + y - 2);
  43.             total += fu( cur + 1, x, y - 1);
  44.             field[x][y - 1] = 0;
  45.             key ^= 1LL << ((x - 1) * 7 + y - 2);
  46.         }
  47.  
  48.         if( field[x - 1][y] == 0 ) {
  49.             field[x - 1][y] = 1;
  50.             key ^= 1LL << ((x - 2) * 7 + y - 1);
  51.             total += fu( cur + 1, x - 1, y);
  52.             field[x - 1][y] = 0;
  53.             key ^= 1LL << ((x - 2) * 7 + y - 1);
  54.         }
  55.  
  56.         if( field[x + 1][y] == 0 ) {
  57.             field[x + 1][y] = 1;
  58.             key ^= 1LL << (x * 7 + y - 1);
  59.             total += fu( cur + 1, x + 1, y);
  60.             field[x + 1][y] = 0;
  61.             key ^= 1LL << (x * 7 + y - 1);
  62.         }
  63.     } else if(abc[cur] == 'R') {
  64.         if( field[x][y + 1] == 0 ) {
  65.             field[x][y + 1] = 1;
  66.             key ^= 1LL << ((x - 1) * 7 + y);
  67.             total += fu( cur + 1, x, y + 1);
  68.             field[x][y + 1] = 0;
  69.             key ^= 1LL << ((x - 1) * 7 + y);
  70.         }
  71.     } else if(abc[cur] == 'L') {
  72.         if( field[x][y - 1] == 0 ) {
  73.             field[x][y - 1] = 1;
  74.             key ^= 1LL << ((x - 1) * 7 + y - 2);
  75.             total += fu( cur + 1, x, y - 1);
  76.             field[x][y - 1] = 0;
  77.             key ^= 1LL << ((x - 1) * 7 + y - 2);
  78.         }
  79.     } else if(abc[cur] == 'U') {
  80.         if( field[x - 1][y] == 0 ) {
  81.             field[x - 1][y] = 1;
  82.             key ^= 1LL << ((x - 2) * 7 + y - 1);
  83.             total += fu( cur + 1, x - 1, y);
  84.             field[x - 1][y] = 0;
  85.             key ^= 1LL << ((x - 2) * 7 + y - 1);
  86.         }
  87.     } else {
  88.         if( field[x + 1][y] == 0 ) {
  89.             field[x + 1][y] = 1;
  90.             key ^= 1LL << (x * 7 + y - 1);
  91.             total += fu( cur + 1, x + 1, y);
  92.             field[x + 1][y] = 0;
  93.             key ^= 1LL << (x * 7 + y - 1);
  94.         }
  95.     }
  96.  
  97.    
  98.     mp[make_pair(key, make_pair(x, y))] = total;
  99. }
  100.  
  101. int main() {
  102.     for( int x = 0; x < 9; x++ ) {
  103.         for( int y = 0; y < 9; y++ ) {
  104.             field[x].push_back(0);
  105.         }
  106.     }
  107.  
  108.     for( int x = 0; x < 9; x++ ) {
  109.         field[0][x] = 1;
  110.         field[8][x] = 1;
  111.         field[x][0] = 1;
  112.         field[x][8] = 1;
  113.     }
  114.  
  115.     field[1][1] = 1;
  116.  
  117.     cin >> abc;
  118.  
  119.     fu( 0, 1, 1 );
  120.     pf("%d\n", best);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement