Problema das Oito Rainhas
Publicado por N 30/01/2006
[ Hits: 10.202 ]
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 <[email protected]> # # 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 : [email protected] # # ICQ : 114965471 # # MSN : [email protected] # # JABBER : [email protected] # # TALK : [email protected] # # 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 - [email protected]\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()
Dicionário Ingles de expressões.
Slackware para Desktop ou Notebook em 2021
Asterisk - Configurando Ramais, Plano de Discagem e URA Simples
Executando Arquivo de Mídia .opus pelo Terminal
Usando AWX no Fedora CoreOS com K3s
Instalando um localizador de aplicações avançado no seu sistema
Zorin OS 16 Beta - Acho que você vai querer usar! (3)
KDE Plasma é a melhor interface do Linux? 5 motivos que fazem eu achar... (39)
O projeto openSUSE decidiu seguir o exemplo da Red Hat e retirou seu p... (0)
Dual boot do Windows 10 com o Mint 20.1 (Ulyssa) - Não é possível inic... (0)
[Tcl/Tk] Mostrar conexões de rede ativas
[Shell Script] Bashblog v3.0 - cria um microblog em HTML5
[Shell Script] Manutenção e limpeza do Linux
[Shell Script] ebook-cli - gerencia livros digitais entre PC e leitores ebooks
[Shell Script] AptList v1.1 - instalação de pacotes DEB a partir de uma lista