Docsity

Lab report system and Python, Schemes and Mind Maps of Computer Communication Systems

Partial preview of the text

Download Lab report system and Python and more Schemes and Mind Maps Computer Communication Systems in PDF only on Docsity!

Lab 5

Problem Solving

Artificial Intelligence

Water Jug Problem

You are given two jugs, a 4-gallon one and a 3- 3 - gallon one, a pump that has unlimited water that you can use to fill the jug, and the ground on which water may be poured. Neither jug has any measuring markings on it. How can you get exactly 2 gallons of water in the 4-gallon jug? To solve this, we must make some assumptions not mentioned in the problem. They are: ● We can fill a jug from the pump. ● We can pour water out of a jug to the ground. ● We can pour water from one jug to another. ● There is no measuring device available. Operators — we must define a set of operators that will take us from one state to another.

8 - Queens Problem

Explanation: The eight queens’ problem is a problem in which we figure out a way to put 8 queens on an 8× chessboard in such a way that no queen should attack the other. For basic info about the queen in a chess game, you should know that a queen can move in any direction (vertically, horizontally, and diagonally) and to any number of places. In the figure below you can see how to place 4 queens on a 4×4 chessboard. Similarly, we have to place 8 queens on an 8×8 chessboard. We will use backtracking to solve this interesting problem(puzzle). Backtracking Backtracking the solution of the problem depends on the previous steps taken. We take a step and then analyze it that whether it will give the correct answer or not? and if not, then we move back and change the previous step. Pseudocode START

  1. begin from the leftmost column
  2. if all the queens are placed, return true/ print configuration
  3. check for all rows in the current column a. if queen placed safely, mark row and column; and recursively check if we approach in the current configuration, do we obtain a solution or not b. if placing yields a solution, return true c. if placing does not yield a solution, unmark and try other rows
  4. if all rows tried and solution not obtained, return false and backtrack END Task: Write Python code to implement eight queens’ problem.

Output: