I have to make a flash game like this:
There is a board with holes in it (more than 1000). Initially, there are 3 pegs placed on the board and a rubber band around them.
We have 3 possible operations:
1. Add peg - adds a peg on the board
2. Remove peg - removes a peg ( if there are more than 3 pegs) - the rubber band must take the shape of the remaining pegs.
3. Move peg - rubber band must be updated with current positions of the pegs.
How would you solve the problem of finding the rubber band's shape optimally?
I have 2 ideeas, but I have to work on them a little bit. The main ideea is that we have to change the rubber band's shape only at "Move" operation, and we use the same number of pegs, only one is changing position:
A derivation from convex hull algorithm. We have to know wich pegs are inside the rubber band and wich are outside. It might get a little complicated.
We work with only 3 pegs: 2 anchors and 1 middle. The 2 anchors form a boundary line for the interaction of the 1 middle peg. On the active side of the line the rubber band functions as 2 segments between the 2 anchor pegs and the middle peg. On the inactive side the 1 middle peg is free to move while the rubber band functions as a straight line between the 2 anchor pegs. The caveat to the above is that there are cases in which movement of the 1 middle peg in the active side of the boundary line can can cause one of the 2 segments to contact a 4th peg. The program must detect this occurrence and update the new anchor pegs accordingly. These are just suggestions from some limited experience with this concept. The developer should determine the best approach based on his experience and judgement.
Do you have any other ideeas, or suggestions?
"The developer should determine the best approach based on his experience and judgement." — did you copy and paste this from the spec you were given? :)
You ask for an "optimal" solution but if I were you I'd aim for a "correct, and fast enough" solution. You've got a contract to fulfil, you can leave the asymptotics to the academics.
Anyway, your plan to update the band only when the player moves a peg looks like a good one. We are going to need to remember all the pegs that are touching the rubber band, and for each peg we have to remember which side of the rubber band it's on (in order to draw the band correctly).
Now, suppose the player moves peg A from a to a'.
As a general principle, it's worth bearing in mind that even if your time segments are short, and the distance from a to a' is small, nonetheless there might be multiple things that happen. So you're going to have to consider all the events that might happen in that time segment, pick the earliest such event, update your data structures accordingly, and then continue with the remainder of the time segment.
So what kind of events are there?
Peg A "picks up" the band. (It does so if peg A was not already on the band, and the line a–a' crosses one of the lines between pegs on the band.)
Peg A "puts down" the band. (It does so if peg A was on the band, with neighbours B and C, and the line a–a' crosses the line B–C.)
Peg A gains a neighbour on the band. (This happens when peg A is on the band, B is a neighbour of A and the triangle a–a'–B contains another peg C.)
Peg A loses a neighbour on the band. (This happens when peg A is on the band, the neighbouring pegs on the band go A–B–C, and peg B is in the triangle a–a'–C.)
So you should determine all such events; work out the time that each event would happen; sort the events into order by time; handle the earliest event (only); repeat for the remainder of the time segment.
(What to do if two events happen simultaneously? I'll leave that up to your experience and judgement.)
Edited to add: a complication is that a peg may appear on more than one segment of the rubber band (for example, the band may go A-B–A-C-A). But I think the above sketch of an algorithm still works.
A further wrinkle is that even with a small number of pegs, you can make arbitrarily twisty configurations of the band. For example, suppose the band is stretched between pegs B and C. Now take peg A and move it in a figure-of-8 around pegs B and C (clockwise around B, anti-clockwise around C, let's say). Each time round the loop, peg A picks up another couple of pieces of the band. You can't afford to let the complexity of the configuration grow without bound, so need some way of stopping things getting out of hand. I suggest imposing a maximum limit on the length of the band, so that any attempt to stretch it too far causes it to snap (of course you'd have warning signs before this happens, e.g. band getting thinner, changing colour, ominous creaky sounds).
Related
This is a homework question, but I don't really know how to go about tackling it and would appreciate any tips, as I haven't found any very similar problems online.
Basically, I have to come up with a program that takes in positions of a number N of pawns on a 8x8 chess board, and the position of a single knight, and then determine whether it's possible for the knight to capture every pawn (while moving in standard chess knight L-shaped fashion) and what is the minimum amount of moves to do so; moreover, every turn all the pawns move 1 square down the board simultaneously. If any of the pawns makes it to the last row, it is promoted into a queen and the knight loses immediately if it doesn't capture it as soon as it is promoted. The knight also loses if it ends a turn in a position where it is to be attacked by any pawn the next turn.
This seems similar to a knight's tour problem, but I'm not sure how to deal with the fact that the targets the knight has to go to are always moving and also making sure the knight doesn't get captured. I first tried solving it with backtracking which seems to be a common approach for this sort of chess problem, but I couldn't really come up with anything. Then what I thought of as a possible, if very inefficient, solution with dynamic programming was to solve the knight's tour problem for each possible starting square for the knight (maybe memoizing the results as a 64x64 array) and then whatever square the knight is on I would know the minimum amount of steps to get to any other square (where a pawn might be). With this I could check which, if any, pawns could be captured before the closest pawn to the bottom row is promoted to queen (by comparing the number of steps for the knight to reach the pawn to the number of steps for the pawn to reach the final row). However, this is not a perfect solution either, as memoizing the knight's tour in this manner would only save the minimum amount of steps taken and not the actual paths (which would presumably take up a lot of memory) and thus I wouldn't be able to check whether the knight is going to be captured by a pawn at some point during the path.
I would think that a breadth-first search would be fine.
The knight has at most eight possible moves at each position and there are at most seven turns before some pawn gets promoted. So that's at most about two million possibilities to search, but in practice there would be a lot fewer.
You only need about N+6 bits to describe a position (64 possible knight locations and a bit vector of which pawns are still alive). Since N cannot be more than eight (otherwise the knight can't capture them all before one gets promoted), you can keep track of all the seen positions and the immediate predecessor of each one with a vector of 16384 shorts.
I'm trying to solve a programming task I've been given and I don't have the slightest idea how to do it.
This is the problem:
Skinny Pete is invited to a garden birthday party. He doesn’t really
like parties too much, but heard that the birthday cake is going to be
really amazing and he wouldn’t like to miss the chance to try it.
There is only one little problem. There is a sprinkler system
installed in the garden and by knowing his friends, there is a high
chance of someone turning it on as a party prank. Pete likes cake, but
really hates getting wet. Luckily he found a sketch of the garden that
has the location of the sprinklers and how far each one can sprinkle
water.
The garden looks like a rectangle that is open on one side and has the house in the opposite side.
The cake is going to be in the house.
The other two sides have fences so one can not enter through there, and the house does not have a back entrance. Pete is interested to
know if it is possible to enter the garden and get to the house
without any risk of getting wet.
For simplicity we can think that the map of the garden is in Cartesian
coordinate system.
The garden is a rectangle that has sides parallel to the axes and having its bottom left corner at the origin (0, 0).
The entrance to the garden is the left side and the the house is at the right side.
Sprinklers are represented as circles with a center and a radius. Stepping anywhere inside such a circle might get you wet.
For the purpose of this problem, and since Pete is so skinny, we can think of him as just a point travelling in the space, with real
numbers as coordinates.
Input Specifications First line of the standard input contains two
space separated integers H and W, the height and the width of the
garden.
Next line contains the number of sprinklers N. After that N lines
follow having three space separated integers each - Xi, Yi and Ri.
This a description of a sprinkler as a circle with center (Xi, Yi) and
radius Ri.
1 ≤ N ≤ 128
1 ≤ H, W ≤ 1024
0 ≤ Xi ≤ W
0 ≤ Yi ≤ H
1 ≤ Ri ≤ 1024
Output Specifications
Output a single line containing “CAKE” (without quotes) if it is
possible to get to the house without getting wet and “NO CAKE”(without
quotes) otherwise.
thanks in advance to helpers
Since you show no code and you only implicitly ask for help, I'll give a key idea and leave the mathematics and the implementation to you.
Skinny Pete can get the cake without getting wet unless there is a chain of sprinkler circles between the bottom and the top of the garden. In other words, we can assume that Pete succeeds. But look through all the circles. We see if any circle intersects the bottom edge of the garden--that is easy mathematics. If there is none, Pete really succeeds. If there is, see if there is another circle that intersects that first one, then if there is another that intersects the second, etc. Finally, you see if the last circle in this chain intersects the top edge of the garden. If there is any such chain of intersecting circles that also intersects top and bottom of the garden, poor Pete goes hungry. (Note that just one circle that intersects both top and bottom would also frustrate Pete--consider that to be a chain of one.)
Here is a diagram of the second example in your contest PDF, where you can see there is no chain of spanning circles so Pete succeeds.
And here is a diagram of the third example, where Pete fails. Note that there is a chain of four circles on the left, colored blue, that spans the garden.
Given that idea, there is an obvious O(N^2) algorithm to find all pairs of intersecting circles and an O(N) algorithm to find the circles intersecting the top and bottom sides of the garden. You could use a path-finding algorithm from graph theory to solve your problem. Think of the top and bottom sides and your circles as nodes in the graph, with two nodes connected with an edge if they intersect. You then search for a path between the nodes representing the top and bottom sides.
Good luck on figuring the mathematics, algorithm, and code.
I am developing a software for a board game (2 player) which has 10x4 cells and both the players have 9 pieces each. Initially, the pieces for player 1 will be at the top of the board and for player 2 all the pieces will be at the bottom of the board (similar to chess but a lot less complex!).
I have used MiniMax algorithm to calculate the next best move. Now, the algorithm itself seems to work fine. The problem that I am facing is in Heuristic value calculation.
If there is no best move found in the depth till I am searching (4 currently), my code simply takes the first step which it finds from the move list. That is probably because the score of all the moves are same till the depth of 4!
So, it just keeps stalling. Eg., it will move piece 1 from position A to B in 1st turn and in the 2nd turn it will move the same piece from position B to A.
It keeps on doing that until the opponent moves closer to my pieces.
Now, what I want would love to know is how do I make sure that if the opponent is NOT closing in, I do that instead of stalling.
Currently, I am calculating the heuristic values based on the difference between my pieces and opp pieces.
How to calculate the values such that those moves which lead to position closer to the opponent's pieces are selected? Appreciate your help! Thanks!
I'm having a hard time finding an admissible heuristic for the Hungarian Rings puzzle. I'm planing on using IDA* algorithm to solve and am writing the program in Visual Basic. All I am lacking is how to implement the actual solving of the puzzle. I've implemented both the left and right rings into their own arrays and have functions that rotate each ring clockwise and counterclockwise. I'm not asking for code, just somewhere to get started is all.
Here is the 2 ring arrays:
Dim leftRing(19) As Integer
' leftRing(16) is bottom intersection and leftRing(19) is top intersection
Dim rightRing(19) As Integer
' rightRing(4) is top intersection and rightRing(19) is bottom intersection
In the arrays, I store the following as the values for each color:
Red value = 1 Yellow = 2 Blue = 3 and Black = 4
I suggest counting "errors" in each ring separately - how many balls need to be replaced to make the ring solved (1 9-color, 1 10-color, one lone ball from a 9-color). At most two balls can be fixed using a rotation, then another rotation is needed to fix another two. Compute the distance of each ring individually = 2n-1 where n is half the amount of bad positions and take the larger of them. You can iterate over all twenty positions when looking for one that has the least amount of errors, but I suppose there's a better way to compute this metric (apart from simple pruning).
Update:
The discussion with Gareth Reed points to the following heuristic:
For each ring separately, count:
the number of color changes. The target amount is three color changes per ring, and at most four color changes may be eliminated at a time. Credits go to Gareth for this metric.
the count of different colors, neglecting their position. There should be: 10 balls of one 10-color, 9 balls of one 9-color and one ball of the other 9-color. At most 2 colors can be changed at a time.
The second heuristic can be split into three parts:
there should be 10 10-balls and 10 9-balls. Balls over ten need to be replaced.
there should be only one color of 10-balls. Balls of the minor color need to be replaced.
there should be only one ball of a 9-color. Other balls of the color need to be replaced. If all are the same color, and 9-color is not deficient, one additional ball need to be replaced.
Take the larger of both estimates. Note that you will need to alternate the rings, so 2n-1 moves are actually needed for n replacements. If both estimates are equal, or the larger one is for the latest moved ring, add an additional one. One of the rings will not be improved by the first move.
Prune all moves that rotate the same ring twice (assuming a move metric that allows large rotations). These have already been explored.
This should avoid all large local minima.
I'm working on a snake game (Nibbles in Linux) that is played on a 60*60 field, with four snakes competing for an apple which is randomly placed.
I've implemented the movement of my snake with the A* (A star) Algorithm.
My problem is this: When I'm not the nearest snake to the apple, I don't want to go to get the apple, because my chance to get it is lower than at least one snake, so I want to look for a place that I hope at the next place that an apple is generated , Then I'll be the nearest snake to that apple. I mean that I'm looking for a place which is nearest to the maximum number of potential locations.
Please suggest any good way or any algorithm that can help me to find this place.
Here is an image of the game. The red points are the snakes' heads.
I tested some ways and Finally I decided to use this way:
I think the best way is to make a 2D array with size:60*60 , then for each node(x) of the array, calculate how many nodes of the field-which are walkable!(not block), is this node(x) nearest to.
then the answer will be The maximum amount, then I set this node the goal.
but because I must find the next move in less than 0.1sec and to do this work, there is 4 loops of size:60, (60^4) and when I found it, A* algorithm will be run too , this work would never be done in less than 0.1 sec.
So , since the Snake can't move Diagonally and it goes just: up,down,right,left, I decided not to check all the nodes,Since in each cycle(0.1sec) , I can just move 1 unit, I decided to check just 4 nodes(up,down,left,right) and move to a node which It's amount is Max.
now it's working almost right. ;)
Since you have already implemented A*, after you generate your map, you could use A* to create a map of values for each cell based on the total cost from each cell to visit every other cell. If you generate this after you've placed your blocks, then your weighted map will account for their presence.
To generate this, off the top of my head, I would say you could start from each cell, and assign it one point for each cell it can visit in one turn. For example, a cell in the corner would get two points for the first move, because it can only access two other cells. It would get five points for the second turn, because it can access five cells in two moves. Repeat until you've visited all the other squares, and then you have the score for that square.
From that point, if an apple appears and your snake is not the closest to it, your snake could head for the highest weighted cell, which you've calculated beforehand.
Edit: Please see comments below for a more advantageous solution.
If you are nearest to apple you should walk to get it but if you are far apart from apple your best chance is walking in a middle of map, you should find strategy to how to occupy the middle of map.
You can divide your map to four zooms (clockwise), upper left, upper right, bottom right and bottom left (1,2,3,4). We check this between two snakes: If apple currently is in zoom 1 (assume center for average) and you are in center of map, your opponent can be in zooms 1,2,3,4 (again assume it's in the center of this zooms to take average in simpler way) if it's in zoom 1 it has better chance (1-0) if it's in zoom 2 or 4, your distance is sqrt(2)/2 and your opponent distance is 1, so you are nearest, and finally if your opponent is in zoom 3 your distance is sqrt(2)/2 and your opponent distance is sqrt(2), so in 3 cases with one oppnent you have better chance.
But because your shape has some blocks, you should calculate center position in other way, in fact, for each point in your grid calculate its distance to all other points. this will take 60^2 * 60^2 which can be done fast. and find cells with minimum total sums(you can select best 10 cells), this cells can be your centers, everytime you should move from one center to another (except when you are nearest to apple or your snake eats apple and wants comback to nearest centers) .
Nearest to the maximum number of locations is the center as others have stated. Nearer to the maximum number of locations than the other snakes is a much, different and harder questions. In that case, I would A* the head of each snake to see who has the most squares under control. That's the base score. Next, as I'm drawing a blank, I'd Monte Carlo a random set of points around the map and choose the point that gave the highest score as a destination. If you had the processing power, you could try every point on the grid and choose the best as K.G. suggested, but that could get pretty intense.
The true test is when you find your point, figure out how far in the future it takes you to get there, and running some AI for the other snakes to see if they will intercept you. You start getting into plys like chess. :)