It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How we can to fill the chessboard with domino and we have a some blocks. and chessboard is n x m. and the places filled with ordered numbers.
Test :
Answer like this :
input give n , m and k. k is number of blocks.
and next k lines give blocks such as 6 7 or 4 9.
sorry for my English.
Here's an idea. In your example board, it is immediately obvious that squares 7 9 and 14 must contain domino 'ends', that is to say it must be the case that there are dominos covering 2-7, 8-9, and 14-15.
(In case it's not 'immediately obvious', the rule I used is that a square with 'walls' on three sides dictates the orientation of the domino covering that square)
If we place those three dominos, it may then be the case that there are more squares to which the same rule now applies (eg 20).
By iterating this process, we can certainly make progress towards our goal, or alternatively get to a place where we know it can't be achieved.
See how far that gets you.
edit also, note that in your example, the lower-left corner 2x2 (squares 11 12 16 17) is not uniquely determined - a 90 degree rotation of the depicted arrangement would also work - so you will have to consider such situations. If you are looking for any solution, you must come up with a way of arbitrarily picking one of many possibilities; if you are trying to enumerate all possibilities, you will have to come up with a way of finding them all!
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have come across a problem about the determination of triangle, it says:
Given a sorted integer array(length n), determinate whether you could
build a triangle by choosing three integers from the array, the
answer is "yes" or "no".
A naive solution is by scanning all the possibilities but it turn out to be O(n^3), seems
it will be C(n, 3) possibilities.
Assuming that the integers represent side lengths and array(0) > 0,
bool IsTriangle(int[] aray, int start) {
if(array.length - start <= 2) return false;
return (array(start+2) < array(start+1) + array(start+0))
|| IsTriangle(array,start+1);
}
This works because the list of integers is sorted; thus the RHS will always be larger using any subsequent elements of the array, and the LHS will be smaller using any previous elements of the array, and thus can satisfy the triangle inequality only if the selected three consecutive elements satisfy it. This is of course O(n) and can easily be converted to a (less elegant but more performant) iterative solution.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
What is an efficient algorithm in C or python that, given a list of unordered pairs signifying a relationship between two numbers, will combine the pairs so that they form a continuous list.
For example, given the following:
(0, 1)
(1, 2)
(3, 2)
(4, 3)
produce the following: [0, 1, 2, 3, 4]
Obviously, this would not work where there are gaps or loops in the pairs, e.g., (0, 1) (3, 4), and in those cases, throw an error message or something similar.
The numbers can be modeled as a vertex in a graph. Each element in the list will then represent an edge between the nodes.
What you are looking for is then the 'Eulerian Path' of this graph. There are multiple algorithms for this. Check out the well known algorithms at: http://en.wikipedia.org/wiki/Eulerian_path
What you are trying to do is not an Eulerian path (visiting each edge once) but a Hamiltonian path (visiting each vertex once), because you do not want to have loops in your path which by the definition of a loop means you don't want to visit any vertex two times.
This problem is known to be NP-complete which means that there is no known efficient algorithms (if by "efficient" you mean in polynomial time).
If you can find an efficient algorithm for this problem, you would also solve the P vs NP problem by showing that P and NP are equal. Most computer scientists believe that the answer to the P vs NP problem is "No, efficient algorithms for NP problems are impossible", which means that there is no possibility of an efficient algorithm to your question (although it remains to be proved).
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I was just reading about it on a book and wikipedia but still dont understand it 100%.
I would really appreciate it if someone could explain it with an example or two.
Thanks
Say I'm looking at a map, searching for a pizza place near my block in the city. A few different strategies I could use:
Breadth first search (BFS): Look at concentric circles of blocks around my block, farther and farther outward until I find a pizza place. This will give me one of the pizza places which is closest to my block as the crow flies.
Depth first search (DFS): Follow a road until I hit a dead end, then backtrack. Eventually all possible branches will be searched, so if there's a pizza place out there somewhere then I'll find it, but it probably won't be very close to my block.
Uniform cost search (UCS): Say traffic is bad on some streets, and I'm really familiar with the city. For any given location I can say how long it will take me to get there from my block. So, looking at the map, first I search all blocks that will take me 1 minute or less to get to. If I still haven't found a pizza place, I search all blocks that will take me between 1 and 2 minutes to get to. I repeat this process until I've found a pizza place. This will give me one of the pizza places which is the closest drive from my block. Just as BFS looks like concentric circles, UFS will looks like a contour map.
Typically you will implement UCS using a priority queue to search nodes in order of least cost.
I assume you were looking at this Wikipedia page. What it means is that the time required for a given operation (adding two numbers, comparing two numbers, retrieving data from memory, etc.) is independent of the size of the variables involved. In other words, an 8-bit comparison takes the same amount of time as a 32-bit comparison. Making this assumption allows you to simplify an efficiency analysis and compare algorithms without getting bogged down in implementation details.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I need a fast algorithm for checking if two non-infinite lines are crossing. Have to be fast because it'll run on a cell phone a lot.
The algorithm do only have to return yes or no, it does not have to find out exactly where the lines cross!
I have looked here: How do you detect where two line segments intersect?
But that thread is a jungle, people keep saying that "this is the answer" but then two other guys say that it is incorrect because of this-and-that bug.
Please help me find a good and working algorithm for this.
Just to be clear: I need a function that you give...
lineApointAx
lineApointAy
lineApointBx
lineApointBy
lineBpointAx
lineBpointAy
lineBpointBx
lineBpointBy
...and that returns true or false depending on if the two lines cross or not.
I would appreciate if you answered with (pseudo-)code, not formulas.
It is necessary and sufficient that the two ends of one segment are on different sides of the other segment, and vise-versa. To determine which side a point is on, just take a cross-product and see whether it's positive or negative:
(Bx - Ax)(Py - By) - (By - Ay)(Px - Bx)
EDIT:
To spell it out: suppose you're looking at two line segments, [AB] and [CD]. The segments intersect if and only if ((A and B are of different sides of [CD]) and (C and D are on different sides of [AB])).
To see whether two points, P and Q, are on different sides of a line segment [EF], compute two cross products, one for P and one for Q:
(Fx - Ex)(Py - Fy) - (Fy - Ey)(Px - Fx)
(Fx - Ex)(Qy - Fy) - (Fy - Ey)(Qx - Fx)
If the results have the same sign (both positive or both negative) then forget it, the points are on the same side, the segments do not intersect. If one is positive and the other negative, then the points are on opposite sides.
If you're two given points are (X1,Y1) and (X2,Y2), imagine both are infinite lines, not just segments:
Determine the formula for both (see: http://en.wikipedia.org/wiki/Linear_equation#Two-point_form)
Determine the intersection point for the two lines (see: http://zonalandeducation.com/mmts/intersections/intersectionOfTwoLines1/intersectionOfTwoLines1.html)
If X1 < intersectionX < X2 and Y1 < intersectionY < Y2, then yes, the segments intersect.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Say you wanted to find which input causes function x to output value y, and you know the (finite) range of possible inputs.
The input and output are both numbers, and positively correlated.
What would be the best way to optimize that?
I'm currently just looping through all of the possible inputs.
Thanks.
One solution would be a binary search over the possible inputs.
Flow:
find the median input x
get the output from function(x)
if the output is less than the desired y
start over using the smaller half of the possible inputs
else
start over using the larger half of the possible inputs
A binary search algorithm, perhaps?
http://en.wikipedia.org/wiki/Binary_search_algorithm
If the range is finite and small, a precomputed lookup table might be the fastest way
if you have some sets of know "x" data that yield "y" you can divied between training and test sets and use neural networks.