main.java

来自「puzzle program witch dfs bsf and a* algo」· Java 代码 · 共 524 行 · 第 1/2 页

JAVA
524
字号
            }            if (temp.gora != null)            {                if (funkcja==1)                    temp.gora.ocena=funOceniajaca1(temp.gora);                else                    temp.gora.ocena=funOceniajaca2(temp.gora);                agwiazdka.open.add(temp.gora);                agwiazdka.closed.add(temp.gora);            }            if (temp.lewo != null)            {                if (funkcja==1)                    temp.lewo.ocena=funOceniajaca1(temp.lewo);                else                    temp.lewo.ocena=funOceniajaca2(temp.lewo);                agwiazdka.open.add(temp.lewo);                agwiazdka.closed.add(temp.lewo);            }            if (temp.prawo != null)            {               if (funkcja==1)                   temp.prawo.ocena=funOceniajaca1(temp.prawo);               else                   temp.prawo.ocena=funOceniajaca2(temp.prawo);               agwiazdka.open.add(temp.prawo);               agwiazdka.closed.add(temp.prawo);            }            licznik++;                  }         for (int i=0; i<agwiazdka.closed.size(); i++)         {            agwiazdka.closed.get(i).ocena=0;         }         agwiazdka.closed.clear();         return wygrany;    }        public static void main(String[] args) {        // TODO code application logic here                 zero[0][0] = 1;         zero[0][1] = 2;         zero[0][2] = 3;         zero[1][0] = 4;         zero[1][1] = 5;         zero[1][2] = 6;         zero[2][0] = 7;         zero[2][1] = 8;         zero[2][2] = 0;                  Wezel wezelZero = new Wezel(zero);         WypiszPuzzle(wezelZero);         BufferedReader br = new BufferedReader( new InputStreamReader(System.in));         int wybor=0, w=0;         System.out.print("Czy komputer ma sam wygenerowac puzzle?(t/n): ");         try {		wybor = br.read();                w = br.read();            } catch (IOException e) {                System.out.println("Nic nie wybrales !");            }                  //Mieszanie         if ((char)wybor=='t')         {         Random rand = new Random();                  for (int i = 0; i < 1000; i++)         {             int x0 = 0, y0 = 0;             //znajdz klocek pusty             for (int k = 0; k<3; k++)                for (int l = 0; l<3; l++)                    if (wezelZero.stan[k][l] == 0)                    {                        x0 = k;                        y0 = l;                    }                                 int kierunek = rand.nextInt(4) + 1;             switch (kierunek)             {                 case 1: {                     //gora                     if (x0 - 1 >= 0)                     {                         int temp = wezelZero.stan[x0][y0];                         wezelZero.stan[x0][y0] = wezelZero.stan[x0 - 1][y0];                         wezelZero.stan[x0 - 1][y0] = temp;                     }                     break;                 }                 case 2: {                      //dol                     if (x0 + 1 <= 2)                     {                         int temp = wezelZero.stan[x0][y0];                         wezelZero.stan[x0][y0] = wezelZero.stan[x0 + 1][y0];                         wezelZero.stan[x0 + 1][y0] = temp;                     }                     break;                                      }                 case 3: {                      //lewo                     if (y0 - 1 >= 0)                     {                         int temp = wezelZero.stan[x0][y0];                         wezelZero.stan[x0][y0] = wezelZero.stan[x0][y0 - 1];                         wezelZero.stan[x0][y0 - 1] = temp;                     }                     break;                                      }                 case 4: {                      //prawo                     if (y0 + 1 <= 2)                     {                         int temp = wezelZero.stan[x0][y0];                         wezelZero.stan[x0][y0] = wezelZero.stan[x0][y0 + 1];                         wezelZero.stan[x0][y0 + 1] = temp;                     }                     break;                                      }                                               }        }        }        else        {            System.out.println("Podaj puzzle: ");            for (int i=0; i<3; i++)            {                for (int j=0; j<3; j++)                {                    try                     {                        wezelZero.stan[i][j] = br.read()-48;                        w = br.read();                    }                     catch (IOException e) {}                }            }        }        System.out.println();        WypiszPuzzle(wezelZero);        System.out.println();        //Wezel zero=new Wezel(stanZero);        // wezelZero.dol=zero;         long start=System.currentTimeMillis();         wezelZero=BudujDrzewo(wezelZero);         long stop=System.currentTimeMillis();         System.out.println("Zbudowano drzewo w "+licznik+" iteracjach po "+(stop-start)+" milisekundach.");                  //Przeszukiwanie wszerz                  licznik=0;         System.out.println("Rozpoczynam przeszukiwanie wszerz");         start=System.currentTimeMillis();         Drzewo wszerz = new Drzewo();         wszerz.open.add(wezelZero);         Wezel wygrany1=null;         while(!wszerz.open.isEmpty())         {            //wszerz.closed.add(wszerz.open.get(0));            Wezel temp=wszerz.open.get(0);            wszerz.open.remove(0);                        if (Wygrana(temp))            {                                wygrany1 = temp;                break;            }                           if (temp.dol != null)                wszerz.open.add(temp.dol);            if (temp.gora != null)                wszerz.open.add(temp.gora);            if (temp.lewo != null)                wszerz.open.add(temp.lewo);            if (temp.prawo != null)                wszerz.open.add(temp.prawo);            licznik++;                  }         stop=System.currentTimeMillis();         if (wygrany1 == null) System.out.print("Nie znaleziono rozwiazania WSZERZ.\n");         else          {            System.out.print("Znaleziono rozwiazanie w szerz po przeszukaniu "+licznik+" stanow po "+(stop-start)+" milisekundach.\n");            WypiszPuzzle(wygrany1);         }                  //Przeszukiwanie w glab                  licznik=0;         System.out.println("Rozpoczynam przeszukiwanie wglab");         start=System.currentTimeMillis();         Drzewo wglab = new Drzewo();         wglab.open.add(wezelZero);         Wezel wygrany2=null;         while(!wglab.open.isEmpty())         {            //wszerz.closed.add(wszerz.open.get(wglab.open.size()-1));            Wezel temp=wglab.open.get(wglab.open.size()-1);            wglab.open.remove(wglab.open.size()-1);                        if (Wygrana(temp))            {                                wygrany2 = temp;                break;            }                           if (temp.dol != null)                wglab.open.add(temp.dol);            if (temp.gora != null)                wglab.open.add(temp.gora);            if (temp.lewo != null)                wglab.open.add(temp.lewo);            if (temp.prawo != null)                wglab.open.add(temp.prawo);            licznik++;                  }         stop=System.currentTimeMillis();         if (wygrany2 == null) System.out.print("Nie znaleziono rozwiazania WGLAB.\n");         else          {            System.out.print("Znaleziono rozwiazanie w glab po przeszukaniu "+licznik+" stanow po "+(stop-start)+" milisekundach.\n");            WypiszPuzzle(wygrany2);         }                  //Metoda A* F1                  licznik=0;         System.out.println("Rozpoczynam przeszukiwanie metoda A* funkcja ocenajaca 1");         start=System.currentTimeMillis();         wezelZero.ocena=funOceniajaca1(wezelZero);         Wezel wygrany3=metodaA(wezelZero,1);         stop=System.currentTimeMillis();         if (wygrany3 == null) System.out.print("Nie znaleziono rozwiazania metoda A*.\n");         else          {            System.out.print("Znaleziono rozwiazanie metoda A* po przeszukaniu "+licznik+" stanow po "+(stop-start)+" milisekundach.\n");            WypiszPuzzle(wygrany3);         }                  //Metoda A* F2                   licznik=0;         System.out.println("Rozpoczynam przeszukiwanie metoda A* funkcja oceniajaca 2");         start=System.currentTimeMillis();         wezelZero.ocena=funOceniajaca2(wezelZero);         Wezel wygrany4=metodaA(wezelZero,2);         stop=System.currentTimeMillis();         if (wygrany4 == null) System.out.print("Nie znaleziono rozwiazania metoda A*.\n");         else          {            System.out.print("Znaleziono rozwiazanie metoda A* po przeszukaniu "+licznik+" stanow po "+(stop-start)+" milisekundach.\n");            WypiszPuzzle(wygrany4);         }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?