Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i need some help with a math task our professor gave us. Any suggestions would help.
The problem is:
There are N cannibals and M missinaries. All missionaries have a strenght attribute, which can be 1 or any positive whole number. Strenght indicates how many cannibals he can fight off.
Basically: there are two sides of the river, there is a 2-slot boat, and you have to transfer all the guys to the other side, without letting the cannibals eat the missionaries.
How would you write a program for this? What would be the transferring-grouping algorythm?
Thanks in anticipation,
Mark.
Model your problem as a states graph.
In here, a state is ({L,R}n,{L,R}m,{L,R}) Where:
First n entries: one for each missionary - where he is: left/right bank of the river
next,m entries: one for each canibal- where he is: left/right bank of the river
Last entry is for the boat
These are your vertices - you should also trim the invalid states - where strength of missionaries is not enough in one (or more) side. It is easy to calculate it for each state.
Your edges are:
E = { (S1,S2) | Can move in one boat ride from S1 to S2 }
All is left to do - use some shortest path algorithm to find the shortest path from: (L,L,....,L) to (R,R,...,R).
You can use BFS for this task, or even bi-directional search - or an informed algorithm (with admissible heuristic) such as A* Algorithm.
PS. The 'graph' is just conceptual, in practice you will have a function next:S->2^S, that given a state - returns all valid successors of this state (states that you can get to them using one edge on the graph from S). This will allow you to "generate the graph" on the fly.
Your next(S) function should be something like (high level pseudo code, without optimizations):
next(S):
let x be the bank where the boat is, and y the other bank
for each person p1 on bank x:
S' = S where boat and p1 moved from x to y
if S' is valid according to strength limitations, yield S'
for each p2 != p1 on bank x:
S' = S where boat and p1 and p2 moved from x to y
if S' is valid according to strength limitations, yield S'
Related
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 2 years ago.
Improve this question
I have a 3d box random in space and know the nodes of it. A ray is intersecting this box. I would like to know the length of this intersection and have no idea how to code this in C or describing it mathematically.
This can be broken down into 3 parts:
a) Use "formula for intersection between line and plane" to determine if/where ray hits each plane; where each plane is determined from 3 vertices of the box (from each face of the box)
b) Determine if the intersection point for each plane (if any) is inside or outside the corresponding face of the box. For this I'd flatten everything to 2D (e.g. discard x, y or z for the plane's vertices and its intersection point, depending on which part of the plane's surface normal is largest); then pick a direction (e.g. "towards +x") and see if a line (in 2D space) from the intersection point heading in the chosen direction hits one edge of the face (and not zero edges or 2 edges). From this you get a list of intersections with faces of the box.
c) Find the closest "intersections with faces of box". This is mostly just a simplified Pythagoras thing ("distance_squared = distance_x * distance_x + distance_y * distance_y + distance_z * distance_z") with some "if(distance_squared < best_distance_squared_seen_so_far)".
Note 1: This method works for arbitrary objects/meshes with any number of faces (not just boxes); however if you wanted to support concave polygons (for faces) you'd need to determine if (after flattening to 2D) a line from the intersection point heading in a chosen direction hits an odd number of edges (and not just one edge).
Note 2: There's a tricky case where (after flattening to 2D) a line from the intersection point heading in a chosen direction hits a vertex perfectly. In this case you need to determine if it hits an edge or misses both edges by determining if both edges leading from the vertex are on the same side of your line. I don't think you need to care about this case for boxes (and can assume "hits vertex = hits an edge") but would need to care about it for arbitrary objects/meshes.
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
Suppose you have two strings. Each string has lines, seperated by a newline character. Now you want to compare both strings and then find the best method (shortest number of steps) by only adding or deleting lines of one string, to transform the second string in to the first string.
i.e.
string #2:
abc
def
efg
hello
123
and string #1:
abc
def
efg
adc
123
The best (shortest steps) solution to transform string #2 in to string #1 would be:
remove line at line position 3 ('hello')
add 'abc' after line
position 3
How would one write a generic algorithm to find the quickest, least steps, solutions for transforming one string to another, given that you can only add or remove lines?
This is a classic problem.
For a given set of allowed operations the edit distance between two strings is the minimal number of operations required to transform one into the other.
When the set of allowed operations consists of insertion and deletion only, it is known as the longest common subsequence edit distance.
You'll find everything you need to compute this distance in Longest common subsequence problem.
Note that to answer this question fully, one would have to thoroughly cover the huge subject of graph similarity search / graph edit distance, which I will not do here. I will, however, point you in directions where you can study the problem more thoroughly on your own.
... to find the quickest, least steps, solutions for transforming
one string to another ...
This is a quite common problem known as the (minimum) edit distance problem (or, originally, the specific 'The String-to-String Correction problem', by R. Wagner and M. Fischer), which is a non-trivial problem for the optimal (minimum = least steps) edit distance, which is what you ask for in your question.
See e.g.:
https://en.wikipedia.org/wiki/Edit_distance
https://web.stanford.edu/class/cs124/lec/med.pdf
The minimum edit distance problem for string similarity is in itself a subclass of the more general minimum graph edit distance problem, or graph similarity search (since any string or even sequenced object, as you have noted yourself, can be represented as a graph), see e.g. A survey on graph edit distance.
For details regarding this problem here on SO, refer to e.g. Edit Distance Algorithm and Faster edit distance algorithm.
This should get you started.
I'd tag this problem rather as a math problem (algorithmic instructions) rather than language specific problems, unless someone could guide you to an existing language (C) library for solving edit distance problems.
The fastest way would be to remove all sub-strings, then append (not insert) all new sub-strings; and to do "all sub-strings at once" if you can (possibly leading to a destPointer = sourcePointer approach).
The overhead of minimising the amount of sub-strings removed and inserted will be higher than removing and inserting/appending without checking if its necessary. It's like spending $100 to pay a consultant to determine if you should spend $5.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I'm stuck with a quite complex problem:
On an MxN field containing a chicken, an eagle and a yard,
the chicken tries to escape the eagle (by entering the yard),
and the eagle tries to catch the chicken. The chicken escapes
when reaches inside the yard, and the eagle catches the chicken
when it's in the same position as the chicken. In a single step,
the eagle can move one or two small squares, and the chicken can
move a single square in any direction. The program should display
a message saying if the chicken can win. It should compute the moves,
and, at each step, it should write in the output file the current
configuration of the field, and it should also visually represent
it on the screen. The dimensions of the field, position of the chicken
and of the eagle, and also of the yard, are given in a file.
I've solved the part with creating the field (a matrix), but I can't figure it out how to solve this. Perhaps backtracking would be an idea, but it's very complicated, and I can't handle it. I think I should find a way to find out the distance between the chicken and the yard, also between the eagle and the yard, and work somehow with that. It has to be in C. Any suggestion, idea is welcomed!
Thank you in advance!
It is an interesting problem. Let's go over the rules again. Players
Chicken: takes shortest path to field (there could be multiple shortest paths) and away from eagle (maximise the distance between itself and eagle among shortest paths).
Eagle: takes shortest path to chicken
To solve the problem we have to assume it is played in turns: first chicken then eagle and so on.
Game is over when :
Eagle is on chicken.
Chicken is on field.
Here is the trick for the distance:
Update
The distance you want is called Chebyshev distance. You can easily calculate it:
distance = max of(difference of corresponding coordinates between the two points)
For (1,1) and (2,3) distance = max(|1-2|,|2-3|) = 2
For (2,3) and (4,7) distance = 4
For (4,5,6) and (1,1,1) distance = 5
You can ignore the older answer if you want.
Old
distance = Manhattan distance - length of longest 45 deg diagonal
Manhattan distance is easy to understand. See its wiki. Take some examples :
---/F
--/-|
-/--|
C---X
manhattan distance = 7
length of max diagonal = 3
distance = 7-3 = 4
Another one
---/-F
--/--|
-/---|
C----X
distance = 8-3 = 5
Caveat: Remember there can be many shortest possible paths. For eg.
---F
--/F
-/-F
C--F
-\-F
--\F
---F
Lots of places to go in 3 moves. Pick one which is farthest from eagle using distance calculator.
Also if distance between eagle and chicken is less than chicken and field at any time then eagle wins else chicken. Just simulate the moves and you will know.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm actually working on a board game which is a variant of the TIC-TAC-TOE game. The specifics of the game are the followings :
1. The game is played on a nxn board, with n variable.
2. A player wins if he succeeds in placing k alignments the first, k is variable.
3. An alignment is constituted of l marks (X or O) in a horizontal, vertical or diagonal. l is fixed.
4. If the nxn grid is full (no player can add a mark either X or O) and no player succeeded in placing k alignments so the game is drawn.
I'm using an minmax with alpha-beta prunning algorithm. This is my first program with artificial intelligence and I don't know exactly how to create the evaluation function to be used by the algorithm. I saw some examples on the net which use a material weighting to evaluate a position but I can't apply that in my case. Actually, I'm using a radom evaluation function which returns a value between -100 and 100.
float Conf_eval(Configuration c)
{
return (rand()%201)-100;
}
Any idea on how can I evaluate a given board configuration ?
This is thoroughly discussed in the book Artificial Intelligence - A Modern Approach
There are also excellent implementations available (this is java, there is also python, you can Google for more) based on the book series. Including for tic-tac-toe (and alpha-beta pruning agents).
If you're using the min-max algorithm with alpha-beta prunning, you can use a sorted "Actions" list to perform better in addition to your heuristic function (a trivial utility-function would be assigning 1 to a victory, 0 to a tie and -1 to a loss - these are all leaf nodes of the min-max expanded tree).
To sort the actions you can, for say, prefer actions that add your symbol(X, O) to a clear-to-victory path. This should eventually lead to better prunning.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
There are n vehicles on an n x n grid. At the start they are ordered in the top row 1. The vehicles have to get to the bottom row such that the vehicle at (1,n) must get to (n, n − i + 1). On each time step, each of the vehicles can move one square up, down, left or right, or it can stay put. If the vehicle stays put, one adjacent vehicle (but not more than one) can hop over it. Two vehicles cannot occupy the same square.
Which of the following heuristics are admissible for the problem of moving all the vehicles to their destination?
i. sum from 1 to n (h1 ... hn)
ii. max(h1 ... hn)
iii. min(h1 ...hn)
I think that iii is the only correct one, but I'm not sure how to formulate my reasoning on why.
I am sure someone will come along with a very detailed answer, but as a favour to those who like me can be a bit overwhelmed by all things AI, an admissible heuristic is quite simply:
A heuristic that never overestimates the true cost of getting to the goal
Not to sound too uncharitable, but it sounds as if maybe the problems you've posted are from a homework problem or assignment. I wouldn't want to spoil your fun working out exactly which of those three heuristics are and aren't admissible - but hopefully that one sentence definition should help you along.
If you get confused, just remember: if once your vehicles have both reached their goals you find the actual cost was less than what the heuristic thought it would be then it's inadmissable.