Cavaliers de Euler - Algo - Programmation
Marsh Posté le 13-05-2004 à 19:50:58
je te la raconte l'histoire du vieux chinois qui demande comme récompense à l'empereur des grains de riz sur un échiquier : 1 sur la première case, 2 sur la deuxième, 4 sur la troisième, etc
Marsh Posté le 14-05-2004 à 08:22:24
Merci de ton aide, j'ai réussi a corriger mon code grâce à tes précieux conseils
/********************************************************
* Titre : Cavaliers *
* Date : 13.05.04 *
* Auteur : Gilles Walther *
* Commentaire : Cavaliers de Euler *
*********************************************************/
import javax.swing.* ;
class Cavaliers
{
static int nbSolutions = 0;
static int coup = 1 ;
static int[][] echiquier ;
static int taille ;
static int[] deltaX = {-2, -1, 1, 2, 2, 1, -1, -2} ;
static int[] deltaY = {1, 2, 2, 1, -1, -2, -2, -1} ;
static int max = 0 ;
public static void main(String[] args)
{
int a = Integer.parseInt(JOptionPane.showInputDialog("Taille de l'échiquier :" )) ;
Cavaliers c = new Cavaliers(a) ;
for(int i=2;i<taille+2;i++)
{
for(int j=2;j<taille+2;j++)
{
c.placeCavalier(i, j, coup) ;
}
}
}
public Cavaliers(int n)
{
taille = n ;
int constr = n+4 ;
echiquier = new int[constr][constr] ;
for(int i=0;i<constr;i++)
{
for(int j=0;j<constr;j++)
{
echiquier[i][j]=(-1) ;
}
}
for(int i=2;i<constr-2;i++)
{
for(int j=2;j<constr-2;j++)
{
echiquier[i][j]=(0) ;
}
}
}
public void placeCavalier(int x, int y, int coup)
{
if((x >= 2) && (x <= taille+2) && (y >= 2) && (y <= taille+2) && (echiquier[x][y] == (0)))
{
echiquier[x][y]=coup ;
coup++ ;
if(coup>taille*taille)
{
affiche() ;
}
else
{
for(int i=0;i<8;i++)
{
placeCavalier((x+deltaX[i]), (y+deltaY[i]), coup) ;
}
}
echiquier[x][y]=0 ;
}
}
static void affiche()
{
System.out.println("Solution "+ (++nbSolutions));
for (int i=2; i<taille+2; i++)
{
for (int j=2; j<taille+2; j++) System.out.print("\t" +echiquier[i][j]) ;
System.out.println("" );
}
System.out.println("--------------------------" );
}
}
Marsh Posté le 14-05-2004 à 08:27:15
tout en static ?
et j'ai du mal à comprendre tes deux boucles d'initialisation, l'une écrase en grande partie l'autre...
Marsh Posté le 14-05-2004 à 08:37:29
Taz a écrit : je te la raconte l'histoire du vieux chinois qui demande comme récompense à l'empereur des grains de riz sur un échiquier : 1 sur la première case, 2 sur la deuxième, 4 sur la troisième, etc |
C'est pas en Chine... c'est en Inde
Marsh Posté le 13-05-2004 à 19:47:33
Hello, j'ai un prob avec mon programme ca me fait un stackOverFLow je comprend pas pourquoi.
Remarque à la ligne 68 si je met ==0 ca me fait un outofbound si je met =!-1 ca me fait un stack overflow....
Si qqun peut m'aider merci
import javax.swing.* ;
class Cavaliers
{
static int tailleEchiquier ;
static int compteur = 1 ;
static int[][] echiquier ;
static int[] deplacementAbcisse = {0, 1, 2, 2, 1, -1, -2, -2, -1} ;
static int[] deplacementOrdonnee = {0, 2, 1, -1, -2, -2, -1, 1, 2} ;
static int posX = 2 ;
static int posY = 2 ;
public static void main(String[] args)
{
int n = Integer.parseInt(JOptionPane.showInputDialog("Veuillez entrer la taille de l'échiquier" )) ;
Cavaliers c = new Cavaliers(n) ;
c.placeCavalier(posX, posY) ;
c.affiche() ;
//placeCavalierFull() ;
}
public Cavaliers(int n)
{
tailleEchiquier = n ;
int t = tailleEchiquier + 4 ;
echiquier = new int[t][t] ;
for(int m=0; m<=t-1;m++)
{
for(int l=0;l<=t-1;l++)
{
echiquier[m][l]=-1 ;
}
}
for(int a=2;a<=t-2;a++)
{
for(int b=2;b<=t-2;b++)
{
echiquier[a][b]=0 ;
}
}
}
public void placeCavalierFull(int[][] tab)
{
for(int j=2;j<=tailleEchiquier+1;j++)
{
for(int i=2;i<=tailleEchiquier+1;i++)
{
placeCavalier(i, j) ;
}
}
}
public void placeCavalier(int x, int y)
{
for(int i=0; i<=8;i++)
{
if(echiquier[deplacementAbcisse[i]+x][deplacementOrdonnee[i]+y]!=(-1))
{
echiquier[deplacementAbcisse[i]+x][deplacementOrdonnee[i]+y]=compteur ;
compteur++ ;
posX = posX+deplacementAbcisse[i] ;
posY = posY+deplacementOrdonnee[i] ;
placeCavalier(posX, posY) ;
}
}
}
public void affiche()
{
for(int i=2;i<=tailleEchiquier+2;i++)
{
for(int j=2;j<=tailleEchiquier+2;j++)
{
System.out.println(echiquier[i][j]) ;
}
}
}
}