How many rooks can hide on a chess board with blockers? [closed] - algorithm

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
You are given an m * n chessboard (where m≤n≤50 ) with x blocked cells. We know where the blocked cells are and we know the exact location of them.
Your job is to provide the maximum number of Rooks you can put on the chessboard so that no 2 Rooks attack each other.
Any pseudocode or even code in any language would be helpful.
input output sample:
in 3*3 chessboard,
x = 3
blocked Cells:
(0, 0),
(0, 1),
(0, 2)
answer = 2

Add a blocker after the last row in each column.
Sort the blockers by column first, row second.
Add an array with indices of the first blocker in each column.
For each row, find the unused column with the nearest blocker per free stretch (any will do if multiple candidates).
Place towers there and mark as used.
Advance all columns where you encountered a block and mark column as usable.
All-in-all, the algorithm should run in O(n * m + x * log(x)).

Related

Possible Array combinations based on constraints [closed]

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 6 years ago.
Improve this question
How many unique arrays of m elements exist such that they contain numbers in the range [1,n] and there exists atleast one subsequence {1,2,3,4....n}?
Constraints: m > n
I thought of combinations approach. But there will be repetitions.
In my approach, I first lay out all the numbers from 1 to n.
For example, if m=n+1, answer is n^2. (n spots available, each number in range [1,n])
Now, I think there might be a DP relation for further calculation, but I am not being able to figure it out.
Here's an example for n=3 and m=5. The green squares are the subsequence. The subsequence consists of the first 1 in the array, the first 2 that's after the first 1, etc. Squares that aren't part of the subsequence can either take n values if they are after the end of the subsequence, or n-1 values otherwise.
So the answer to this example is 1*9 + 3*6 + 6*4 = 51, which is easily verified by brute force. The coefficients 1,3,6 appear to be related to Pascal's triangle. The rest is left to the reader.

Dynamic Programming for sub sum elements in a matrix [closed]

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 4 years ago.
Improve this question
Given a chess board with 4 rows and n columns. On every cell there's an integer. Given 2n discs that part of them, or all of them you can put on a different cell on the board so the total sum is maximal. The only limitation is that 2 discs can't be next to each other vertically or horizontally. How to place the best combination of discs on the board in O(n) using DP ?
1st, we cannot possibly use more than 2*n disk as any column could contain at maximum 2 disks.
let's say d[i][mask] - maximum amount obtained after filling columns from 1 to i with disks so that the last column is filled as mask ( mask could be 0000, 0001, 0010, 0100, 0101, 1000, 1001 or 1010 where 1 means disk was placed and 0 means it wasn't)
So d[i][mask1] = max of (d[i-1][mask2] + number obtained from applying mask1 on i-th column), where mask2 could be any mask which doensn't contradict with mask1
Edit 1: You do not need to change anything. When you are on i-th step on certain mask, it only depends on the answers for masks of i-1. And you already have all of them. You just try to update the current d[i][mask] from those which are valid. As I already said d[i][mask] - stores the maximum value which could be obtained filling columns from 1 to i, optimally, so that the last column has disks in form of this mask (doesn't matter masks before the i-th column were filled, because they won't affect next column)

What's the minimal column sums difference? [closed]

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 7 years ago.
Improve this question
Imagine you are given a matrix of positive integer numbers (maximum 25*15, value of number does not exceed 3000000). When you do column sums and pick the smallest and the largest one, the difference between them must be the smallest possible.
You can swap numbers in every row (permute rows), not in column, how many times you want.
How would you solve this task?
I'm not asking for your code but your ideas.
Thanks in advance
I would make an attempt to solve the problem using Simulated Annealing. Here is a sketch of the plan:
Let the distance to optimize the difference between the max and min column sums.
Set the goal to be 0 (i.e., try to reach as close as possible to a matrix with no difference between sums)
Initialize the problem by calculating the array of sums of all columns to their current value.
Let a neighbor of the current matrix be the matrix that results from swapping two entries in the same row of the matrix.
Represent neighbors by their row index and two swapping column indexes.
When accepting a neighbor, do not compute all sums again. Just adjust the array of sums in the columns that have been swapped and by the difference of the swap (which you can deduce from the swapped row index)
Step 6 is essential for the sake of performance (large matrices).
The bad news is that this problem without the limits is NP-hard, and exact dynamic programming at scale seems out of the question. I think that my first approach would be large-neighborhood local search: repeatedly choose a random submatrix (rows and columns) small enough to be amenable to brute force and choose the optimal permutations while leaving the rest of the matrix undisturbed.

How many combinations are there to put n chapters into k(<n)volumes? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Also, how can algorithmicly generate them within polynomial of n? Pseudo-code is ok.
This problem comes down to selecting k-1 borders, which divide n elements in k parts. As such, k-1 positions need to be selected out of n+k-1 positions, resulting in
possibilities. As an example, let's say we need to divide four sweets among three children. In this case, a first possibility would be to put the two borders on the first two positions, leaving the four sweets in positions 3-6:
As such, the first two children get no candy, while the third child gets all four. Another possibility would be to put the first border at position 2 and the second border at position 4:
Now the first two children get one candy each (positions 1 and 3), while the third child gets two (positions 5-6). Iterating over all positions results in a total of 15 possibilities.
Note that the answer is different when all volumes need to contain at least one chapter. In this case, k items are already given to one volume each, leaving n-k chapters. As such, there are
possibilities. Note that the condition k<n is important in this case.

How to sort 3 dimensional point? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Suppose you have some points in 3D. You would like to sort it either increasing or decreasing order. You don't consider CW/CCW on sorting. How do you sort?
One method of sorting 3D points would be to compare their magnitudes - their distance from the origin point (0, 0, 0) - which may be computed using a 3D analogue of Pythagora's Theorem:
M = sqrt(x^2 + y^2 + z^2) http://www.sciweavers.org/upload/Tex2Img_1392933041/eqn.png
You'll then have a list of floats/doubles that may be sorted using any conventional sorting algorithm.
That's just the most common method, though. There exist infinitely many ways of comparing 3D points, some of which are more sensible than others. For example, which is the "bigger" point, (1, 0, 0) or (-10, -50, 5)? Comparing the X or Y coordinate would suggest the former being larger, while comparing the Z coordinate or magnitude suggests the latter being larger. None of these answers are completely right or wrong; it really depends on what you need your application to do.

Resources