Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
There's a matrix of NxM, with each element as 0 or 1. We select a specific r, c (such that 1<=r<=N and 1<=c<=M) and starting from (0,0) to (r-1,c-1), we toggle the values. That is, 0 becomes 1 and 1 becomes 0.
Basically, each "move" is to toggle all the values in an arbitrary top-left sub-matrix of our original matrix.
I need to write a function to calculate the minimum number of moves such that all elements of the matrix end up at 1, but not getting how to do it. Please help.
For example, in the following matrix (N == 2 and M == 4):
0 1 0 0
1 0 0 1
after doing the move (2, 1) we'll end up with this matrix: (note the toggled values in the first column and unchanged values in the rest of the matrix.)
1 1 0 0
0 0 0 1
Following should give you an idea.
If you start from the right bottom corner and traverse the matrix backwards, you can get the number of 'moves'.
Lets call move(r,c) as pressing the button at r,c.
So for example, if the N-1,M-1 entry is a zero, then you will have to press a button at N-1,M-1. Subsequently all entries before it shall get toggled.
Now you check this for each entry in the last row backwards. Subsequently check this for each entry in the last column backwards.
Instead of actually toggling all the entries, you can keep a count for the number of times a 'column' is toggled while traversing a row and the number of times a row is toggled while traversing a column.
Now decrease N by 1 and M by 1 and repeat. The present value of each entry shall be:
Original value ^ number of times its column has been toggled ^ number of times its row has been toggled & 1.
Related
Is it possible to show only values > 0 in a chart? On the X - axis I have several fields with a 0 and that's why I want to hide them. So can I set anywhere a minimum value?
Try using the "Y" axis instead of "X" - It has the effect you may be looking for.
If you want to start at your highest value, enter a 1 for 100% in the Y axis start box and leave the end at auto, and the graph will automatically size from your lowest value greater than zero
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
We have been trying to find a way to enumerate and index all possible trees with n labelled vertices. By Cayley's theorem there shall be nn−2 number of trees from n labelled vertices. Is there a way, in C/C++, to index all the possible trees so that when a user inputs an integer/number a unique tree will generated in real time?
A quick glance at the Wikipedia article on Cayley's formula (the nn−2 formula you mention) pointed me to Prüfer sequences, which is a sequence of length n−2 consisting of (possibly repeated) node labels. It's obvious that there are nn−2 such sequences, and each sequence can be represented as an n−2 digit base n number. It's less obvious that every Prüfer sequence corresponds to a unique tree with n labeled nodes, but that fact is sufficient to demonstrate Cayley's formula.
The Wikipedia article on Prüfer sequences explains how to turn a sequence into its corresponding tree; which is equivalent to turning an integer into a tree.
I haven't tried any of this, but it looks convincing.
I'm not well-versed in C or C++, but I think I can provide the theory such that enumerating every tree shouldn't be too hard. Comment if I need to clarify anything.
Think Binary.
Take an adjacency matrix. To describe whether one vertex is connected to another, we use 1 or 0. So to find all the graphs using adjacency matrices, we would fill up a matrix with all the combos of 1 and 0. The only constraint is that for trees, a node can't be its own parent, and can't have multiple parents. Example with three vertices:
0 1 1 0 1 1 0 0 1
1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 1 0 etc.
What we can do is to lay the rows out side-by-side such that a binary sequence describes every matrix. Example:
0 1 1 1 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 1 1 0 0 0 1 0 etc.
So given nine bits, we can describe all graphs with three vertices. This translates to one tree for every number 1-2^9, minus the numbers which are rotations of each other.
To turn a number into a tree, you just convert the number to binary, and turn the binary into a matrix. To fix the self-connections, for every "1" that is on or past the diagonal, move it further by one. So then:
1 0 0 1 0 1
0 1 0 -> 0 0 0
0 0 1 0 1 0
in this 2d array 1 represents a point and 0 represents blank area.
for example this array:
1 0 0 0 1
0 0 1 0 0
0 0 0 0 0
0 0 0 0 1
my answer should be 2, because there are 2 squares (or rectangles) in this array like this
all the points should be used, and you can't make another square | rectangle if all its points are already used (like we can't make another square from the point in the middle to the point in the top right) because they are both already used in other squares, you can use any point multiple times just if at least one corner is not used point.
I could solve it as an implementation problem, but I am not understanding how backtracking is related to this problem.
thanks in advance.
Backtracking, lets take a look at another possible answer to your problem, you listed:
{0,0} to (2,1}
{0,0} to {4,0}
As one solution another solution is (With respect to the point can be used multiple times as long as one point is unused):
{4,0} to {2,1} (first time 4,0 and 2,1 is used)
{0,0} to {2,1} (first time 0,0 is used)
{0,0} to {4,4} (first time 4,4 is used)
Which is 3 moves, with backtracking it is designed to show you alternative results using recursion. In this equation if you start the starting location for calculating the squares at different areas of the array you can achieve different results.
for an example iterating starts from 0,0, and going right across each row trying to find all possible rectangles starting with [0,0] will give the solution you provided, iteratings starting from 4,0 and going left across each row trying to find all possible solutions will give my result.
I've got a 2D-binary matrix of arbitrary size. I want to find a set of rectangles in this matrix, showing a maximum area. The constraints are:
Rectangles may only cover "0"-fields in the matrix and no "1"-fields.
Each rectangle has to have a given distance from the next rectangle.
So let me illustrate this a bit further by this matrix:
1 0 0 1
0 0 0 0
0 0 1 0
0 0 0 0
0 1 0 0
Let the minimal distance between two rectangles be 1. Consequently, the optimal solution would be by choosing the rectangles with corners (1,0)-(3,1) and (1,3)-(4,3). These rectangles are min. 1 field apart from each other and they do not lie on "1"-fields. Additionally, this solution got the maximum area (6+4=10).
If the minimal distance would be 2, the optimum would be (1,0)-(4,0) and (1,3)-(4,3) with area 4+4=8.
Till now, I achieved to find out rectangles analogous to this post:
Find largest rectangle containing only zeros in an N×N binary matrix
I saved all these rectangles in a list:
list<rectangle> rectangles;
with
struct rectangle {
int i,j; // bottom left corner of rectangle
int width,length; // width=size in neg. i direction, length=size in pos. j direction
};
Till now, I only thought about brute-force-methods but of course, I am not happy with this.
I hope you can give me some hints and tips of how to find the corresponding rectangles in my list and I hope my problem is clear to you.
The following counterexample shows that even a brute-force checking of all combinations of maximal-area rectangles can fail to find the optimum:
110
000
110
In the above example, there are 2 maximal-area rectangles, each of area 3, one vertical and one horizontal. You can't pick both, so if you are restricted to choosing a subset of these rectangles, the best you can do is to pick (either) one for a total area of 3. But if you instead picked the vertical area-3 rectangle, and then also took the non-maximal 1x2 rectangle consisting of just the leftmost two 0s, you could get a better total area of 5. (That's for a minimum separation distance of 0; if the minimum separation distance is 1, as in your own example, then you could instead pick just the leftmost 0 as a 1x1 rectangle for a total area of 4, which is still better than 3.)
For the special case when the separation distance is 0, there's a trivial algorithm: you can simply put a 1x1 rectangle on every single 0 in the matrix. When the separation distance is strictly greater than 0, I don't yet see a fast algorithm, though I'm less sure that the problem is NP-hard now than I was a few minutes ago...
One can enter the matrix from any cell, and each cell contains some clue regarding the position of the cell needed to be found like
Total 8 types of clues can be present:
Exactly Above it
Exactly Below it
Exactly To the Right
Exactly To the Left
In the Top Right Region
In the Bottom Right Region
In the Top Left Region
In the Bottom Left Region
In each step he can move to one adjacent cell as per the above eight movements.
My question is minimum no. of steps he needs to take to reach that cell in the worst case.
Test Case :
2 2 : 1
3 3 : 1
My solution looked like this :
I thought If I start from the middle region of the matrix , then I would have to take the minimum no. of steps.
cin >> n >> m ;
ans = max ( n/2 , m/2 ) ;
But obviously it was wrong.
The correct solution looked like this
cin >> n >> m ;
ans = log( max(n,m) , 2) ; //base 2
Can anyone explain this ?
The question can be found here : http://www.codechef.com/TRNT2014/problems/TR001
These two items:
In each step he can move to one adjacent cell as per the above eight movements.
The correct solution looked like this:
ans = log( max(n,m) , 2) ; //base 2
are in contradiction. If the first is true, then your solution is correct. If the second is true, then the first must be false, and he can jump to any cell (or at least a cell (n/4,m/4) away).
Contrary to the description above, the linked description does not seem to limit which box you can check at any time.
As such, the correct approach seems to be basically a 2D binary search. Start at the middle box. If (for example) the clue says the correct box is up and to the right, move half the distance toward that corner. At that point, it might say (for example) that the correct box is up and to the left. If so, you move half the distance that direction.
In short, it's a binary search, except that the result at each point is a vector--a direction and a distance--rather than just a distance as you'd have in a typical 1D binary search.