{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "ename": "IndentationError", "evalue": "expected an indented block (, line 11)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m11\u001b[0m\n\u001b[0;31m import copy\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" ] } ], "source": [ "# Return True if it is legal to place a queen\n", "# on board at position (row, col)\n", "def is_legal(board, row, col):\n", " \n", " # Return false if a queen at (row, col) \n", " # can attack any of the other queens on board\n", " \n", " # Otherwise, return True\n", " \n", "# Try to place a queen on board in specified row\n", "import copy\n", "def eight_queens(board, row):\n", "\n", " # If a solution is found:\n", " global count # global variable count to store number of solutions\n", " count = count + 1 # increment count\n", " \n", " # Otherwise, try to place a queen at every col in specified row\n", " # Make a recursive call for the legal ones\n", " # Pass copy of board to recursive call\n", " new_board = copy.deepcopy(board)\n", " # update new_board with new queen\n", " eight_queens(new_board, row + 1)\n", " \n", "# Initialize all the variables and start the search\n", "count = 0\n", "chessboard = [[False] * 8 for i in range(8)]\n", "eight_queens(chessboard, 0)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "[True, False, False, False, False, False, False, False]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[False, False, True, False, False, False, False, False]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[False, False, False, False, False, False, False, False]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[False, False, False, False, False, False, False, False]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[False, False, False, False, False, False, False, False]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[False, False, False, False, False, False, False, False]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[False, False, False, False, False, False, False, False]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[False, False, False, False, False, False, False, False]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#Just an example of what our data structure looks like\n", "chessboard = [[False] * 8 for i in range(8)]\n", "chessboard[0][0] = True\n", "chessboard[1][2] = True\n", "for row in chessboard:\n", " show(row)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "SageMath 8.3", "language": "", "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.15" } }, "nbformat": 4, "nbformat_minor": 2 }