Algo de backtracking

marianne31 Messages postés 68 Statut Membre -  
 EulerDZ -
Bonjour,

est ce que quelqu'un aurait un exemple d'algorithme de backtracking en Java ?

Merci

Marianne

3 réponses

  1. infrasound
     
    Backtrack sur a2b3 : quelles sont les différentes manières de placer 2 'a' et 3 'b' :

    import java.util.*;
    
    class Backtracka2b3{
    	
    	public static void resolution(int maxA, int maxB){
    		Stack<Character> pile = new Stack<Character>();
    		int nbA = 0;
    		int nbB = 0;
    		int position = 0;
    		int nbSolution = 0;
    		char valeurCourante = premValPossible(nbA,nbB,maxA,maxB);
    		
    		while(estDansGrille(position)){
    			while(valeurCourante!=' '){
    				if(valeurCourante=='a') nbA++;
    				if(valeurCourante=='b') nbB++;
    				pile.push(valeurCourante);
    				
    				if(estSolution(nbA,nbB,maxA,maxB)){
    					System.out.println(pile);
    					nbSolution++;
    					valeurCourante = valSuivantePossible(valeurCourante,nbB,maxB);
    					char c = pile.pop();
    					if (c=='a') nbA--;
    					if (c=='b') nbB--;
    				}else{
    					position = posSuivante(position);
    					valeurCourante = premValPossible(nbA,nbB,maxA,maxB);
    				}
    			}
    			position = posPrecedente(position);
    			if(!pile.empty()){
    				char c = pile.pop();
    				if (c=='a') nbA--;
    				if (c=='b') nbB--;
    				valeurCourante = valSuivantePossible(c,nbB,maxB);
    				
    			}
    		}
    		System.out.println("Nb sol = "+nbSolution);
    	}
    	
    	
    	public static char premValPossible(int nbA, int nbB, int maxA, int maxB){
    		if(nbA<maxA){
    			return 'a';
    		}
    		else if(nbB<maxB){
    			return 'b';
    		}
    		else return ' ';
    	}
    	
    	public static boolean estSolution(int nbA, int nbB, int maxA, int maxB){
    		return (nbA==maxA && nbB==maxB);
    	}
    	
    	public static int posSuivante(int pos){
    		return pos+1;
    	}
    	
    	public static int posPrecedente(int pos){
    		return pos-1;
    	}
    	
    	public static char valSuivantePossible(char val, int nbB, int maxB){
    		if(val=='a'){
    			if(nbB<maxB){
    				return 'b';
    			}
    			else return ' ';
    		}else{
    			return ' ';
    		}
    	}
    	
    	public static boolean estDansGrille(int pos){
    		return pos>=0;
    	}
    	
    	public static void main(String[] args){
    		
    		int maxA = 2;
    		int maxB = 3;
    		resolution(maxA,maxB);
    	}
    		
    	
    }
    
    


    On a :

    [a, a, b, b, b]
    [a, b, a, b, b]
    [a, b, b, a, b]
    [a, b, b, b, a]
    [b, a, a, b, b]
    [b, a, b, a, b]
    [b, a, b, b, a]
    [b, b, a, a, b]
    [b, b, a, b, a]
    [b, b, b, a, a]
    Nb sol = 10
    6
    1. EulerDZ
       
      dans votre exemple la récursivité est beaucoup mieux et voila le code en c#

      using System;

      class Program
      {
      static int nbB = 2, nbA = 3;

      static void Main(string[] args)
      {
      f("", 'a', 0, 0);
      f("", 'b', 0, 0);
      }

      static void f(string st, char ch, int a, int b)
      {
      st += ch;
      if (ch == 'a') a++;
      else b++;
      if (a == nbA && b == nbB)
      Console.WriteLine(st);
      else
      {
      if (a < nbA) f(st, 'a', a, b);
      if (b < nbB) f(st, 'b', a, b);
      }
      }
      }
      0
  2. badr
     
    Moi aussi je cherche cette algorithme, si tu l'as marianne diffuser le svp
    0