Skip to content

Commit 4ec88fa

Browse files
authored
Add solution1 for Day 03
1 parent 66e1c4d commit 4ec88fa

1 file changed

Lines changed: 145 additions & 0 deletions

File tree

  • Problems/Mathematics/Day-03/Vishva
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#include<bits/stdc++.h>
2+
#include<queue>
3+
using namespace std;
4+
typedef unordered_map<int, int> umii;
5+
typedef unordered_map<long long, long long> umll;
6+
typedef unordered_map<char, long long> umci;
7+
typedef vector<pair<int, int>> vpi;
8+
typedef vector<int> vi;
9+
typedef long long ll;
10+
typedef vector<long long> vll;
11+
typedef unordered_map<int , bool> umib;
12+
#define sum(v) accumulate(v.begin(), v.end(), 0)
13+
#define endl '\n'
14+
#define f0(i, n) for(long long i = 0; i < n; i++)
15+
#define f1(i, n) for(long long i = 1; i < n; i++)
16+
#define as(v) sort(v.begin(), v.end())
17+
#define all(x) (x).begin(), (x).end()
18+
#define pb push_back
19+
template<class T> umll frequency(vector<T> &v) {umll freq;for(auto &x:v) freq[x]++; return freq;}
20+
template<class T> umci S_frequency(vector<T> &v) {umci freq;for(auto &x:v) freq[x]++; return freq;}
21+
template <class T> void input(vector<T> &v){for(auto &x:v)cin>>x;}
22+
ll power(ll x, ll y){ ll res = 1; while (y > 0){ if (y & 1) res = (ll)(res*x); y = y>>1; x = (ll)(x*x); } return res; }
23+
void pvll(const vector<long long> &arr){for(auto it : arr){cout << it << " ";}cout << endl;}
24+
void pvi(const vector<int> &arr){for(auto it : arr){cout << it << " ";}cout << endl;}
25+
26+
const int N=7;
27+
ll ans =0;
28+
vector<vector<ll>> vis(N, vector<ll>(N, 0));
29+
string s;
30+
31+
bool inside(ll i,ll j)
32+
{
33+
// this is to check whether our pointer is in bounded area or not
34+
return i>=0 && i<N && j>=0 && j<N;
35+
}
36+
37+
void dfs(ll x,ll y,ll step)
38+
{
39+
// Base Condition
40+
41+
if(step == N*N-1 || (x== N-1 && y==0))
42+
{
43+
// if i reached bottom left with 48 steps
44+
if(step == N*N-1 && (x== N-1 && y==0))
45+
{
46+
47+
ans+=1;
48+
}
49+
return;
50+
}
51+
52+
// Trapped Optimisation
53+
54+
// above and below are visited(or out of bounds) and left and righ is free
55+
56+
if((!inside(x-1,y) || vis[x-1][y]) && (!inside(x+1,y) || vis[x+1][y]))
57+
{
58+
if(inside(x,y-1) && !vis[x][y-1] && inside(x,y+1) && !vis[x][y+1])
59+
{
60+
return;
61+
// TRAPPED
62+
}
63+
}
64+
65+
// left and right are visited/out of bound and up and down are free
66+
67+
if((!inside(x,y-1) || vis[x][y-1]) && (!inside(x,y+1) || vis[x][y+1]))
68+
{
69+
if(inside(x-1,y) && !vis[x-1][y] && inside(x+1,y) && !vis[x+1][y])
70+
{
71+
return;
72+
//TRAPPED
73+
}
74+
}
75+
76+
// mark the visited cell as one so we dont visit again
77+
78+
vis[x][y] =1;
79+
80+
if(s[step]=='?' || s[step] == 'L')
81+
{
82+
if(inside(x,y-1) && !vis[x][y-1])
83+
{
84+
dfs(x,y-1,step+1);
85+
}
86+
}
87+
if(s[step] == '?' || s[step] == 'R')
88+
{
89+
if(inside(x,y+1) && !vis[x][y+1])
90+
{
91+
dfs(x,y+1,step+1);
92+
}
93+
}
94+
if(s[step] == '?' || s[step] == 'U')
95+
{
96+
if(inside(x-1,y) && !vis[x-1][y])
97+
{
98+
dfs(x-1,y,step+1);
99+
}
100+
}
101+
if(s[step] == '?' || s[step] == 'D')
102+
{
103+
if(inside(x+1,y) && !vis[x+1][y])
104+
{
105+
dfs(x+1,y,step+1);
106+
}
107+
}
108+
109+
//backtracking after exploring one route now i will go back and will do vis[x][y] = 0;
110+
111+
vis[x][y]=0;
112+
113+
114+
115+
}
116+
117+
// SUBMISSION LINK:- https://cses.fi/problemset/result/15756456/
118+
119+
120+
121+
void solve(){
122+
cin >> s;
123+
124+
dfs(0,0,0);
125+
cout << ans << endl;
126+
127+
// TimeComplexity :- O(48*88414) == O(48*1e5)
128+
129+
//SpaceComplexity = O(49)
130+
131+
//-------------INPUT-------------
132+
133+
134+
135+
//-------------CODE--------------
136+
137+
138+
139+
}
140+
141+
142+
int main(){
143+
//int tt; cin >> tt; while(tt--)
144+
{solve();};
145+
}

0 commit comments

Comments
 (0)