CSES - Shared codeLink to this code:
https://cses.fi/paste/530e118314bb7f3e986009/
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define ll long long int
#define rep(i, n) for(int i=0 ;i<n ;i++)
#define repp( i , begin , end) for(int i=begin ; i<end; i++)
#define take(b) for(int i=0; i<b.size(); i++)cin>>b[i]
#define takepair(a) for(int i=0; i<a.size(); i++)cin>> a[i].first >> a[i].second
#define sort01(a) sort(a.begin(),a.end())
#define sort10(a) sort(a.begin(),a.end(),greater<>())
#define endl "\n"
#define pii pair<int,int>
#define linear_output(a) for(auto i: a) cout<<i<<" ";cout<<endl
#define sorted_count(a,x) (upper_bound(a.begin(), a.end(), x)- lower_bound(a.begin(), a.end(), x))
#define ld long double
#define makep(a, b) make_pair(a, b)
template< typename T>
ll asqrt(T n)
{
ll l = -1, r = n + 1;
while (r - l > 1)
{
ll m = (r + l) >> 1;
if (m * m == n)
return m;
if (m * m < n)
l = m;
else
r = m;
}
return l;
}
template< typename T>
ll Asqrt(T n)
{
ll l = -1, r = n + 1;
while (r - l > 1)
{
ll m = (r + l) >> 1;
if (m * m == n)
return m;
if (m * m < n)
l = m;
else
r = m;
}
return r;
}
const int mode = 1e9 + 7;
const double eps = 1e-5;
#define MAX_N (int)(1e9+1e8)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////SOLUTION//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pair<int, int> dir[4]{ makep(0, 1), makep(0, -1), makep(-1, 0), makep(1, 0) };
bool visited[9][9];
vector<int> S;
int func(int row, int col, int i) {
if (visited[row][col])
return 0;
if (row == 7 && col == 1)
if (i >= 48)
return 1;
else
return 0;
if (!visited[row - 1][col] && !visited[row + 1][col] && visited[row][col - 1] && visited[row][col + 1])
return 0;
if (visited[row - 1][col] && visited[row + 1][col] && !visited[row][col - 1] && !visited[row][col + 1])
return 0;
visited[row][col] = true;
if (S[i] < 4) {
int ret = func(row + dir[S[i]].first, col + dir[S[i]].second, i + 1);
visited[row][col] = false;
return ret;
}
int ret = func(row, col + 1, i + 1) + func(row, col - 1, i + 1) + func(row - 1, col, i + 1) + func(row + 1, col, i + 1);
visited[row][col] = false;
return ret;
}
int main() {
/*ios_base::sync_with_stdio(false);
cin.tie(NULL);*/
string s;
cin >> s;
for (int i = 0; i < 8; i++)
visited[0][i] = true, visited[i][8] = true, visited[8][i + 1] = true, visited[i + 1][0] = true;
for (int i = 1; i <= 7; i++)
for (int j = 1; j <= 7; j++)
visited[i][j] = false;
S.resize(s.size());
for(int i = 0 ; i<s.size() ; i++)
switch (s[i])
{
case 'R':
S[i] = 0;
break;
case 'L':
S[i] = 1;
break;
case 'U':
S[i] = 2;
break;
case 'D':
S[i] = 3;
break;
default:
S[i] = 4;
break;
}
std::cout << func(1, 1, 0) << endl;
return 0;
}