This question already has an answer here:
Algorithm for toggling values in a matrix [closed]
(1 answer)
Closed 9 years ago.
Flip the world is a game. In this game a matrix of size N*M is given, which consists of numbers. Each number can be 1 or 0 only. The rows are numbered from 1 to N, and the columns are numbered from 1 to M.
Following steps can be called as a single move.
Select two integers x,y (1<=x<=N and 1<=y<=M) i.e. one square on the matrix.
All the integers in the rectangle denoted by (1,1) and (x,y) i.e. rectangle having top-left and bottom-right points as (1,1) and (x,y) are toggled(1 is made 0 and 0 is made 1).
For example, in this matrix (N=4 and M=3)
101
110
101
000
if we choose x=3 and y=2, the new state of matrix would be
011
000
011
000
For a given state of matrix, aim of the game is to reduce the matrix to a state where all numbers are 1. What is minimum number of moves required.
How to solve this problem?
This is not a homework problem.I'm pretty confused with it.I'm fighting with this problem for past two days.And maintaining a 2-D array for number of ones and zeros.
I tried like balancing the number of one's and number of zeroes.But didn't work out.Any hints or solutions. ?
Source: Hackerearth
Hint #1: Use a greedy approach from bottom to top. That is: if the cell (n, m) is 0 then you must apply XOR to the rectangle (0,0)-(n,m). So try traversing all cells from bottom to top and from right to left and if the current cell is zero then perform a move on it.
This yields a O(n^4) solution.
To get a n^2 solution use, for example, accumulated sums in every rectangle.
Related
I have been trying to answer this question for months now but I am still stuck.
The question requires me to write a program to output YES or NO to whether the given set has a line that can divide it.
I am looking for a possible algorithm to determine the answer, I want to interpret it into code once I have a firm grasp on the answer.
Given an even length set of Circles on a 2D plane that are guaranteed not to touch. determine if it is possible to draw a line through the set dividing it exactly in two without intersecting any circle.
Circle Radius is greater than zero
No Circle touches or contains one another
A set of length 2 is always possible
every Circle can be unique in size
Input Format:
N - number of circles in set
x y r - N lines of: x coordinate, y coordinate, radius
Input repeats until EOF
Output YES or NO for each test case
Example input:
4
0 0 20
0 40 20
0 30 10
40 -30 10
4
0 0 20
0 40 20
20 40 20
20 -40 20
Output:
YES
NO
Edit: My attempts to solve
First attempt was to find all lines that could solve this problem if every circle were zero radius points to give me a set of possible solutions to the problem.
Link to a Dividing a plane of points into two equal halves
Afterwards I would return the radii back then iterate through each possible solution.
This algorithm was extremely slow (I did'nt bother to calculate the O time since the required algorithm was required to run in a reasonable time frame of a second)
My Second attempt was to project these circles onto the y and x axis and rotate the set until there existed a section of the x or y axis without a "shadow" of any circle while splitting the sets into two.
This method would only require a maximum rotation of 1/2pi radians to determine the answer but attempts to program were complex and slow.
I cannot find the question anywhere online as it was presented on paper last year created by a professor at my university.
Simple algorithm with cubic complexity:
Find common tangents for all circle pairs. There are 4*n*(n-1)/2 ~ n^2 tangents.
For every tangent check whether it intersects all circles. n*n^2=n^3 operations
I think that algorithms with better complexity might exist (based on tangent direction sorting)
Imagine n axis-aligned rectangles (specified by its position (x,y), width and height). The rectangles are aligned in a way so that the i-th rectangle necessarily intersects with the (i+1)-th. For example let n = 3, then 1 necessarily intersects with 2 and 2 with 3. It is important to mention that this is not transitive; 3 can intersect with 1 but there is no guarantee (see figure for two valid alignment examples).
What I'm now looking for is the maximum possible number of regions where exactly k = 2,...,n rectangles intersect with each other (these regions are shown in the figure). In other words, I'm looking for a worst-case alignment of n rectangles so that the number of regions where exactly k rectangles intersect reaches its maximum. Theoretical, the maximum possible number of regions where exaclty k rectangles intersect is n over k (the binomial coefficient). However, this formula is geometrical only valid for n < 4 as it is not possible to align (and to draw) rectangles for n >= 4 so that in the worst-case n over k regions exist where exactly k rectangles intersect.
The first sub-image of the figure shows the worst-case alignment for n = 3. There are 3 over 2 = 3 regions where exactly two rectangles intersect and 3 over 3 = 1 region where exactly three rectangles intersect. The second sub-image also shows a valid alignment for three rectangles however this is not a worst-case alignment as, for example, there is no region where exactly three rectangles intersect.
A WRONG answer; not removed only because of the approach that may or may not be useful.
The geometric data -- which rectangles intersect -- can be abstracted away: all that matters is the following property:
Property P: If rectangles i and j intersect that implies that i intersects with i+1,...,j-1 as well.
If your representation of the problem encodes P it doesn't matter anymore that you started with the rectangles.
Now, how do we keep record which rectangles intersect? One way would be a graph with nodes being the rectangles and edges intersections, but that isn't very useful because the above property P is not evident in a graph. A better way would be to setup the following matrix:
Represent i-th rectangle with the i-th row of a matrix A that has 0s until the entry A(i,i), 1s from A(i,i) to A(i,i+m), where i+m is the index of the furthest rectangle that intersects with rectangle i. That is, A has n rows, one per the original rectangle, it consists of 0s and 1s, and A(i,j) for j>i is 1 if and only if rectangles i and j intersect. For j
Now, what does it mean that we have an area of exactly k intersecting rectangle? I claim that the above matrix represents that by a column that has exactly k 1s. Why? Suppose that your area is an intersection of rectangles i+1,...,i+k. Take a look at the matrix entry A(i+k,i+k). The column above it has 1s in rows from 1+1 to i+k and 0s otherwise.
The above matrix looks superficially similar to young's skew-tableau, thus the comment. But yes, similarity is superficial because it doesn't originate from a partition.
Now it remains to maximize the number of columns in A that has exactly k 1s. I think the best one would be a matrix with exactly k 1s in each row, which would give the answer to the original problem n. The answer is obviously wrong, so I'm missing something here. Aaaaah!
Build a matrix where the cell Aij is 1 if rectangles i and j intersect or 0 if they don't. This matrix is symmetric.
Now notice that many 1's close to the diagonal mean more intersections between contiguous-aligned rectangles.
The "worst" case, where k, the number of rectangles that intersect each other, is maximum, is represented in the matrix by k contiguous 1's, with no 0 in between. You can consider the elements after the diagonal. This way the property "rectangle i intersects rectangle j" is fulfilled.
The problem is how to swap rows and columns to achieve this result. It may be a matrix bandwidth reduction problem.
See, for example, this and this.
You can also generate your own algorithm for your special case. Be aware of it can be a NP problem, and that several solutions may exist.
I am trying to solve this algorithmic problem. For convenience, I have replicated it below:
We are given a grid G of size N x M (1 <= N,M <= 1000) containing only 1s and 0s. If we choose one of the cells, this will toggle the value in adjacent cells (and that cell itself). Two cells that differ by exactly 1 row or 1 column are considered adjacent (i.e. diagonal cells are not adjacent). Our goal is to find a grid G' containing 1s at cell positions that we need to choose in order to turn all cells in G to 0 (those cells that we don't have to choose are marked with 0). Given any G (in this problem), G' is guaranteed to exist.
Note: There is no wraparound in the grid.
For example, if G is given as the following:
000
100
000
If we choose the center cell, G will become:
010
011
010
For this example, G' is:
001
011
001
It looks very similar to the lights-out puzzle. I am only able to solve this for small instances (N,M <= 20) using brute force. A search on google also yields a solution (for the lights-out puzzle) that uses Gaussian elimination. But this only works on small grids (N,M <= 100) since this method has a cubic time complexity.
Could someone please advise me on how I could solve this problem?
Gaussian elimination doesn't have cubic time complexity. It takes O(N^3) time for NxN matrices, but in terms of the input size, that's just O(N^1.5)
To solve the lights out puzzle on an NxM grid with Gaussian elimination takes O(N^2*M) time and O(N*M) space, where N can be whichever dimension is smaller.
On an average PC or MAC you should be able to do 1000x1000 in a small number of seconds in C++ or Java if you make sure the inner loops are fast.
I am attempting to solve the following question from SPOJ :
On a rectangular mesh comprising nm fields, nm cuboids were put, one
cuboid on each field. A base of each cuboid covers one field and its
surface equals to one square inch. Cuboids on adjacent fields adhere
one to another so close that there are no gaps between them. A heavy
rain pelted on a construction so that in some areas puddles of water
appeared.
Task
Write a program which:
reads from the standard input a size of the chessboard and heights of cuboids put on the fields
computes maximal water volume, which may gather in puddles after the rain
writes results in the standard output.
Input
The number of test cases t is in the first line of input, then t test
cases follow separated by an empty line. In the first line of each
test case two positive integers 1 <= n <= 100, 1 <= m <= 100 are
written. They are the size of the mesh. In each of the following n
lines there are m integers from the interval [1..10000]; i-th number
in j-th line denotes a height of a cuboid given in inches put on the
field in the i-th column and j-th raw of the chessboard.
Output
Your program should write for each tes case one integer equal to the
maximal volume of water (given in cubic inches), which may gather in
puddles on the construction.
Example
Sample input:
1
3 6
3 3 4 4 4 2
3 1 3 2 1 4
7 3 1 6 4 1
Sample output:
5
I am using a BFS to add how much water will flow from the border elements into the puddle(if theres any path found). But I am unable to handle cases where a puddle maybe like two consecutive cuboids. Can anyone help me with that case?
Here is my answer for the problem. For speaking convenience, I assume the index start from (1,1) to (M,N)
As you can imagine as the flow of water, the water can only travel from the higher cuboid to the lower cuboid ( there is no revert direction i.e. the water from lower cuboid will hit the wall of higher cuboid and stop there).
So we build the graph G = (V,E) such that V are M x N vertices i.e. the cuboid on the matrix. The edge are connected (1-way ONLY) that connected cuboid i(th) to cuboid j(th) when
height(i) >= height(j) and (i and j are physically CONNECTED)
So the problem are just a simple BFS.
By the way, I found another one solve this problem as well. Please take a look
http://www.spojsolutions.com/212-water-among-cubes/
Given an area of specific size I need to find out how many pavement stones to use to completely pave the area. Suppose that I have an empty floor of 100 metre squares and stones with 20x10 cm and 30x10 cm sizes. I must pave the area with minimum usage of stones of both sizes. Anyone knows of an algorithm that calculates this?
(Sorry if my English is bad)
C# is preferred.
This is a category of problem known as a Packing Problem.
This DevX article provides background and outlines approaches without directly solving the homework for you (it also provides code, but you'll have to think a bit to apply it to your situation).
You can solve this in your head (assuming the area to fill is rectangular). If you have to fill an area of N squares, and your tiles are 2x1 and 3x1, you never need more than two 2x1 tiles. This gives the total number of tiles you need to be N/3 (rounded up) except for the case N=1 which is impossible.
Proof:
Suppose your area is A x B and that not both of A and B are 1. Assume without loss of generality that A != 1.
You can tile a rectangular area A x 3 with 3x1 tiles easily. Repeat this pattern to fill up the are as much as possible.
If there are no rows left, you're done (you've tiled the entire area with 3x1 tiles)
If there's one row left, fill with 3x1 tiles until you have either 0, 1 or 2 spaces left. 0 => you're done, 1 => replace the last 3x1 with two 2x1, 2 => fill the last square with a 2x1.
If there's two rows left, do a similar construction. You'll be left with either 0 columns (you're done), 1 column (fill it with a 2x1), or 2 columns (fill it with two 2x1).