system.exit(0)を使用せずにこのバックトラッキングを停止するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/6325768

  •  27-10-2019
  •  | 
  •  

質問

static void LasVegas(int []tablero, int f, int ultimaReina){


    HashSet<Integer> enterosUsados = new HashSet<Integer>();


    if (ultimaReina!=-1) enterosUsados.add(ultimaReina);
    if ((ultimaReina-1) >=0){enterosUsados.add(ultimaReina-1);}
    if ((ultimaReina+1 != tablero.length) && (ultimaReina!=-1)){enterosUsados.add(ultimaReina+1);}
  //  if(ultimaReina+1!=tablero.length){enterosUsados.add(ultimaReina+1);}

    Random random = new Random();
        int posReina;

        if (f==tablero.length){
            printBoard(tablero);
            stop=System.currentTimeMillis();
            System.out.println(stop-start);
            System.exit(0);
            return;
        }

        do {

            do{
            posReina= Math.abs(random.nextInt())%tablero.length;
            }
            while(enterosUsados.add(posReina)==false);


            tablero[f]=posReina;

            if (check(tablero, f)){
                LasVegas(tablero, f+1, posReina);
            }



    } while (enterosUsados.size()<tablero.length);

  }

public static void main(String[] args) {

       // testChiCuadrado(410,30);

        int [] tablero = new int[8];
        Arrays.fill(tablero, -1);

        start = System.currentTimeMillis();
        LasVegas(tablero, 0, -1);


    }

static boolean  check (int [] array, int f){

       for (int i=0; i<f; i++){

       if (array[i]==array[f]) return false;

       if( Math.abs(array[f]-array[i])== Math.abs(f-i)) return false;


       } return true;



   }


   static void printBoard(int [] tablero) {

       char [] linea = new char[tablero.length];
       Arrays.fill(linea, '*');
       for (int i=0;i<tablero.length;i++){

           linea[tablero[i]]='D';
           System.out.println(new String(linea));
           linea[tablero[i]]='*';

       }

   }

ラスベガスアルゴリズムを使用してボード上でランダムなクイーンポジションを生成します。複数の実行を介してタイミングを実行したいのですが、使用しています System.exit(0) ソリューションが見つかったときにバックトラッキングを停止するために、そこで止まらない場合、アルゴリズムは他のソリューションを提供しますが、それは望ましくありません。

ここ:

       if (f==tablero.length){
            printBoard(tablero);
            stop=System.currentTimeMillis();
            System.out.println(stop-start);
            System.exit(0);
            return;
        } 

どうすればそれを変更してアルゴリズムを止めることができますか System.exit(0) ループ内で複数回呼び出すことができますか?

役に立ちましたか?

解決

戻りタイプを変更します LasVegasboolean. 。通話を削除します System.exit() そして、すぐに変更します return; 声明 return true;. 。再帰呼び出しを変更します。

if (LasVegas(tablero, f+1, posReina)) return true;

他のヒント

function returnブールを作成できます

static bool LasVegas( ...

の代わりに Exit 戻る false. 。戻る true 他のケースでは。

また、関数を再帰的に呼び出す場合、結果を確認するだけで、falseの場合はfalseを返します。

       if (check(tablero, f)){
            if (!LasVegas(tablero, f+1, posReina))
                return false;
       }

出口を返品に置き換えます。
しばらくすると、ブレークを呼び出すことができます。それを終了します

ここに良い提案があります:

バックトラッキングが完了していて、続行したくない場合。

  1. 静的またはダミー変数(booleanまたはint)を作成する
  2. 完了したら、ある程度の価値を割り当てます(trueまたは1としましょう)
  3. BackTrackメソッドのダミー変数値と「return」を確認してください。

if(done)return; //ここで、すべてのバックトラックメソッドは、単に呼び出されたものにtrueを返し、何もしません。重要なことは、このコードがバックトラッキング方法の先頭にあることです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top