I am trying to find a solution to this board game problem this is how it goes.
Mike and Bob are playing a board game on an m by n board. The board has at least 3 rows and 3 columns. The bottom row is row 0 and the top row is row m − 1; the left-most column is column 0 and the right-most column is column n − 1.
Mike moves first, then Bob, then Mike, then Bob, etc. until the game is over. The game is over when one of two things happens: Bob wins or Mike wins.
Bob wins if he lands on the same square as Mike before Mike reaches the top row. Note that this winning condition is checked only after Bob moves; Bob can never win right after Mike moves, even if Mike lands on the same square as Bob
Mike wins if Mike reaches the top row before Bob wins, i.e., Mike reaches the top row without Bob
ever landing on the same square as Mike. As soon as Mike reaches the top row, Mike wins (Bob cannot move
anymore).
Mike’s move on each turn is fixed according to the following rule: he goes 1 square up and then, if not already at the
right edge, goes 1 square to the right as well.
Bob, by contrast, has eight choices of move to make on his turn:
1 up, 2 right
1 up, 2 left
1 down, 2 right
1 down, 2 left
2 up, 1 right
2 up, 1 left
2 down, 1 right
2 down, 1 left
Note that some moves may be unavailable depending on Bob’s location; for example, if Bob is already in column
n − 1, then any move that tries to go to the right is not allowed.
Given the starting locations of Mike and Bod, come up with a solution that determines the result of the game, as follows:
If it is possible for Bob to win, then report that Bob can win and give the minimum number of Bob moves required for him to win.
Otherwise, Mike wins; report the number of Bob moves that occur before Mike wins
we can assume that
Mike’s starting location is never in the top row
Bob’s starting location is never the same as Mike’s
we can use any type of methods we like, the only real restriction to solving this problem are mentioned above. How would we approach a question like this.
Notation
First note that Mike has a fixed, predefined movement strategy.
Let us denote by M0, M1, .., Mk the cells that Mike would go through in his path towards the top row. Here M0 is the starting cell of Mike, and Mk is the cell on the top row that Mike would reach if Bob would not intervene.
Additionally, let us denote by B0 the cell where Bob starts from. According to the statement it is guaranteed that B0 is different from M0.
Reformulation
The question is whether there is sequence of i moves for Bob, B0 -> B1 -> .. -> Bi such that Bi = Mi, for an i between 1 and k - 1.
Approach
One possible approach is to generate the list of all possible cells that can be reached in exactly i moves, for any i.
For i = 0, there is a single cell that can be reached by Bob, the starting position B0.
Afterwards, for i = 1, 2, .. k - 1, we should consider every cell that can be reached in i - 1 steps (we already know this set of cells), and perform each legal move for Bob. The resulting list of positions would be the set of cells reachable in i steps.
Now that we found the cells that can be reached in i steps by Bob, we should simply check whether Mi is in that set. If this happens for at least one i between 1 and k - 1, then Bob wins.
Implementation
For efficiency, it is important to make sure that if the same cell is reached from different sources in the same number of steps, i, then the duplicates are stored only once.
This removal of duplicates is essential, because otherwise the size of the list of reachable cells would grow exponentially with i.
One approach would be to maintain an m by n matrix of booleans for each i, specifying whether the respective cell can be reached in i steps. Improvements in terms of memory are possible, such that only the matrix of booleans for the current i is maintained (along with a temporary matrix to help advancing towards the next set), but a little bit more care would be needed in that case.
Old topic, but I feel like puzzling for a moment:
Start by general observations
On a 3 by 3 board Mike wins in exactly 4 starting positions, namey those where Bob has to move to (1, 2) (order of coordinates here m before n, i.e. not common to chess notations) und Mike can move there.
If Bob moves onto Mike's square, Mike can win the very next move or never at all, because he generally needs two moves from Bob's square to Bob's next square.
So as long as Bob moves diagonally it is just a matter of distance and speed (Mike is 50% faster, just calculate distance to Bob's diagonal).
On the right border Mike is 100% faster. He still has to catch up before the top. Plus Mike has no way of winning if both players end up on different colors on one and those each move.
Related
I've been practicing some probabilities for last few weeks around 10/week usually did the job, however as the topics got harder I've started struggling and now I'm completely stuck. I've been looking online and i did find similar examples but nothing to touch my case in particular. I will continue looking for an answer so even if u can't answer my specific cases links to online literature will be appreciated.
answers are welcome, however i would rather be more interested in explanation of how a problem works.
urn contains m white and k black balls. two players are drawing the balls one after another without putting them back into the urn. the winner is the first person to draw white ball. what is the probability that second player will be the winner? (k=4, m=4)
women tend to vote with probability of a, men tend to do the same with probability of b. the probability c tells us that if we take a couple one of them will not go voting. what are the chances that at least one of them will vote? (a=0.49, b=0.61, c=0.75)
we are sending a message of n bytes. to obtain higher chance of sending entire message without ruining it we use k different wires. What is the probability of sending an entire message through one of the wires without ruining it, if the probability of ruining any of the bytes at any of the wires is p. (p=0.06, n=7, k=6)
the basketball game finals play to N wins. after m + n games the result is m : n. what is the probability of winning the finals for the lagging(the team that's behind) team if it is known that the leading team wins each match with a probability of p? (m=3 n=2 N=5 p=0.36)
sorry for my English and any help will be appreciated
Just A
So Given m = k and both = 4. And each player takes a ball out per turn. Given the worst case scenario 4 good balls will be picked (K) there will be 2 turns of both players picking the black ball then the first player will be guaranteed the white ball next turn. Therefore you need to work out the probability for the first two turns.
Turn 1
- P1 turn 1 Chance for black= 4/8
- P2 turn 2 Change for white = 4/7
Therefore turn 1 chance = (4/8)*(4/7). = 28.57%
Turn 2
- P1 turn 1 Chance for black= 2/6
- P2 turn 2 Change for white = 4/5
Therefore turn 2 chance = (4/6)*(4/5). = 26.664% but to get to turn 2 is a 28.57% chance
Therefore its the first probability add the second probability given the first case occurs. So 28.57% + (26.44%*28.57%)= 36.12%
The third case is a always lose. Top tip then be the first player!
Turn 3 Instance win for player 2 therefore a loss. = 0%
I have an n x n adjacency matrix for a directed graph. I want to search to see if any of the columns sum up to n. The problem is that I have to do this in O(n) time. Is there any way to approach this with O(n) time or is it not possible (no code required)?
For reference, below is the question I am trying to solve:
Problem:
During the school bandmates election, each person has a few preferences for the president and the set of preferences for a person includes him/herself all the time. The "perfect" president is the one who is in the set of preferences of every person and who does not prefer anyone but him/herself. All we want to know is whether such a person exists or not.
Define a directed graph with the set of candidates as the vertices and a directed edge from vertex a to vertex b if and only if b is in the set of preferences of a.
There are n people
We want an algorithm which executes in O(n) time
We are given the graph described above in the form of an n x n adjacency matrix
I figured that if everyone votes for the "perfect president", then he/she will have n incoming nodes, therefore summing the column should give us n. If there is a better way to approach this than the way I am doing it, any hints would be appreciated to guide me in the right direction.
Let me repeat the rules and give them numbers for easy reference:
All prefer themselves
All prefer the perfect president (if there is one)
The perfect president (if there is one) does not prefer anyone else
Let's define the adjacency matrix so that the value in row i and column j is 1 when person i has a preference for person j. All other values are 0.
The above three rules can be reformulated in terms of the adjacency matrix as follows:
The main diagonal of the adjacency matrix will have all 1's.
If the perfect president is number i, then column i will have all 1's.
If the perfect president is number i, then row i will have all 0's, except in column i.
Note that there cannot be two or more perfect presidents, as then they would have to prefer each other (rule 2), which violates rule 3.
Algorithm: Zig-Zag Phase
The perfect president (if existing) can be found in linear time, by zig-zagging from the top-left corner of the adjacency matrix (row 0, column 0), according to the following rules:
If the value is 1, then move down to the next row (same column)
If the value is 0, then move right to the next column (same row)
While staying within the boundaries of the matrix, keep repeating the first two steps.
If you exit the matrix this phase ends. Let's call the column, where we exit the matrix, column p.
Observation: because of rule 1, this part of the algorithm will never get into the values above the main diagonal: the 1-values on that diagonal are like a wall that pushes us downward whenever we bump into it. You will therefore always exit the matrix via a downward move from the last row. In the most extreme case it would be a downward move at the 1 that is in the right-bottom corner (on the diagonal) of the matrix.
Here is a simple example of an adjacency matrix where arrows indicate how the algorithm dictates a path from the top-left corner to a 1 somewhere in the bottom row:
1 1 0 1 0
↓
1 1 0 1 0
↓
0 → 1 1 1 0
↓
0 0 → 0 → 1 0
↓
0 1 1 1 1
↓
=p
Note that in this example there is a perfect president, but that is of course not a requirement: the algorithm will give you a value p whether or not there is a perfect president.
Claim: The perfect president, if there is one, has to be the person with number p.
Proof
Given is the p returned by the above zig-zag algorithm.
First we prove by contradiction that:
a) the perfect president cannot be a person with a number i less than p.
So let's assume the contrary: the perfect president is person i and i < p:
Since we start the zig-zag phase in column 0 and ended up in column p, and since we cannot skip a column during the process, there was a moment that we were in column i. Since we didn't stay in that column, it must mean that column i has a zero in one of its rows which called us to move to the right. But rule 2 demands that if i is a perfect president, column i should only have 1-values (rule 2). Contradiction!
Secondly we prove by contradiction that:
b) the perfect president cannot be a person with a number i greater than p.
So let's assume the contrary: the perfect president is person i and i > p:
Since we start the zig-zag phase in row 0 and reached the last row (cf. the observation), and since we cannot skip a row during the process, there was a moment that we were in row i. Since we didn't stay in that row, but moved down at some point (cf. the observation: we moved out of the matrix with a downward move), it must mean that row i has a 1 in one of its columns which called us to move downwards. This 1 cannot be the 1 that is on the diagonal (at [i,i]), because we did not reach column i: i is greater than p and the algorithm ended in column p. So it must have been another 1, a second one, in row i.
But rule 3 demands that if i is a perfect president, row i should only have one 1-value, with all other values being zeroes. Contradiction!
These two proofs by contradiction leave us no other possible number for the perfect president, if there is one, than number p.
Algorithm: Verification Phase
To actually check whether person p is indeed a perfect president is trivial: we can just check whether column p contains all 1's, and that row p only contains 0's, except in column i.
Time Complexity
All this can be done in linear time: at most 2n-1 reads from the matrix are required in the zig-zag phase, and 2n-2 more in the final verification phase. Thus this is O(n).
Given N binary sequence
Example :
given one sequence 101001 means
player 0 can only choose a position with 0 element and remove the sequence from that position resulting {1 if he choose 2nd element or 101 if he choose 4nd element or 1010 if he choose 5th element}
player 1 can only choose a position with 1 element ans remove the sequence from that position resulting {null if he choose 1st element or 10 if he choose 3rd element or 10100 if he choose 6th element}
now player 0 and player 1 take turn reducing N sequence, on each turn they pick one sequence, pick an element and remove from that position the the end of the chosen sequence, if a player can't make a move, he lose.
Assume both player play optimally, who will win ?
I tried to solve this problem with grundy but i'm unable to reduce the sequence to a grundy number because it both player don't have the same option to move. Can anyone give me a hint to solve this problem ?
btw sorry for my bad english
It's not Nim. This is the game of Blue-red Hackenbush. There's even an online hackenbush calculator which could solve this specific case (just change B and R to 0 and 1), along with this short explanation of the algorithm:
Until a color change, each segment is worth +1 or -1 (depending on whether it is Blue or Red, respectively).
Once a color change occurs, each subsequent segment (regardless of color), is worth half of the previous segment, with a +/- corresponding to the color.
Thus, the string BBRB would be worth +1+1-1/2+1/4=7/4.
So you can compute the value of each sequence. (Let's suppose player 0 is assigned to the positive side, that is, "0" evaluates to +1.) If the sum of these values over all sequences is positive, then player 0 wins. If it's negative, player 1 wins. And if it's 0, then whoever move first loses.
Ok this is my second try
if player X found X at the top of the stack or list X------- he will take it resulting an empty list that it will leave no moves to the other player and X will win
If Player X found Y at as the first element of the list he will lose any way because what ever he choose on the next turn the player Y will take the first element leaving the Player X with empty list and X will lose
like
YXXXXXXXX
if player X chose any X from the list, Y will select the first and win.
Did I get your point or not Yet
Edit: as pointed out following is not an optimal solution.
IMO if there is only one sequence, then the Player whose number starts the sequence shall win the game. This is because at his/her turn, he/she will simply remove the first element resulting in NULL string and so no move for the other player.
For multiple sequences I can't find a better optimal strategy than following:
Now let us assume there are m sequences that start with 1 and k sequences that start with 0. Each player's strategy shall be to quickly finish the other player's winning sequences.
Hence Player 0 shall choose the first zero in the m sequences beginning with 1 and Player 1 shall choose the first one in the k sequences beginning with 0.
The player looses whose winning sequence finishes first.
I will compete in the OBI (Brazilian Olympiad of Informatics, in English) and I am trying a few exercises from the last years. But I can't find a solution for this exercise (I translated it, so there may be a few errors):
Chocolate Competition
Carlos and Paula just got a bag of chocolate balls. As they would eat
everything too quickly, they made a competition:
They will eat alternately, one after the other (Paula always starts).
Each time, they can only eat from 1 to M balls, M decided by Paula's mother, so they don't choke.
If one ate K balls in his/her turn, the next can't eat K balls.
Whoever can't play according the rules above loses.
In the example below with M = 5 and 20 balls, Carlos has won:
Who plays How many ate Balls left
20
Paula 5 15
Carlos 4 11
Paula 3 8
Carlos 4 4
Paula 2 2
Carlos 1 1
Note that in the end, Carlos couldn't eat 2 balls to win, because Paula ate 2 in her last turn. But Paula couldn't eat the last ball, because Carlos ate 1 in his last turn, so Paula can't play and loses.
Both are very smart and play optimally. If there is a sequence of turns that ensures him/her the victory independent of the other's turns, he/she will play these sequences.
Task:
Your task is to find out who will win the competition if both play optimally.
Input:
The input contains only a single test group, which should be read from the standard input (usually the keyboard).
The input has 2 integers N (2 ≤ N ≤ 1000000) and M (2 ≤ M ≤ 1000), being N the number of balls and M the number allowed per turn.
Output:
Your program should print, in the standard output, one line containing the name of the winner.
Examples:
Input: Output:
5 3 Paula
20 5 Carlos
5 6 Paula
I've been trying to solve the problem, but I have no idea how.
A solution in C can be found here: http://olimpiada.ic.unicamp.br/passadas/OBI2009/res_fase2_prog/programacao_n2/solucoes/chocolate.c.txt But I can't understand the algorithm. Someone posted a question about this problem in another site, but nobody replied.
Could you explain me the algorithm?
Here are the expected outputs of the program: http://olimpiada.ic.unicamp.br/passadas/OBI2009/res_fase2_prog/programacao_n2/gabaritos/chocolate.zip
Let's say we have a boolean function FirstPlayerWin (FPW) that takes two arguments: number of chocolates left (c) and the last move (l) i.e. the number of chocolates taken on the previous round, which is 0 at the first move. The routine returns true if and only if the first player to play at this situation is guaranteed a win.
The base case is that FPW(0, l) = false for any l != 1
Otherwise, to calculate FPW(c, l), FPW(c, l) is true if for any x <= M, x <= c, x != l, FPW(c - x, x) is false. Otherwise it is false. This is where dynamic programming kicks, because now the calculation of FPW is reduced to calculating FPW for smaller values of c.
However, storing the entries for this formulation would require N * M table entries, where as the solution you pointed to uses only 2N table entries.
The reason for this is that if FPW(c, 0) is true (first player wins if any move is available at chocolate count c) but FPW(c, x) is false for x > 0, FPW(c, y) for and y != x must be true. This is because if denying the move x makes the player lose, i.e. the player would win only by playing x, then the move x is available when y is banned instead. Therefore it is enough to store for any count 'c' at most one forbidden move that causes the player to lose there. So you can refactor the dynamic programming problem so that instead of storing the full 2-dimensional array FPW(c, x) you have two arrays, one stores the values FPW(c, 0) and the other stores the single forbidden moves that cause the first player to lose instead of winning, if any.
How you get to the exact text of the quoted C program is left as an exercise to the reader.
I think this is yet another thinly disguised exercise in dynamic programming. The state of the game is described by two quantities: the number of balls remaining, and the number of balls eaten in the previous move. When the number of balls remaining is <= M, the game is either won (if the number remaining is not equal to the number eaten in the previous move) or lost (if it is - you can't eat all the balls, and your opponent can eat the balls that you leave).
If you have worked out the win/lose situation for all numbers of balls up to H, and all possible numbers of balls eaten by the previous player and written this down in a table, then you can work out the answers for all numbers of balls up to H+1. A player with H+1 balls and k balls eaten in the previous move will consider all possibilities - eat i balls for i = 1 to M except for the illegal value of k, leaving a position with H+1-i balls and a previous move of i. They can use the table giving the win-lose situation for up to H balls left to try and find a legal k that gives them a win. If they can find such a value, the H+1/k position is a win. If not, it is a loss, so they can extend the table to cover up to H+1 balls, and so on.
I haven't worked through all the uncommented example code, but it looks like it could be doing something like this - using a dynamic programming like recursion to build a table.
I'm training code problems, and on this one I am having problems to solve it, can you give me some tips how to solve it please.
The problem is taken from here:
https://www.ieee.org/documents/IEEEXtreme2008_Competitition_book_2.pdf
Problem 12: Cynical Times.
The problem is something like this (but do refer to above link of the source problem, it has a diagram!):
Your task is to find the sequence of points on the map that the bomber is expected to travel such that it hits all vital links. A link from A to B is vital when its absence isolates completely A from B. In other words, the only way to go from A to B (or vice versa) is via that link.
Due to enemy counter-attack, the plane may have to retreat at any moment, so the plane should follow, at each moment, to the closest vital link possible, even if in the end the total distance grows larger.
Given all coordinates (the initial position of the plane and the nodes in the map) and the range R, you have to determine the sequence of positions in which the plane has to drop bombs.
This sequence should start (takeoff) and finish (landing) at the initial position. Except for the start and finish, all the other positions have to fall exactly in a segment of the map (i.e. it should correspond to a point in a non-hit vital link segment).
The coordinate system used will be UTM (Universal Transverse Mercator) northing and easting, which basically corresponds to a Euclidian perspective of the world (X=Easting; Y=Northing).
Input
Each input file will start with three floating point numbers indicating the X0 and Y0 coordinates of the airport and the range R. The second line contains an integer, N, indicating the number of nodes in the road network graph. Then, the next N (<10000) lines will each contain a pair of floating point numbers indicating the Xi and Yi coordinates (1 < i<=N). Notice that the index i becomes the identifier of each node. Finally, the last block starts with an integer M, indicating the number of links. Then the next M (<10000) lines will each have two integers, Ak and Bk (1 < Ak,Bk <=N; 0 < k < M) that correspond to the identifiers of the points that are linked together.
No two links will ever cross with each other.
Output
The program will print the sequence of coordinates (pairs of floating point numbers with exactly one decimal place), each one at a line, in the order that the plane should visit (starting and ending in the airport).
Sample input 1
102.3 553.9 0.2
14
342.2 832.5
596.2 638.5
479.7 991.3
720.4 874.8
744.3 1284.1
1294.6 924.2
1467.5 659.6
1802.6 659.6
1686.2 860.7
1548.6 1111.2
1834.4 1054.8
564.4 1442.8
850.1 1460.5
1294.6 1485.1
17
1 2
1 3
2 4
3 4
4 5
4 6
6 7
7 8
8 9
8 10
9 10
10 11
6 11
5 12
5 13
12 13
13 14
Sample output 1
102.3 553.9
720.4 874.8
850.1 1460.5
102.3 553.9
Pre-process the input first, so you identify the choke points. Algorithms like Floyd-Warshall would help you.
Model the problem as a Heuristic Search problem, you can compute a MST which covers all choke-points and take the sum of the costs of the edges as a heuristic.
As the commenters said, try to make concrete questions, either here or to the TA supervising your class.
Don't forget to mention where you got these hints.
The problem can be broken down into two parts.
1) Find the vital links.
These are nothing but the Bridges in the graph described. See the wiki page (linked to in the previous sentence), it mentions an algorithm by Tarjan to find the bridges.
2) Once you have the vital links, you need to find the smallest number of points which given the radius of the bomb, will cover the links. For this, for each link, you create a region around it, where dropping the bomb will destroy it. Now you form a graph of these regions (two regions are adjacent if they intersect). You probably need to find a minimum clique partition in this graph.
Haven't thought it through (especially part 2), but hope it helps.
And good luck in the contest!
I think Moron' is right about the first part, but on the second part...
The problem description does not tell anything about "smallest number of points". It tells that the plane flies to the closest vital link.
So, I think the part 2 will be much simpler:
Find the closest non-hit segment to the current location.
Travel to the closest point on the closest segment.
Bomb the current location (remove all segments intersecting a circle)
Repeat until there are no non-hit vital links left.
This straight-forward algorithm has a complexity of O(N*N), but this should be sufficient considering input constraints.