8 Digital issues

Source: Internet
Author: User

Eight digital games (eight digital problems) is described as: on the 3x3 composition of the nine Gongge board, there are eight cards, each of the cards are engraved with 1-8 eight digital one of the digital. There is a space in the checkerboard that allows one of its neighbors to move the card to a space so that it can change the layout of the card continuously by moving the card. The problem with this game solution is that given an initial layout or structure (called the initial state) and a target (called the target State), ask how to move the card to achieve the transition from the initial state to the target State.

To solve the eight-digit problem, we must first consider whether there is an answer. Each state can be considered as a 1x9 matrix, the problem is whether the transformation of the matrix can be transformed into a matrix corresponding to the target State. It is known from mathematical knowledge that the inverse values of the two ordered series can be computed, and if both are even or odd, they can be reached by a transform, otherwise, these two states are not reachable. In this way, it is possible to determine whether the problem is solvable before the problem is solved, thus avoiding unnecessary searches.

A *: The estimate in the heuristic is expressed in the valuation function, such as: f (n) = g (n) + h (n) where F (n) is the value function of node n, and g (n) is the actual cost from the initial node to the N node in the state space, and H (N) is the estimated cost from N to the In this eight-digit problem, it is clear that g (n) is the number of steps moved from the initial state to the current state, the estimated cost of H (n) We can take the current state of each digital card is not the number of unknown target State, that is, the number of dislocation.


Programming Steps:

There are open, close, distance three lists, where open puts the surrounding points that were generated during the last iteration (the surrounding points cannot be in the close table, because you cannot walk the road that has been traversed), and distance puts the best F-values for all the points that have been traversed, Close is the point that has gone by

Each time by getting the first value in the open (open already sorted) and emptying the open, you get the point around temp (excluding the point already in close and updated by distance, which is, if it is larger than the value of that point in distance), The distance is updated with this point in the distance, otherwise it is updated) and the surrounding points are placed in open. Go to the next iterative process

Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Set; Public classEightpuzzlealgorithm {//Eight Digital issues                         Public voidSearch (eightnode start,eightnode target,list<eightnode> open,list<eightnode> Close,List<EightNode > Distance) {//Distance value stores the F value of the state in open//if (! Issolution (Start.getnodevalue (), Target.getnodevalue ())) {//System.out.println ("Two number does not meet the conditions, can not be found");//return;//        }                if(close==NULL) {Close=NewArrayList (); }                if(Open.size () ==0) {System.out.println ("Find failed!!!" "); return; }        if(Open.get (0). Equals ((target))) {System.out.println ("Find Successful!!!"); return; }        //for an already sorted open, choose the state with the lowest F valueSystem.out.println ("************************"); Eightnode Temp= Open.get (0); int[]tempnode =Temp.getnodevalue ();  for(inti = 0;i<3;i++){             for(intj = 0;j<3;j++) {System.out.print ("" +tempnode[i*3+J]);        } System.out.println (); } List<int[]>arround =Move (temp); System.out.println (Arround.size ()+ "********&&***&&"); //generate a list of arroundnode that are not included in closeList<eightnode>arroundnode =NewArraylist<eightnode>();  for(inti = 0;i<arround.size (); i++) {eightnode node=NewEightnode ();            NODE.SETF (Fvalue (Temp,arround.get (i), target));            NODE.SETG (Gvalue (temp));            Node.seth (Hvalue (Temp,arround.get (i), target));            Node.setnodevalue (Arround.get (i)); if(!close.contains (node))            {Arroundnode.add (node);                                }} close.add (temp); //Update distance (distance is used to store information about the surrounding points of all traversed points).          for(inti = 0;i<arroundnode.size (); i++){            BooleanFlag =false;  for(intj = 0;j<distance.size (); j + +){                if(Arroundnode.get (i). Equals (Distance.get (j))) {flag=true; if(Distance.get (j). GETF () >Arroundnode.get (i). GETF ())                    {Distance.set (J, Arroundnode.get (i)); }                     Break; }            }            if(Flag = =false) {Distance.add (Arroundnode.get (i)); }        }                //Update OpenOpen =NewArrayList ();        Open.addall (Arroundnode); System.out.println (Open.size ()+ "***********&&");  for(inti = 0;i<open.size (); i++){             for(intj = 0;j<distance.size (); j + +){                if(Open.get (i). Equals (Distance.get (j))) {Open.set (I, Distance.get (j)); }}} System.out.println (Open.size ()+ "***............********&&"); Open=sort (open);    Search (start,target,open,close,distance); }         Public intHvalue (Eightnode Temp1,int[]arroundi,eightnode Target1] {        int[]temp =Temp1.getnodevalue (); int[]target=Target1.getnodevalue (); intH = 0;  for(inti = 0;i<target.length;i++){            if(target[i]!=Arroundi[i]) {h=h+1; }        }        returnh; }         PublicList<eightnode>sort (list<eightnode>Open) {         for(inti = 0;i<open.size (); i++){             for(intJ =i+1;j<open.size (); j + +){                if(Open.get (i). GETF () >Open.get (j). GETF ()) {Eightnode temp=Open.get (i);                    Open.set (I, Open.get (j));                Open.set (J, temp); }            }        }        returnOpen; }                 Public intgvalue (Eightnode temp) {returnTEMP.GETG (+1); }     Public intFvalue (Eightnode Temp1,int[]arroundi,eightnode Target1] {        returnHvalue (TEMP1,ARROUNDI,TARGET1) +Gvalue (TEMP1); }                //for an open selected state, how to get its neighboring state     Publiclist<int[]>Move (Eightnode temp1) {int[]temp =Temp1.getnodevalue (); List<int[]>list =Newarraylist<int[]>(); //first Find out 0 (where the space is located)        intPosition = 0;  for(inti = 0;i<temp.length;i++){            if(temp[i]==0) {Position=i;  Break; }        }        if(position==0) {List.add (Swap (Temp.clone (),0,1)); List.add (Swap (Temp.clone (),0,3)); }Else if(position==1) {List.add (Swap (Temp.clone (),1,0)); List.add (Swap (Temp.clone (),1,4)); List.add (Swap (Temp.clone (),The)); }Else if(position==2) {List.add (Swap (Temp.clone (),2,1)); List.add (Swap (Temp.clone (),5,2)); }Else if(position==3) {List.add (Swap (Temp.clone (),3,0)); List.add (Swap (Temp.clone (),3,4)); List.add (Swap (Temp.clone (),3,6)); }Else if(position==4) {List.add (Swap (Temp.clone (),4,1)); List.add (Swap (Temp.clone (),4,3)); List.add (Swap (Temp.clone (),4,5)); List.add (Swap (Temp.clone (),4,7)); }Else if(position==5) {List.add (Swap (Temp.clone (),5,2)); List.add (Swap (Temp.clone (),5,8)); List.add (Swap (Temp.clone (),5,4)); }Else if(position==6) {List.add (Swap (Temp.clone (),6,3)); List.add (Swap (Temp.clone (),6,7)); }Else if(position==7) {List.add (Swap (Temp.clone (),7,6)); List.add (Swap (Temp.clone (),7,8)); List.add (Swap (Temp.clone (),7,4)); }Else if(position==8) {List.add (Swap (Temp.clone (),8,5)); List.add (Swap (Temp.clone (),8,7)); }        returnlist; }             Public int[] Swap (int[] temp,intIintj) {        intA =Temp[i]; Temp[i]=Temp[j]; TEMP[J]=A; returntemp; }                            //determine if there is a solution, the inverse number of two arrays is even or odd     Public  BooleanIssolution (int[]current,int[]target] {        intN1 =Inversenumber (current); intN2 =Inversenumber (target); if(n1%2==n2%2){            return true; }Else{            return false; }    }    Private  intInversenumber (int[] current) {        intLength1 =current.length; intCount1 = 0;  for(inti = 0;i<length1;i++){            inttemp =Current[i];  for(intj = i+1;j<length1;j++){                if(temp>Current[j]) {Count1= Count1+1; }            }        }        returnCount1; }                    }

8 Digital issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.