enres of the numbers ram which allocates the integers 1-8 to the squares in the figure abc tions that no two adjacent squares contain consecutive int E, we mean vertically, horizontally, or diagonally tive, we mean two numbers whose difference is 1.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Another example:
Eight Numbers in a Cross
serad the
Write a program which allocates the integers 1-8 to the squares in the figure above, subje
to the restrictions that no two adjacent squares contain consecutive integers.
2
By adjacent, we mean vertically, horizontally, or diagonally
By consecutive, we mean two numbers whose difference is 1.
Try it.
B
Transcribed Image Text:Another example: Eight Numbers in a Cross serad the Write a program which allocates the integers 1-8 to the squares in the figure above, subje to the restrictions that no two adjacent squares contain consecutive integers. 2 By adjacent, we mean vertically, horizontally, or diagonally By consecutive, we mean two numbers whose difference is 1. Try it. B
Expert Solution
Step 1: Step

def is_valid(board, row, col, num):

    # Check if the number is already placed in the same row or column

    for i in range(8):

        if board[row][i] == num or board[i][col] == num:

            return False


    # Check diagonals

    directions = [(-1, -1), (-1, 1), (1, -1), (1, 1)]

    for dr, dc in directions:

        r, c = row, col

        while 0 <= r < 8 and 0 <= c < 8:

            if board[r][c] == num:

                return False

            r += dr

            c += dc


    return True


def solve_puzzle(board, row, col):

    if row == 8:

        # All squares are filled, a valid solution is found

        return True


    next_row, next_col = (row, col + 1) if col < 7 else (row + 1, 0)


    if board[row][col] != 0:

        # This square is already filled, move to the next square

        return solve_puzzle(board, next_row, next_col)


    for num in range(1, 9):

        if is_valid(board, row, col, num):

            board[row][col] = num


            if solve_puzzle(board, next_row, next_col):

                return True


            # If placing num doesn't lead to a solution, backtrack

            board[row][col] = 0


    return False


def print_board(board):

    for row in board:

        print(' '.join(str(num) if num != 0 else '.' for num in row))


def eight_numbers_in_cross():

    board = [[0] * 8 for _ in range(8)]


    # Start solving from the center square (3, 3)

    solve_puzzle(board, 3, 3)

    

    print("Solution:")

    print_board(board)


i

f __name__ == "__main__":

    eight_numbers_in_cross()

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Topological Sort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education