This site requires JavaScript, please enable it in your browser!
Greenfoot back
Alexlazea
Alexlazea wrote ...

2017/2/22

backtracking

Alexlazea Alexlazea

2017/2/22

#
so i'm trying to animate some backtracking for school the program should combine 3 baloons in all the posibilities whithout repetitions ( ex: 2 red baloons are not allowed to be in the same combination) The combinations are retained in a vector (st) and the baloons images should change depending on the st vector i tried this, but it doesn't work
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class baloon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class baloon extends Actor
{
    /**
     * Act - do whatever the baloon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int st[],n,k;
    private boolean as, ev;
    private String[] a={"","baloon1.png","baloon2.png","baloon3.png"};
    public void act() 
    {
        main();
    } 

    int main()
    {
        n=3;
        k=1;
        init(k,st);

        while(k>0)
        {
            do
            {
                succesor(k,st,as);
                if(as) validare(k,st,ev);

            } while(as&&!ev);
            if(as)
                if(solutie(k))
                    tipar();
                else
                {
                    k++;
                    init(k,st);
                }
            else k--;

        }
        return 0;
    }

    void init(int k, int st[])
    {
        st[k]=0;
    }

    void succesor(int k, int st[],boolean as)
    {
        if(st[k]<3)
        {
            st[k]++;
            as=true;
        } else
            as=false;
    }

    void validare (int k, int st[], boolean ev)
    {
        int i;
        ev=true;
        for(i=1;i<=k-1;i++)
            if(st[k]==st[i])
                ev=false;
    }

    boolean solutie(int k)
    {
        if(k==n) return true;
        else return false;
    }

    void tipar()
    {
        getWorld().getObjects(baloon1.class);
        setImage(a[st[1]]);
        Greenfoot.delay(199);
        getWorld().getObjects(baloon1.class);
        setImage(a[st[2]]);
        Greenfoot.delay(199);
        getWorld().getObjects(baloon1.class);
        setImage(a[st[3]]);
        Greenfoot.delay(199);
    }

}
danpost danpost

2017/2/22

#
With three balloons of three different colors, there are only 6 possible combinations. -- ABC, ACB, BAC, BCA, CAB and CBA. I do not know why you do not just use a for loop and alternate switching the first two and second two as demonstrated with the following code:
int[] st = { 1, 2, 3 };
for (int i=0; i<6; i++)
{
    int base = i%2;
    int hold = st[base];
    st[base] = st[base+1];
    st[base+1] = hold;
    System.out.println(""+st[0]+" "+st[1]+" "+st[2]);
}
Alexlazea Alexlazea

2017/2/23

#
because it will get way more complicate in the future and the posibilities will be more than just 6 And i really want to do it this way :D
You need to login to post a reply.