You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
1.0 KiB

7 months ago
  1. \item \lect Complete the following snippet of the python script with the necessary constraint statements.
  2. The script reads a file that represents a \texttt{size\_x} $\times$ \texttt{size\_y} grid, which includes walkable cells denoted by '$\_$'.
  3. Write constraints for the variables \texttt{coords\_x} and \texttt{coords\_y} such that the variables can only take values that are within the boundaries of the grid and can only represent walkable cells.
  4. \begin{pythonSourceCode}
  5. from z3 import *
  6. ...
  7. size_y = len(grid)
  8. size_x = len(grid[0])
  9. coords_x = Int("coords_x")
  10. coords_y = Int("coords_y")
  11. # Enforce that the position is in the grid, use size_x and size_y
  12. solver.add(coords_x >= 0)
  13. solver.add(coords_x < size_x)
  14. solver.add(coords_y >= 0)
  15. solver.add(coords_y < size_y)
  16. # Enforce that the coordinates can only be a valid cell
  17. for i in range(size_y):
  18. for j in range(size_x):
  19. if grid[i][j] != "_":
  20. solver.add(Not(And(coords_y == i, coords_x == j)))
  21. \end{pythonSourceCode}