0% found this document useful (0 votes)
66 views41 pages

Problem Solving

Uploaded by

sahil1740b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views41 pages

Problem Solving

Uploaded by

sahil1740b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Problem Solving

Subject Code: CSEG1027

Dr. Sanoj Kumar


School of Computer Science
University of Petroleum and Energy Studies, Dehradun
Email id: [email protected]
Mb. Num.: 9058523010
Empty Boxes
Strategy
1. The unknown is the number of boxes in the final state; what is known is the number of empty

boxes.

2. Variables b and e for the number of boxes and the number of empty boxes, respectively, at an

arbitrary point in time.

3. Goal— determine the number of boxes given the number of empty boxes — to the

introduction of variables b and e.

A common mistake is to try to count the number of medium boxes or the number of small boxes.
These are irrelevant, and a solution that introduces variables representing these quantities is over-
complicated. This is a key to effective problem solving: keep it simple!
THE TUMBLER PROBLEM

Several tumblers are placed on a table. Some tumblers are upside down, some are upside
up. It is required to turn all the tumblers upside up. However, the tumblers may not be
turned individually; an allowed move is to turn any two tumblers simultaneously.

From which initial states of the tumblers is it possible to turn all the tumblers
upside up?
Crossing River

Brute-force search means systematically trying all possibilities. Brute force does not
require any skill, but it does require a lot of careful and accurate work. Using brute
force is not something human beings are good at; lots of careful, accurate work is
something more suited to computers. But brute force is not even practical for
implementation on a computer. The amount of work involved explodes as the problem
size gets bigger, making it impractical for all but toy problems.
Nevertheless, it is useful to know what brute force entails, because it helps to
understand the nature of problem solving.
Nervous Couples
Three couples (president and bodyguard) wish to cross a river. They have one boat,
that can carry at most two people, making several trips across the river necessary.
The current is swift, the river is deep, and none of them can swim. Each president is
nervous about the other presidents' bodyguards, so no president is willing to be on a
bank or in the boat with another president's bodyguard, unless his or her own
bodyguard is also present.

How can all three couples cross the river?


Three couples are on the vacation. They need to cross the river to reach their hotel. There
are 3 rules which are as follows:-
Rule 1:- The boat can only carry two people at a time. If the third person trying to get in the
boat then the boat will sink.
Rule 2:- The husbands are so jealous that they can not let their wife with another man,
without their presence.
Rule 3:- The boat cannot run on itself. At-least one person should be on the boat to go from
one side to another.
Solution:
Let the 3 couples have wore green, red and blue dresses. So, there should be green wife and green
husband, red wife and red husband, blue wife and blue husband.
Step 1:-Green couple will go to the hotel side and green husband will return with the boat.(2 moves)
Step 2:-Now red wife and blue wife will go to the hotel side and red wife will return with the boat.(2
moves)
Step 3:-Green husband and blue husband will go to the hotel side and blue couple will return with the
boat.(2 moves)
Step 4:-Red husband and blue husband will go to the hotel side and green wife will return with the
boat.(2 moves)
Step 5:-Red wife and blue wife will go to the hotel side and blue wife will return with the boat.(2
moves)
Step 6:-Finally, blue wife and red wife will go the hotel side.(1 move)
So, there will be a total of 11 steps.
THE BRIDGE PROBLEM
Four people wish to cross a bridge. It is dark, and it is necessary to use a torch
when crossing the bridge, but they have only one torch between them. The
bridge is narrow, and only two people can be on it at any one time. The four
people take different amounts of time to cross the bridge; when two cross
together they proceed at the speed of the slowest. The first person takes 1
minute to cross, the second 2 minutes, the third 5 minutes and the fourth 10
minutes. The torch must be ferried back and forth across the bridge, so that it
is always carried when the bridge is crossed.
Show that all four can cross the bridge within ? minutes.
A person has 3000 bananas and a camel. The person wants to transport the
maximum number of bananas to a destination which is 1000 KMs away,
using only the camel as a mode of transportation. The camel cannot carry
more than 1000 bananas at a time and eats a banana every km it travels.
What is the maximum number of bananas that can be transferred to the
destination using only camel (no other mode of transportation is allowed).
Games: Matchstick Games
Combinatorial games, like matchstick games, are played by moving things around (in this
case, matchsticks) according to a set of rules. Taking out, moving around, or joining
matchsticks in these games is often done in a planned way to either win or stop your
opponent from winning. Understanding winning tactics is important for these games, and
ideas from game theory can help you do that.
1. A Look at Matchstick Games
Nim, which means "subtraction," is the most popular matchstick game. Each player takes
a turn taking matchsticks from a pile. If someone must take the last matchstick, they lose
(or win in some versions). Each player takes away a set number of matchsticks.

The goal of subtraction games like Nim is to make your opponent lose, which is
usually done by changing the number of matchsticks that are left.
2. Tips for Winning Matchstick Games
a) Assumptions

Strategies that work often depend on a few simple assumptions:

 Perfect Information: Both players know exactly what's going on in the game.
 No Randomness: There is no chance in the game; the result is completely
determined by the moves of the players.
 Ideal Play: Both players try to make it as difficult as possible for the other
player to win by playing ideally.
3. Labelling Positions

Labelling positions in matchstick games helps define which positions are winning
and losing. The key concepts are:

 Winning Position: A position where the player to move can force a win no
matter how well the opponent plays.

 Losing Position: A position where every possible move leaves the opponent in
a winning position.
4. Formulating Requirements for Winning Strategies

To develop a winning strategy, you must:

 Identify losing positions.

 Work backwards from losing positions to identify which moves put the opponent in
these losing positions.

 Apply the concept of "forced moves" to continuously push your opponent toward a
losing position.
5. Subtraction-Set Games
A Subtraction-Set Game is a type of game where, on each turn, a player can remove a fixed
number of matchsticks based on a pre-defined set of moves (e.g., remove 1, 2, or 3
matchsticks).

Examples of Strategies in Matchstick Games


Simple Nim Game

Rules: There are 21 matchsticks, and players can remove 1, 2, 3 or 4 matchsticks on their

turn. The player forced to take the last matchstick loses.


For any given subtraction set, the winning and losing positions can always be computed. We exemplify the
process in this section by calculating the winning and losing positions when the allowed moves are:
 remove one match,
 remove three matches,
 remove four matches.
In other words, the subtraction set is {1 , 3 , 4}.
Positions in the game are given by the number of matches in the pile. We refer to the positions using this
number. So, “position 0” means the position in which there are no matches remaining in the pile, “position
1” means the position in which there is just one match in the pile, and so on.
Beginning with position 0, and working through the positions one by one, we identify whether each position is a
winning position using the rules that
 a position is a losing position if every move from it is to a winning position, and
 a position is a winning position if there is a move from it to a losing position.
Questions

1. There is one pile of matches. Each player is allowed to remove 1 match.


What are the winning positions?
2. There is one pile of matches. Each player is not allowed to remove 0 matches but must remove at least 1
match.
What are the winning positions?
3. Can you see a pattern in the last two problems and the example discussed above (in which a player is
allowed to remove 1 or 2 matches)?
In other words, can you see how to win a game in which an allowed move is to remove at least 1 and at
most N matches, where N is some number fixed in advance?
4. There is one pile of matches. Each player is allowed to remove 1, 3 or 4 matches.
What are the winning positions and what is the winning strategy?
Game of Nim
This game depends on two factors:
1.The player who starts first.
2.The initial configuration of the piles/heaps.
We can predict the winner of the game before even playing the game !
Nim-Sum : The cumulative XOR value of the number of matchstick in each piles/heaps at
any point of the game is called Nim-Sum at that point.

“If both A and B play optimally (i.e- they don’t make any mistakes), then the player
starting first is guaranteed to win if the Nim-Sum at the beginning of the game is non-zero.
Otherwise, if the Nim-Sum evaluates to zero, then player A will lose.”
Bitwise XOR binary operation

Bitwise XOR (exclusive or) denoted by ⊕ or (^, in C programming) is a binary operation


that compares two-bit patterns of equal length and performs a logical operation on each
pair of corresponding bits. The result of the operation is:
 1: If only one of the bits is 1
 0: If both bits are 0 or both are 1

Process: Nim-sum=3⊕4⊕3=2.

• First convert the decimal numbers in binary numbers


• Apply the XOR operation among the numbers
• Last, convert the binary number (obtained in
previous step) in decimal number
Grundy Numbers/Numbers and Mex

Grundy Number is a number that defines a state of a game.

Grundy Numbers or Numbers determine how any Impartial Game (not only the Game
of Nim) can be solved once we have calculated the Grundy Numbers associated with
that game using Sprague-Grundy Theorem.

Mex?
‘Mex’ of a set of numbers is the smallest
non-negative number not present in the
set.
How to calculate Grundy Numbers?
The Grundy Number/ number is equal to 0 for a game that is lost immediately by the
first player and is equal to Mex of the numbers of all possible next positions for any
other game.
Example 1
The game starts with a pile of n matchsticks, and the player to move may take any
positive number of matchsticks. Calculate the Grundy Numbers for this game. The
last player to move wins. Which player wins the game?
Example 2
The game starts with a pile of n stones, and the player to move may take any positive
number of stones up to 3 only. The last player to move wins. Which player wins the
game? This game is 1 pile version of Nim.

So, we can define Grundy Number of any n >= 4 recursively as-


Grundy(n) = Mex[Grundy (n-1), Grundy (n-2), Grundy (n-3)]
What is Sprague-Grundy Theorem?

Suppose there is a composite game (more than one sub-game) made up of N sub-games
and two players, A and B. Then Sprague-Grundy Theorem says that if both A and B play
optimally (i.e., they don’t make any mistakes), then the player starting first is guaranteed
to win if the XOR of the Grundy numbers of position in each sub-games at the beginning
of the game is non-zero. Otherwise, if the XOR evaluates to zero, then player A will lose,
no matter what.
How to apply Sprague Grundy
Theorem ?

The basic steps are listed as follows:


1.Break the composite game into sub-games.
2.Then for each sub-game, calculate the Grundy Number at that position.
3.Then calculate the XOR of all the calculated Grundy Numbers.
4.If the XOR value is non-zero, then the player who is going to make the turn (First Player)
will win else he is destined to lose, no matter what.
Example : The game starts with 3 piles having 3, 4 and 5 matchsticks, and the player
to move may take any positive number of matchsticks up to 3 only from any of the
piles [Provided that the pile has that much number of matchsticks]. The last player to
move wins. Which player wins the game if both players play optimally?
How to tell who will win by applying Sprague-Grundy Theorem?
As, we can see that this game is itself composed of several sub-games.
First Step : The sub-games can be considered as each piles.
Second Step : We see from the below table that
Grundy(3) = 3
Grundy(4) = 0
Grundy(5) = 1
Third Step : The XOR of 3, 0, 1 = 2
Fourth Step : Since XOR is a non-zero number, so we can say that the first
player will win.

You might also like