0% found this document useful (0 votes)
8 views8 pages

Exp - No: Date: Vacuum Cleaner Problem

Vaccum cleaner problem

Uploaded by

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

Exp - No: Date: Vacuum Cleaner Problem

Vaccum cleaner problem

Uploaded by

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

EXP.

NO: DATE:
VACUUM CLEANER PROBLEM

AIM:

ALGORITHM:
OUTPUT:
PROGRAM:
class VacuumCleaner:
def __init__(self, grid):
self.grid = grid
self.x = 0
self.y = 0
self.rows = len(grid)
self.cols = len(grid[0])
self.directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] # Right, Down, Left, Up

def print_grid(self):
for row in self.grid:
print(' '.join(map(str, row)))
print()

def move_right(self):
if self.y < self.cols - 1:
self.y += 1
else:
self.move_down()

def move_down(self):
if self.x < self.rows - 1:
self.x += 1
self.y = 0
else:
self.x = 0
self.y = 0

def clean(self):
if self.grid[self.x][self.y] == 1:
self.grid[self.x][self.y] = 0

def is_done(self):
return all(cell == 0 for row in self.grid for cell in row)

def main():
# User input for grid dimensions
rows = int(input("Enter number of rows: "))
cols = int(input("Enter number of columns: "))

# Initialize grid with user input


grid = []
for i in range(rows):
row = list(map(int, input(f"Enter row {i + 1} (space-separated 0s and 1s):
").strip().split()))
if len(row) != cols:
print("Error: The number of columns does not match.")
return
grid.append(row)

vacuum = VacuumCleaner(grid)
print("Initial Grid:")
vacuum.print_grid()

while not vacuum.is_done():


vacuum.clean()
vacuum.print_grid()
vacuum.move_right()

print("Cleaning completed!")
vacuum.print_grid()

if __name__ == "__main__":
main()

RESULT:

You might also like