Problema das Oito Rainhas
Publicado por N 30/01/2006
[ Hits: 14.539 ]
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()
Problema das Oito Rainhas (Random)
O maior quebra cabeças de sempre
Faça suas próprias atualizações de pacotes/programas no Void Linux e torne-se um Contribuidor
Como rodar o Folding@home no Linux
Criando um painel de controle (Dashboard) para seu servidor com o Homepage
O Abismo entre o Código e o Chão: Saltos Tecnológicos e a Exclusão Estrutural no Brasil
Instalar e Configurar a santíssima trindade (PAP) no Void Linux
Pisando no acelerador do Linux Mint: Kernel XanMod, zRAM e Ajustes de Swap
Como compilar kernel no Linux Mint
Lançamento do Brutal DOOM test 6
Consertando o erro no Brave de webgl
Solução para ter de volta as bordas e barra de títulos das janelas em zenity no Debian 13.x
Seno, Coseno, Tangente em CLIPPER (0)
Inserir uma URL num arquvo pelo Ubuntu (CLIPPER) (0)
VMWare Player não conecta na rede nem consigo intercambiar arquivos (1)









