Count submatrices with all ones - c++

I am given a N*M Grid of 0s ans 1s.I need to find number of those submatrices of size A*B which have all 1s inside them.
Like suppose i have a grid of 2*6
The Grid is :
0 1 1 1 1 0
0 1 1 1 1 1
Now if say i want to find submatrices of size 2*3
Then here answer is 2.

EDIT: The following hints assume that by "submatrix" you meant "the intersection of a contiguous subset of rows and a contiguous subset of columns". (Usually a submatrix is allowed to skip rows and columns.)
I believe this is a homework question, so I'll just provide a hint instead of a full answer.
Suppose there was a way to efficiently calculate, for each cell (i, j), whether it was the rightmost cell of a run of at least m 1s in a row. How would that help?
Another hint: any given cell (i, j) is either the bottom-rightmost corner of some N*M grid of 1s, or it isn't.

Related

Permutations of NxN matrix with equal summation of any row elements or column elements (N being odd number)

The matrix NxN has N rows and columns. It has all unique elements starting from 1 to (N^2). The condition is the summation of any row elements should be equal to summation of any other row or column elements.
Example: For 3x3 matrix, one of the possible combination looks like following.
4 8 3
2 6 7
9 1 5
Now the question is how many possible combinations can occur to satisfy the given condition of given NxN matrix where N is any odd number?
Thanks for the help in advance.
Patrick
The best available answer is, "A heck of a lot."
If you add the condition "same sum down the diagonal", these are magic squares. As http://oeis.org/A006052 notes, the count of magic squares is known for n = 1, 2, 3, 4, and 5. The exact answer for 6 is not known, but it is in the order of 10**20.
Your counts will be higher still because you lack the diagonal condition. But the computational complexities are the same. Brute force will give you answers for n = 1, 2, 3, and 4 fairly easily. 5 will be doable. 6 will be intractable. 7 will be "no hope".

C++ Search a 2D vector for elements surrounding a chosen element?

Stuck here in my assignment. I am working with 2D Vectors. What my professor wants us to do is write a program that has the user enter a size of a matrix (N X N) and print the matrix with random 1's and 0's, which I have done.
What I am stuck is that he wants to find "nonzero" elements around a certain element. For instance:
0 0 0
0 1 1
1 1 1
Now the user is asked to type in a row and column to (to locate an element) then search for nonzero values adjacent to that element. So if rows and columns start at 0, row 1 and column 1 holds the value "1" (the center of the matrix) and has 4 adjacent nonzero elements. I am not quite sure where to go from here. Would I use the find code? I am not sure how to limit that to the adjacent locations of one element.
Thank you
Hint: if you want to look at the adjacent elements, you can just shift each index by one position. For example, if the given (row, column) is (1, 1), the adjacent positions are (0, 1), (2, 1), (1, 0), (1, 2). You should make sure your code only reads indices in the range (0..N, 0..N).
This is your assignment and you should do your best to finish it. Go for it and make us proud!

How to find the largest sum with the smallest possible path in a tetrahedron of integers?

First here is the question,
Say that an integer can be represented as a perfect sphere, in which the value of the sphere is equal to the integer it contains. The spheres are organized into a tetrahedral pyramid in which N = the length of the side, N being between 1 and 15. Pick (a possibly empty) subset of sphere's such that their sum of values is maximized. Note that the sphere can hold a negative value so it is not necessarily desirable to pick up every sphere. We do not want to disorganize the pyramid so we cannot take any one sphere without first taking the ones above it.
Input Format:
Line 1: integer N
Line 2: N(N+1)/2+1
Output Format:
Line 1: One integer, the maximum sum of values achievable
Sample Input:
3
5
-2 -7
-3
1 0 8
0 3
2
Sample Output:
8
Here is a sample solution given to my understanding so far:
The best solution is shown as bold in the diagram bellow. It is not smart to take 1 because that would require taking -2, decreasing the total. There for 8, 3, and 2 should be taken because they outweigh -3 and -7.
My question is,
How do I store the input so that I can retain the proper order? Or do i even need to? I am trying to use a queue but my program gets very lengthly because I have to find the sum for each possible path and then compare each sum to find the max. I am also having a lot of difficulty breaking the data up into the right pattern so I don't recount a number or take one out of sequence. Is there a more efficient way to do this? Can Dijkstra's algorithm be of any use in this case? If so, then how? Any help is greatly appreciated!!
I would use a 3-dimensional array. To use your example:
A[0][0][0] = 5
A[1][0][0] = -2
A[1][1][0] = -3
A[1][0][1] = -7
A[2][0][0] = 1
A[2][1][0] = 0
A[2][2][0] = 2
A[2][0][0] = 0
A[2][1][0] = 3
A[2][0][0] = 8
The "above" relationship is simply a matter of index arithmetic: [ia, ja, ka] is above [ia+1, ja, ka], [ia+1, ja+1, ka] and [ia+1, ja, ka+1].

Issue when generate random vectors with limits on matlab

I have a problem, I want to generate a table of 4 columns and 1 line, and with integers in the range 0 to 9, without repeating and are random each time it is run.
arrives to this, but I have a problem I always generates a 0 in the first element. And i dont know how to put a limit of 0-9
anyone who can help me?
Code of Function:
function [ n ] = generar( )
n = [-1 -1 -1 -1];
for i = 1:4
r=abs(i);
dig=floor((r-floor(r))*randn);
while find (n == dig)
r=r+1;
dig=dig+floor(r-randn);
end
n(i)=dig;
end
end
And the results:
generar()
ans =
0 3 9 6
generar()
ans =
0 2 4 8
I dont know if this post is a duplicate, but i need help with my specific problem.
So assuming you want matlab, because the code you supplied is matlab, you can simply do this:
randperm(10, 4) - 1
This will give you 4 unique random numbers from 0-9.
Another way of getting there is randsample(n, k) where n is an integer, then a random sample of size k will be drawn from the population 1:n (as a column vector). So for your case, you would get the result by:
randsample(10, 4)' - 1
It draws 4 random numbers from the population without replacement and all with same weights. This might be slower than randperm(10, 4) - 1 as its real strength comes with the ability to pass over population vectors for more sophisticated examples.
Alternatively one can call it with randsample(pop, k) where pop is the population-vector of which you want to draw a random sample of size k. So for your case, one would do:
randsample(0:9, 4)
The result will have the same singleton dimension as the population-vector, which in this case is a row vector.
Just to offer another solution and get you in touch with randsample().

set intersection

I want to calculate the gcd of two numbers m and n by prime factorization and taking common factors
like this. Take example m = 36 n = 48
vector<int> factors1 = prime_factorization(m); // 2 2 3 3
vector<int> factors2 = prime_factorization(n); // 2 2 2 2 3
vector<int> intersection(10);
set_intersection(factors1.begin(), factors1.end(), factors2.begin(), factors2.end(), intersection.begin());
intersection is now 2 2 3 0 0 0 0 0 0 0. For this i must set the size of the vector beforehand. Also the remaining elements are set to 0. I don't want this to happen.
Is there a better way to do this? Using sets or anything else?
Also, how do i calculate the product of elements in the vector intersection (2*2*3) using stl ignoring the zeroes?
You can use a back-inserter:
vector<int> intersection;
set_intersection(..., back_inserter(intersection));
Note that there are much better ways of determining the GCD, such as Euclid's algorithm.
Oli's answer is best in the situation as you describe it. But if you were using a vector that already existed and had elements that you were writing over, and you wanted to chop off the extra numbers, you can do it a different way. By calling the vector member erase using the return value of set_intersection:
intersection.erase(
set_intersection(factors1.begin(), factors1.end(), factors2.begin(), factors2.end(), intersection.begin()),
intersection.end());