Problema das Oito Rainhas
Publicado por N 30/01/2006
[ Hits: 13.623 ]
Um enigma para os fãs de xadrez é o problema das Oito Rainhas,
que exige o seguinte: é possível colocar oito rainhas em um
tabuleiro de xadrez vazio de modo que nenhuma esteja 'atacando'
alguma outra (isto é, sem que duas rainhas estejam na mesma linha,
na mesma coluna ou na mesma diagonal)?
#!/usr/bin/env python # -*- coding: utf-8 -*- # # Problema das Oito Rainhas. # Copyright (C) 2006 by Nycholas de Oliveira e Oliveira <nycholas@gmail.com> # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # <Este programa é software livre; você pode redistribuí-lo e/ou modificá-lo sob os # termos da Licença Pública Geral GNU conforme publicada pela Free Software Foundation; # tanto a versão 2 da Licença, como (a seu critério) qualquer versão posterior.> # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # <Este programa é distribuído na expectativa de que seja útil, porém, SEM NENHUMA # GARANTIA; nem mesmo a garantia implícita de COMERCIABILIDADE OU ADEQUAÇÃO A UMA # FINALIDADE ESPECÍFICA. Consulte a Licença Pública Geral do GNU para mais detalhes.> # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # <Você deve ter recebido uma cópia da Licença Pública Geral do GNU junto com este # programa; se não, escreva para a Free Software Foundation, Inc., no endereço 59 # Temple Street, Suite 330, Boston, MA 02111-1307 USA.> # ######################################################### # NOME : Nycholas de Oliveira e Oliveira # # E-MAIL : nycholas@gmail.com # # ICQ : 114965471 # # MSN : o_lalertom@hotmail.com # # JABBER : nycholas@jabber.org # # TALK : nycholas@gmail.com # # DESCRICAO : Problema das Oito Rainhas # # LOCALIZACAO : Uberlandia - MG # # LOCALIZACAO : Brasil # ######################################################### class OitoRainhas: def __init__(self): print self.printInit() self.setVars() self.loop() def printInit(self): p = \ " + Problema das Oito Rainhas\n" \ " * Nycholas de Oliveira e Oliveira - o_lalertom - nycholas@gmail.com\n\n" \ ">> Para melhor visualizar o conteúdo impresso em tela, o terminal deve esta setado com a coluna em 42.\n" return p def printRainha(self): p = \ "\n==========================================\n" \ " + " + str(len(self.posRainha)) + " Rainha\n\n" \ + str(self.t) return p def setVars(self): self.TABULEIRO = \ "########\n" \ "########\n" \ "########\n" \ "########\n" \ "########\n" \ "########\n" \ "########\n" \ "########\n" self.VAZIO, self.ATAQUE, self.RAINHA = "#", "+", "*" self.posRainha, self.posAtaque, self.posInvalida, self.posValida = [], [], [], [] self.t = [list(line) for line in self.TABULEIRO.splitlines()] self.posValida = self.setPosValida() def setPosValida(self): l = [] for y in xrange(len(self.t)): for x in xrange(len(self.t)): l.append([x,y]) return l def setVazio(self, x, y): self.t[x][y] = self.VAZIO def setRainha(self, x=0, y=0): if 0 <= x <= 7 and 0 <= y <= 7: if self.t[x][y] == self.VAZIO and self.testPos(x, y) == True: self.t[x][y] = self.RAINHA self.posRainha.append([x, y]) self.posInvalida.append([x, y]) self.posValida.remove([x, y]) if self.moveX(x, y) or self.moveY(x, y) or self.moveXYDir(x, y) or self.moveXYEsq(x, y): pass print self.printRainha() def setAtaque(self, x, y): self.t[x][y] = self.ATAQUE self.posAtaque.append([x, y]) self.posInvalida.append([x, y]) self.posValida.remove([x, y]) def moveX(self, x=0, y=0): if 0 <= x <= 7: if self.t[x][y] == self.VAZIO: self.setAtaque(x, y) if self.moveX(x+1, y) or self.moveX(x-1, y): pass elif self.t[x][y] == self.RAINHA: if self.moveX(x+1, y) or self.moveX(x-1, y): pass else: if self.X < 7: self.X += 1 if self.moveX(x+self.X, y) or self.moveX(x-self.X, y): pass def moveY(self, x=0, y=0): if 0 <= y <= 7: if self.t[x][y] == self.VAZIO: self.setAtaque(x, y) if self.moveY(x, y+1) or self.moveY(x, y-1): pass elif self.t[x][y] == self.RAINHA: if self.moveY(x, y+1) or self.moveY(x, y-1): pass else: if self.Y < 7: self.Y += 1 if self.moveY(x, y+self.Y) or self.moveY(x, y-self.Y): pass def moveXYDir(self, x=0, y=0): if 0 <= x <= 7 and 0 <= y <= 7: if self.t[x][y] == self.VAZIO: self.setAtaque(x, y) if self.moveXYDir(x+1, y+1) or self.moveXYDir(x-1, y-1): pass elif self.t[x][y] == self.RAINHA: if self.moveXYDir(x+1, y+1) or self.moveXYDir(x-1, y-1): pass else: if self.XYD < 7: self.XYD += 1 if self.moveXYDir(x+self.XYD, y+self.XYD) or self.moveXYDir(x-self.XYD, y-self.XYD): pass def moveXYEsq(self, x=0, y=0): if 0 <= x <= 7 and 0 <= y <= 7: if self.t[x][y] == self.VAZIO: self.setAtaque(x, y) if self.moveXYEsq(x+1, y-1) or self.moveXYEsq(x-1, y+1): pass elif self.t[x][y] == self.RAINHA: if self.moveXYEsq(x+1, y-1) or self.moveXYEsq(x-1, y+1): pass else: if self.XYE < 7: self.XYE += 1 if self.moveXYEsq(x+self.XYE, y-self.XYE) or self.moveXYEsq(x-self.XYE, y+self.XYE): pass def testPos(self, x, y): r = False for i in self.posValida: if x == i[0] and y == i[1]: r = True return r def loop(self): for i in self.posValida: self.X, self.Y, self.XYD, self.XYE = 0, 0, 0, 0 self.setRainha(i[0], i[1]) if len(self.posRainha) < 8: self.loop() if __name__ == "__main__": oitoRainhas = OitoRainhas()
Script para obter um wallpaper de como está o globo em tempo real
Combinações de letras, números e muito mais
Atenção a quem posta conteúdo de dicas, scripts e tal (2)
Manutenção de sistemas Linux Debian e derivados com apt-get, apt, aptitude e dpkg
Melhorando o tempo de boot do Fedora e outras distribuições
Como instalar as extensões Dash To Dock e Hide Top Bar no Gnome 45/46
Como Atualizar Fedora 39 para 40
Instalar Google Chrome no Debian e derivados
Consertando o erro do Sushi e Wayland no Opensuse Leap 15
Instalar a última versão do PostgreSQL no Lunix mantendo atualizado
Flathub na sua distribuição Linux e comandos básicos de gerenciamento
Cant ´t attach process a10c: error 0 Wine fecha (3)
Dongle Bluetooth 5.0 não funciona no Pop Os 22.04 (4)
Acesso a internet servidor virtualizado (10)
Sistema da Coréia do Norte - Red Star OS (11)
redirecionando saida de comando touch para o AWK[AJUDA] (17)