2D vector subscript out of range - 2d-vector

It says vector subscript out of range when achieving if statement. I think I added extra int to floor, or extra floor vector to 2D vector. I am using VS 2010(C++)
I tried to find it on other questions but not succeeded.
bool is_perfect_square(int);
int main()
{
int cust;
vector<int>floor;
vector<vector<int>>hotel;
floor.push_back(0);
hotel.push_back(floor);
hotel[0][0]=1;
for(cust=2; ; cust++)
{
for(int i=0; i<=hotel.size(); i++)
{
if(is_perfect_square(cust+hotel[i][floor.size()]))
{
floor.push_back(0);
hotel[i][cust]=cust;
break;
}
else
{
hotel.push_back(floor);
hotel[hotel.size()][0]=cust;
}
}
}
int sum=0;
for(int a=1; a<=hotel.size(); a++)
{
for(int b=1; b<=floor.size(); b++)
{
if(pow(a-1,2)+pow(b-1,2)==14234886498625)
sum+=hotel[a-1][b-1];
}
}
cout<<sum<<endl;
system("pause");
return 0;
}
bool is_perfect_square(int n)
{
int root=floor(sqrt(n));
return n == root * root;
}

I put my answers in the comments.
bool is_perfect_square(int);
int main()
{
int cust;
vector<int>floor;
vector<vector<int>>hotel;
floor.push_back(0);
hotel.push_back(floor);
hotel[0][0]=1;
// you may not be able to get out of this loop
// because the "break" below only exits one level of the loop.
for(cust=2; ; cust++)
{
// this should be changed to "i<hotel.size()",
// because the subscript of a vector ranges from 0 to its size minus one.
for(int i=0; i<=hotel.size(); i++)
{
// here "floor.size()" should be floor.size() - 1, the same reason.
if(is_perfect_square(cust+hotel[i][floor.size()]))
{
floor.push_back(0);
hotel[i][cust]=cust;
break;
}
else
{
hotel.push_back(floor);
hotel[hotel.size()][0]=cust;
}
}
}
int sum=0;
for(int a=1; a<=hotel.size(); a++)
{
for(int b=1; b<=floor.size(); b++)
{
if(pow(a-1,2)+pow(b-1,2)==14234886498625)
sum+=hotel[a-1][b-1];
}
}
cout<<sum<<endl;
system("pause");
return 0;
}
bool is_perfect_square(int n)
{
int root=floor(sqrt(n));
return n == root * root;
}

Related

Can I say that this algorithm can solve the n-queens problem?

#include<iostream>
#include<algorithm>
#define INT unsigned __int128
using namespace std;
INT base[25][25],lit[25],n,error=0;
void expbase(INT p,INT q)
{
for(INT i=1;i<=n;i++)
{
base[p][i]=1;
base[i][q]=1;
}
INT cp=p,cq=q;
while(cp>=1&&cp<=n&&cq>=1&&cq<=n)
{
base[cp][cq]=1;
cp--;
cq++;
}
cp=p,cq=q;
while(cp>=1&&cp<=n&&cq>=1&&cq<=n)
{
base[cp][cq]=1;
cp++;
cq--;
}
cp=p,cq=q;
while(cp>=1&&cp<=n&&cq>=1&&cq<=n)
{
base[cp][cq]=1;
cp--;
cq--;
}
cp=p,cq=q;
while(cp>=1&&cp<=n&&cq>=1&&cq<=n)
{
base[cp][cq]=1;
cp++;
cq++;
}
base[p][q]=2;
return;
}
void make(INT ed)
{
INT d[25][25]={0,},ch=0,mult=1;
for(INT i=1;i<=n;i++)
{
INT cop[25][25]={0,},co=0;
if(lit[i])continue;
for(INT p=1;p<=n;p++)
{
for(INT q=1;q<=n;q++)
{
cop[p][q]=d[p][q];
d[p][q]=0;
}
}
if(i==ed)
{
/*Insert Queen*/
for(INT j=1;j<=n;j++)
{
if(cop[i][j]!=(1<<mult)-1&&!base[i][j]){expbase(i,j);return;}
else if(j==n){error=1;return;}
}
}
else
{
/*Check the possibility*/
for(INT j=1;j<=n;j++)
{
if(!base[i][j])
{
INT ch[25][25]={0,};
for(INT p=1;p<=n;p++)
for(INT q=1;q<=n;q++)
d[p][q]=(d[p][q]<<mult);
for(INT p=1;p<=n;p++)if(!ch[i][p]){d[i][p]+=(1<<mult)-1;ch[i][p]=1;}
for(INT p=1;p<=n;p++)if(!ch[p][j]){d[p][j]+=(1<<mult)-1;ch[p][j]=1;}
INT ci=i,cj=j;
while(ci>=1&&ci<=n&&cj>=1&&cj<=n)
{
if(!ch[ci][cj]){d[ci][cj]+=(1<<mult)-1;ch[ci][cj]=1;}
ci--;cj++;
}
ci=i,cj=j;
while(ci>=1&&ci<=n&&cj>=1&&cj<=n)
{
if(!ch[ci][cj]){d[ci][cj]+=(1<<mult)-1;ch[ci][cj]=1;}
ci++;cj++;
}
ci=i,cj=j;
while(ci>=1&&ci<=n&&cj>=1&&cj<=n)
{
if(!ch[ci][cj]){d[ci][cj]+=(1<<mult)-1;ch[ci][cj]=1;}
ci++;cj--;
}
ci=i,cj=j;
while(ci>=1&&ci<=n&&cj>=1&&cj<=n)
{
if(!ch[ci][cj]){d[ci][cj]+=(1<<mult)-1;ch[ci][cj]=1;}
ci--;cj--;
}
for(INT p=1;p<=n;p++)
for(INT q=1;q<=n;q++)
if(!ch[p][q])d[p][q]+=(cop[p][q]|cop[i][j]);
co++;
}
}
mult*=co;
}
}
return;
}
int main()
{
scanf("%lld",&n);
for(INT i=1;i<=n;i++)
{
for(INT j=1;j<=n;j++)
{
scanf("%lld",&base[i][j]);
if(base[i][j]==2)lit[i]=1;
}
}
cout<<endl;
for(INT i=n;i>=1;i--)
{
if(!lit[i])make(i);
if(error){cout<<"does not exist"<<endl;return 0;}
}
for(INT i=1;i<=n;i++)
for(INT j=1;j<=n;j++)
if(base[i][j]==0)base[i][j]=2;
cout<<endl;
for(INT i=1;i<=n;i++)
{
for(INT j=1;j<=n;j++)
printf("%llx ", (unsigned long long)(base[i][j] & 0xFFFFFFFFFFFFFFFF));
cout<<endl;
}
return 0;
}
The above code is written to solve the n-queens problem, which is the problem presented to solve the P versus NP problem.
Currently, the above code only works if the multiplication of the number of zeros present in each column is 128 or less.
I wonder if I can solve the n-queens problem by making several modifications to this algorithm.

iterating through vector array error bs lc first question

class Solution {
public:
int search(vector<int>& nums, int target) {
int count=0;
for(unsigned long i=0; i<nums.size(); ++i){
if(nums[i]==target)
{ return nums[i]; ++count; break;}
}
if(count==0) return -1;
}
};

distance between any two nodes in a graph(based on edges between them)

The below code in c++ is to find the distance between any two nodes in a graph but there is some logical error where I tried to find but can't. Everything seems to be perfect but I can say that there is something wrong with a break inside if condition inside for loop which is inside while(s.!empty()).
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int find_distance(int link[100][100],int start,int dest,int n,int e)
{
start=start-1;
dest=dest-1;
if(start==dest)return 0;
int visited[100];
int distance;
for(int i=0;i<n;i++)
{
visited[i]=0;
}
stack<int>s;
int k;s.push(start);
visited[start]=1;
bool found=false;
int count =0;
while(!s.empty())
{
k=s.top();
int i;
int check_point=1;
for(i=0;i<n;i++)
{
if(link[k][i]==1 && visited[i]!=1)
{
s.push(i);
count ++;
check_point=0;
if(i==dest)
{
cout<<"found";
found=true;
}
break;
}
}
if(check_point==1)
{
s.pop();
count--;
}
if(found)break;
}
return count;
}
int main()
{
int n,e,a,b;
int link[100][100];
cout<<"enter the number of vertices and edges";
// cin>>n>>e;
n=5;e=4;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
link[i][j]=0;
else
link[i][j]=INT_MAX;
cout<<link[i][j]<<" ";
}
cout<<endl;
}
int input[4][2]={{1,2},{1,3},{2,4},{2,5}};
for(int i=0;i<e;i++)
{
a=input[i][0];
b=input[i][1];
link[a-1][b-1]=1;
link[b-1][a-1]=1;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<link[i][j]<<" ";
}
cout<<endl;
}
while(true) {
cout<<endl<<"enter the starting point .. ";
int start;
cin>>start;
int dest;
cout<<endl<<"enter the destination point .. ";
cin>>dest;
int distance = find_distance(link,start,dest,n,e);
cout<<endl<<"distance is "<<distance;
}
return 0;
}

How can I print order of odd numbers first and then even instead of smaller numbers using heapsort?

Suppose there is an array consisting of numbers 1,2,4,3,5,6,7
.I want to print 1,3,5,7,2,4,6 using heapsort.
I've been trying to modify basic heapsort but not been able to have correct output.
Can you please help?
#include<bits/stdc++.h>
using namespace std;
int heapsize;
int make_left(int i)
{
return 2*i;
}
int make_right(int i)
{
return (2*i)+1;
}
void max_heapify(int a[],int i)
{
// cout<<heapsize<<endl;
int largest=i;
// printf("current position of largest is %d and largest is %d\n",largest,a[largest]);
int l=make_left(i);
int r=make_right(i);
// printf("current position of left is %d and left element is %d\n",l,a[l]);
// printf("current position of right is %d and right element is %d\n",r,a[r]);
if(a[l]>=a[largest] && l<=heapsize && a[l]%2!=0)
largest=l;
if(a[r]>a[largest] && r<=heapsize && a[l]%2!=0)
largest=r;
//printf("Finalcurrent position of largest is %d and largest is %d\n",largest,a[largest]);
if(largest!=i)
{
swap(a[i],a[largest]);
max_heapify(a,largest);
}
}
void buildmax(int a[],int n)
{
for (int i=n/2;i>=1;i--)
{
// printf("main theke call\n");
max_heapify(a,i);
}
}
void heapsort(int a[],int n)
{
buildmax(a,n);
// printf("After being buildmax\n");
// for (int i=1;i<=n;i++)
//{
//printf("i is %d\n",i);
// cout<<a[i]<<endl;
//}
for (int i=n;i>=2;i--)
{
// printf("1st element is %d and last elemenet is %d\n",a[1],a[heapsize]);
swap(a[1],a[heapsize]);
//printf("1st element is %d and last elemenet is %d\n",a[1],a[heapsize]);
heapsize--;
max_heapify(a,1);
}
}
int main()
{
int n;
cin>>n;
heapsize=n;
int a[n];
printf("The elements are\n");
for (int i=1;i<=n;i++)
{
cin>>a[i];
}
heapsort(a,n);
printf("After being sorted\n");
for (int i=1;i<=n;i++)
{
//printf("i is %d\n",i);
cout<<a[i]<<endl;
}
}
You can use the same heapsort algorithm as before, just replace the less than operator (or greater than if you are using that for comparison) everywhere with a new function:
bool LessThan(int a, int b)
{
if (a%2 == 1 && b%2 == 0)
return true;
if (a%2 == 0 && b%2 == 1)
return false;
return a < b;
}

Shoot balloons and Collect Maximum Points

There are 10 balloons and each balloon has some point written onto it. If a customer shoots a balloon, he will get points equal to points on left balloon multiplied by points on the right balloon. A Customer has to collect maximum points in order to win this game. What will be maximum points and in which order should he shoot balloons to get maximum points ?
Please note that if there is only one balloon then you return the points on that balloon.
I am trying to check all 10! permutations in order to find out maximum points. Is there any other way to solve this in efficient way ?
As i said in the comments a Dynamic programming solution with bitmasking is possible, what we can do is keep a bitmask where a 1 at a bit indexed at i means that the ith baloon has been shot, and a 0 tells that it has not been shot.
So a Dynamic Programming state of only mask is required, where at each state we can transition to the next state by iterating over all the ballons that have not been shot and try to shoot them to find the maximum.
The time complexity of such a solution would be : O((2^n) * n * n) and the space complexity would be O(2^n).
Code in c++, it is not debugged you may need to debug it :
int n = 10, val[10], dp[1024]; //set all the values of dp table to -1 initially
int solve(int mask){
if(__builtin_popcount(mask) == n){
return 0;
}
if(dp[mask] != -1) return dp[mask];
int prev = 1, ans = 0;
for(int i = 0;i < n;i++){
if(((mask >> i) & 1) == 0){ //bit is not set
//try to shoot current baloon
int newMask = mask | (1 << i);
int fwd = 1;
for(int j = i+1;j < n;j++){
if(((mask >> j) & 1) == 0){
fwd = val[j];
break;
}
}
ans = max(ans, solve(newMask) + (prev * fwd));
prev = val[i];
}
}
return dp[mask] = ans;
}
#include<iostream>
using namespace std;
int findleft(int arr[],int n,int j ,bool isBurst[],bool &found)
{
if(j<=0)
{
found=false;
return 1;
}
for(int i=j-1;i>=0;i--)
{
if(!isBurst[i])
{
return arr[i];
}
}
found = false;
return 1;
}
int findright(int arr[],int n,int j,bool isBurst[],bool &found)
{
if(j>=n)
{
found = false;
return 1;
}
for(int i= j+1;i<=n;i++)
{
if(!isBurst[i])
{
return arr[i];
}
}
found=false;
return 1;
}
int calc(int arr[],int n,int j,bool isBurst[])
{
int points =0;
bool leftfound=true;
bool rightfound=true;
int left= findleft( arr, n-1, j,isBurst , leftfound);
int right = findright( arr,n-1, j,isBurst, rightfound);
if(!leftfound && !rightfound)
{
points+=arr[j];
}
else
{
points+=left*right*arr[j];
}
return points;
}
void maxpoints(int arr[],int n,int cp,int curr_ans,int &ans,int count,bool isBurst[])
{
if(count==n)
{
if(curr_ans>ans)
{
ans=curr_ans;
return;
}
}
for(int i=0;i<n;i++)
{
if(!isBurst[i])
{
isBurst[i]=true;
maxpoints(arr,n,i,curr_ans+calc(arr,n,i,isBurst),ans,count+1,isBurst);
isBurst[i]=false;
}
}
}
int main()
{
int n;
cin>>n;
int ans=0;
int arr[n];
bool isBurst[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
isBurst[i]=false;
}
maxpoints(arr,n,0,0,ans,0,isBurst);
cout<<ans;
return 0;
}

Resources