Can I know the computational complexity and space complexity and time complexity? #include using namespa

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Can I know the computational complexity and space complexity and time complexity? #include using namespa

Post by answerhappygod »

Can I know the computational complexity and space complexity and
time complexity?
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
vector<string> printPath(int m[MAX][MAX], int row,int
col,int sx,int sy,int dx,int dy);
int main()
{
int t;
cout<<"Enter The Number Of Test
Cases:\n";
cin >> t;
while (t--)
{
int
row,dx,dy,sx,sy,col;
cout<<"Enter Row And Col
Size of matrix:\n";
cin>>row>>col;
int m[MAX][MAX];
cout<<"Enter Matrix
Entries:\n";
for (int i = 0; i < col; i++)
{
for (int j = 0; j
< row; j++)
{
cin
>> m[j][col-i-1];
}
}
cout<<"Enter the source
and destination of x and y
co-ordinates:"<<endl;
cin>>sx>>sy>>dx>>dy;
vector<string> result =
printPath(m,row,col,sx,sy,dx,dy);
if (result.size() ==
0)
cout <<
"There is No Path From Source To Destination!\n";
else
cout <<
"There is a Path From Source To Destination.\n";
cout <<
endl;
}
return 0;
}
vector<vector<bool> > visit;
void f(int m[MAX][MAX],int row,int col,int sx,int sy,int
dx,int dy,string temp,vector<string> &ans)
{
if(sx<0||sx>=row||sy<0||sy>=col||visit[sx][sy]==true||m[sx][sy]==0)
{
return;
}
if(sx==dx&&sy==dy)
{
ans.push_back(temp);
}
visit[sx][sy]=true;
f(m,row,col,sx-1,sy,dx,dy,temp+"U",ans);
f(m,row,col,sx+1,sy,dx,dy,temp+"D",ans);
f(m,row,col,sx,sy-1,dx,dy,temp+"R",ans);
f(m,row,col,sx,sy+1,dx,dy,temp+"L",ans);
visit[sx][sy]=false;
}
vector<string> printPath(int m[MAX][MAX], int row,int
col,int sx,int sy,int dx,int dy)
{
vector<string> ans;
visit.resize(row,vector<bool>(col,false));
f(m,row,col,sx,sy,dx,dy,"",ans);
sort(ans.begin(),ans.end());\
return ans;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply