Problem Info
My Work
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (3.141592653589)
#define mod 1000000007
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
int ans;
string s;
vector<int> dir = {1, 0, -1, 0, 1}; // D,L,U,R
string dic = "DLUR";
void paths(int i, int j, vector<vector<int>> vis, int cnt)
{
// if it reaces the desired block, stop (increment ans if all blocks covered)
if (i == 6 && j == 0)
{
if (cnt == 48)
ans++;
return;
}
// if it can only go either left or right stop
if ((i > 0 && i < 6 && vis[i + 1][j] && vis[i - 1][j]) || (i == 6 && vis[i - 1][j]) || (i == 0 && vis[i + 1][j]))
return;
// try for each of four direction
for (int k = 0; k < 4; k++)
{
int ii = i + dir[k], jj = j + dir[k + 1];
//only go in that direction if it is in bounds, not visited and the move matches our pattern
if (ii >= 0 && ii < 7 && jj >= 0 && jj < 7 && vis[ii][jj] == 0 && (s[cnt] == '?' || dic[k] == s[cnt]))
{
vis[ii][jj] = 1;
paths(ii, jj, vis, cnt + 1);
vis[ii][jj] = 0;
}
}
}
int main()
{
fast;
cin >> s;
//2d array to keep track of visited blocks
vector<vector<int>> vis(7, vector<int>(7, 0));
ans = 0;
vis[0][0] = 1;
paths(0, 0, vis, 0);
cout << ans;
return 0;
}
Add explanation of code here
What I’ve Tried
I looked through carefully but can’t figure out why it is going into infinte loop( i assume).
When I compile it and run the program, it runs for 8 seconds and gives 0 output. And when I run it on CPH vscode extension, it again runs for 8 seconds, and gives output SIGTERM
.
Question
What is wrong with this code?
Thanks for the help.
Checklist
See How to ask for help on a problem for more information.
- Include a link to the problem
- Format your post (especially code blocks)
- Include what you’ve tried so far
- Add comments to your code
- Read the debugging module