I encountered the following problem in my programming book which I could not solve:
Given a nxm grid, write a recursive algorithm to find the number of ways that this grid could be filled by 3x1 and 1x3 blocks.
My logic for 3 x M grids:
Find the number of block combinations that could be used to fill side M of the grid.
I do not know how to change the logic to solve the question above.
Could someone please advise? Thanks.
Let position be the upper left corner, and thereafter the first unfilled slot of the grid (left to right then top to bottom). There are up to two ways to place a block at postion. Try placing a 1x3 horizontal block at position, and call the recursive function on the remaining grid. Try placing a 3x1 vertical block at position, and call the recursive function on that. Observe that if there is no legal way to place a block (at the end, for instance, say there are is only a 2x2 square left), this branch of the program failed to find a solution. That's because the grid has to be filled by 3x1 blocks.
Your logic isn't recursive - it's a combinatorial approach, trying to count. But as long a the grid isn't huge, the computer has enough memory to actually try all the combinations. If you could relate this approach to other problems solved recursively in the book that would be great.
Here's an idea of the method signature
int blockFillings(boolean[][] grid, int posx, int posy)
Related
I'm trying to approach this problem of moving objects on a grid from start to finish. I'm well aware of A* Pathfinding algorithm, but I'm a bit clueless to how I can modify it so it will handle my problem:
I have a WxH grid. I need to move the star box to the empty box position (they are always in the same positions: 0,0 and W-1,H-1). My starting point is the empty (W-1,H-1) place, and each step I take is non-diagonal. If I move up, I need to move the box that blocks my way downwards to the empty space, and so forth until I reach the star (0,0) and then I need to start moving it the same way towards my starting point. To make things easier, the movement of the star is always to the direction of the starting point and never away from it. I need to find the shortest route to do so, aka the shortest number of steps required to move the star to the start position.
Here is a 2x2 grid to illustrate the problem:
This is obviously a shortest path problem (maybe A*), but I can't figure out the modifications needed here. I'm not looking for solutions or answers, just for a direction, because I'm a bit lost of where should I even start.
P.S. the grid might also have immovable boxes, but I can handle this once I understand the algorithm behind the problem itself
I'm not looking for solutions or answers, just for a direction, because I'm a bit lost of where should I even start.
Hint: instead of treating each block as a node in the graph, treat the entire state of all the blocks as a single node. Then the neighbors of each node are the states that can be reached in a single move.
It is all about 3 robots, named A, B, and C, that can move around their environment using informed search. The environment contains obstacles. The 3 robots need to meet at some point such that the total distance they walk is minimum.
To make the problem a little computationally simpler, we will limit the obstacles to be rectangles. The 3 robots are always circles with radius 1. The goal state is to have the 3 robots (circles) to touch each other; the distance between the centers of every two robots is 1 unit. While moving, robots must not cut across any of the obstacles. In every step, one of the 3 robots moves one unit from the current location in each of the four directions: left, right, up and down. A move from one point to another must not go through any obstacle.
I just need a good heuristic function that can approximate the distance between the 3 robots can you help me guys?
I solved this issue here is the code Github code
I assume here that you want to minimize the number of total steps (only one robot moves at a time). If the robots all move at the same time and you want to minimize the time needed the solution would be different. Also, some fine tuning is necessary becuase the robots should just touch, not come to the same point.
You actually need a lower bound, not an approximation. A good lower bound can be calculated by disregarding any obstacles and check how many moves are necessary on an empty field. Since the robots can only move horizontally or vertically we consider these directions separately.
Take the horizontal direction first. The leftmost and the rightmost robot need to meet somewhere between them so independent where they meet, in total they have to move the horizontal distance between them. If they meet at the horizontal position of the middle robot, the middle does not have to move. So the horizontal steps necessary in any case is the horizontal distance between the leftmost and rightmost robot. Call this the horizontal span.
A similar argument holds for the vertical direction.
In total a lower bound is therefore the vertical span plus the horizontal span.
If each robot moves at the same time and it is known where the obstacles are at the beginning(so a known map), and obstacles are none moving this would work. Uou would need to generate a grid of sorts using a wavefront algorithm. This would be done for each robot in the beginning. Then going through each grid add the steps from each robot. Then find the grid with the lowest amount.
I have an array with the coordinates of the center of small circles which have the same radius. I know how to find when the mouse is over a circle, but my array is big and I want the fastest way to calculate this operation.
Is there a way of finding if the mouse is over a circle without looping all the array for each movement of the mouse?
Initially, set up some 'zones' for quicker reference:
Separate the whole surface into a small number of rectangles that don't intersect.
For each of these rectangles, list all circles that are at least partially contained in it. (A circle may end up listed in multiple rectangles, but that's okay.)
Every time you want to check whether the mouse is over a circle, you won't have to go through the whole array of circles. Instead:
Figure out which rectangle you're in.
Check only the circles that are listed under that rectangle.
This looks like a problem of optimizing the boundary check for a large number of items. The approach of going linearly does not scale well for thousands of circles.
This is a good topic to read on the net. But first, without going there, I'll try to explain (as an exercise) what I would explore. I would create a binary tree and partition the space, then instead of using an array I would put the circle points in such a tree. Looking the tree elements that are closer to the actual X,Y location becomes a matter of doing a binary search on the tree. The you have the closest point as a result of that search and can check for collision on it. There is still more to be done to the algorithm, and further optimizations are needed. For example, how to check for more points and not only the final one? Potentially I need a tree for the X coordinate, and another for the Y coordinate, etc... But I would explore these ideas. I will come back to this post and expand my reply with an actual example and a more concrete solution.
What if you check the coordinates that are r(radius) distance from the mouse? Then you could narrow your search down in the array if it is ordered.
I'm currently writing a graphical sudoku solver program in Java using Swing/Awt. So I decided I wanted to shade the boxes (marked with thicker lines) on the board the way these are (with 3x3 and 2x3 boxes):
(source: zitowolf.net)
(source: zitowolf.net)
A 6x6 board and a 8x8 board
A board in the program is basically represented as an int[dimension][dimension] array and the the program accepts boards with dimensions from 4x4 -> 16x16.
The problem is to make sure no boxes next to each other in a row or column has the same shading. The GUI shading process itself is not the problem, but finding out whether a square should be shaded as part of a shaded box.
I've tried for hours to implement a simple algorithm for this problem going through the array with a double for-loop, but I haven't gotten any reasonable pattern yet and I'm out of ideas. My algorithm so far is based on the assumption that every other box should be colored from top left to right bottom. This doesn't work for the 6x6 variant because there you have the sequence: colored box->white box->white box.
Could anyone here suggest an algorithm or another approach? I'll provide more info if needed. Much appreciated.
bool should_shade(x,y,size_x,size_y)
//sizes are size of small box and position x,y count from 0
{
return ( (x/size_x + y/size_y ) % 2) == 0; //division is integer division
}
I am trying to solve a problem of compositing two images in Java. The program will take a part of the first image and past it on the second image. The goal is to make the boundary between the two images less visible. The boundary must be chosen in such a way that the difference between the two images at the boundary is small.
My Tasks:
To write a method to choose the boundary between the two images. The method will receive the overlapping parts of the input images. This must first be transformed so that the boundary always starts from the left-top corner to the right-bottom corner.
NOTE:
The returned image should not be the joined image but gives which parts of the two images were used.
The pixels of the boundary line can be marked with a constant(SEAM). Pixels of the first image can be marked with integer 0, pixels of the second image with integer 1. After choosing the boundary line, the floodfill algorithm can be used to fill the extra pixels with 0 or 1.
NOTE: The image can be represented as a graph whereby each pixel is connected with its left, right, top and bottom neighbor. So using the flood fill will be like depth-first search.
The shortest path algorithm must be used to choose the boundary in order to make it small.
NOTE: I can not use any java data structure except Arrays (not even ArrayList)
Guys, am new in this area and am trying to solve it. What steps must I follow to solve this problem? or a pointer to a tutorial
I would do it so:
Choose the width of the border checked. At your will.
1. find the maximal possible shift in pixels. That is D.
2. For all possible shifts in the square (+-D,+-D) find the k (correlation quocient) for the border. The border is taken in the middle of the shift.
3. The shift that has the largest k is the best. Let it be taken for granted.
4. Now begin to move the border, checking it by "k" the same way. Find the place of it. Done.
If D is large and the process is long, do it in 2(or more) stages. On the first stages the step of counting k is large, the last stage has step of 1. You could also use previous filtering.
If the border or relative images' position could be turned, the algorithm doesn't change principally - only add to it trying for the best k among different slightly turned positions and later - turned border, too.