这是我写的一个九宫格问题,写得比较水,太复杂的就搞不好,希望看着能提出优化建议!!!

 

  1. import java.util.LinkedList;  
  2. import java.util.Queue;  
  3. import java.util.Scanner;  
  4.  
  5. import javax.management.Query;  
  6.  
  7.  
  8. public class java_debug {  
  9.       
  10.     static int queue_node_num=0;//node number  
  11.     static int son_node=0;//子节点位置  
  12.     static int closed_node_first=0;//close队列首节点  
  13.     int node_now=0;//中间变量  
  14.     static boolean node_exist = false;  
  15.     static node[] node_name=new node[10000];  
  16.     static int[][]   initial_element=new int[][]{{0,0,0},{0,0,0},{0,0,0}};  
  17.      //队列  
  18.      static Queue<Integer> open= new LinkedList<Integer>();  
  19.      static Queue<Integer> closed= new LinkedList<Integer>();  
  20.       
  21.     static int[][]  target_element= new  int[][]{{1,2,3},{4,5,6},{7,8,0}};  
  22.       
  23.       
  24.  
  25.     private static  void  input_function(int[][]  target_input)  
  26.     {  
  27.         System.out.println("please input the node!");  
  28.             for(int j=0;j<=2;j++)  
  29.                 for(int i=0;i<=2;i++){  
  30.                     Scanner   target_node=new Scanner(System.in);  
  31.                     target_input[j][i]=target_node.nextInt();  
  32.                     }  
  33.             for(int i=0;i<=2;i++)  
  34.             for(int ele_node:target_input[i])  
  35.             {  
  36.                 if(ele_node>9&&ele_node<0)   {System.out.println("the input is error!");  
  37.                     }}  
  38.         }  
  39.     //比较函数  
  40.     private static boolean compare_function(int[][] target_node,int[][] son_target)  
  41.         {   for(int i=0;i<=2;i++)  
  42.                 for(int j=0;j<=2;j++)  
  43.                     {  if(target_node[i][j]!=son_target[i][j])  
  44.                             return false;  
  45.                     }  
  46.         return true;  
  47.           
  48.           
  49.           
  50.         }  
  51.     //初始化该节点  
  52.     private static void initialize_node(node[] node_name,int queue_node_num,int father_node_num)  
  53.     {   node_name[queue_node_num+1].node_name=queue_node_num+1;  
  54.         node_name[queue_node_num+1].father_name=father_node_num;  
  55.         node_name[queue_node_num+1].location=location(node_name[queue_node_num+1]);  
  56.         open.offer(node_name[queue_node_num+1].node_name);  
  57.         }  
  58.     //0的位置     
  59.     private  static int location(node node_name)  
  60.     {int location_coord=0;  
  61.         for(int j=0;j<=2;j++)  
  62.         for(int i=0;i<=2;i++)  
  63.         {  
  64.             if(node_name.node_element[j][i]==0)  
  65.                 {location_coord=(j*3+i);  
  66.             return location_coord;}  
  67.             }  
  68.             return 9;  
  69.         }  
  70.     //该节点是否已经存在  
  71.     private static boolean compare_old_node(node[] node_name2,int queue_node_num)  
  72.         {  
  73.         while(closed.peek()!=null)  
  74.         {int i=0;i++;                         
  75.             if(i==0) { closed_node_first=closed.poll(); son_node=closed_node_first;}                      
  76.                 else son_node=closed.poll();  
  77.             if((son_node==closed_node_first)&&(i!=0)) {closed.offer(son_node);return false;}   
  78.             if(node_name2[queue_node_num+1].location==node_name2[son_node].location)   
  79.             node_exist=compare_function(node_name2[queue_node_num+1].node_element,node_name2[son_node].node_element);  
  80.             if(node_exist==true)  
  81.                 return true;  
  82.                 closed.offer(son_node);  
  83.           
  84.             }  
  85.             return false;  
  86.           
  87.         }  
  88.     //x,y代表初始坐标  z,w  代表目标坐标  row代表在行上移动还是在列上移动  
  89.     private   static void  initial_son_node(node[] node_name,int queue_node_num,int father_node_num,int x,int y, int z,int w,boolean row)  
  90.         {    node_name[queue_node_num+1]=new node( initial_element,queue_node_num+1,father_node_num,0);  
  91.             node_name[queue_node_num+1].node_element[z][w]=node_name[father_node_num].node_element[x][y];  
  92.           node_name[queue_node_num+1].node_element[x][y]= node_name[father_node_num].node_element[z][w];  
  93.           node_name[queue_node_num+1].node_name=queue_node_num+1;  
  94.           node_name[queue_node_num+1].father_name=father_node_num;  
  95.          for(int j=0;j<=2;j++)  
  96.          for(int i=0;i<=2;i++)  
  97.              {if(row==true) {  
  98.                  if(j==x&&(i==y||i==w))   
  99.                     {}  
  100.                  else     
  101.                      node_name[queue_node_num+1].node_element[j][i]=node_name[father_node_num].node_element[j][i];}  
  102.              else 
  103.                      {if(i==y&&(j==x||j==z))   
  104.                         {}  
  105.                      else     
  106.                          node_name[queue_node_num+1].node_element[j][i]=node_name[father_node_num].node_element[j][i];  
  107.                
  108.              }}  
  109.           
  110.           
  111.           
  112.           
  113.         }  
  114.     private static  int search(node[] node_name,int queue_node_num,int father_name_num,int x,int y,int z,int w,boolean row)  
  115.     {  boolean   closed_exist_state=true;  
  116.             initial_son_node(node_name,queue_node_num,father_name_num,x,y,z,w,row);//从父节点得到子节点  
  117.             closed_exist_state=compare_old_node(node_name,queue_node_num);//该节点是否存在closed节点中  
  118.             if(closed_exist_state==false)    
  119.                 {  
  120.                 initialize_node(node_name,queue_node_num,father_name_num);//对该类就行初始化  
  121.                 closed_exist_state=compare_function(node_name[queue_node_num].node_element,target_element);  
  122.                 if(closed_exist_state)  System.out.println("the result has find");//如果与目标节点相同则输出!  
  123.                 queue_node_num++;}  
  124.         return queue_node_num;  
  125.           
  126.     }  
  127.  
  128.       
  129.       
  130.       
  131.       
  132.  
  133.     @SuppressWarnings("null")  
  134.     public static void main(String[] args) throws Exception  
  135.     {  
  136.           
  137.        
  138.        
  139.      int[][]   begin_element=new  int[][]{{1,2,3},{0,4,5},{7,8,6}};  
  140.      int j=0;  
  141.      int father_node_num=0;  
  142.           
  143.      //初始化首节点              
  144.              node_name[0]=new node(begin_element,0,0,0);  
  145.              open.offer(node_name[0].node_name);//压入节点名称  
  146.              node_name[0].location=location( node_name[0]);  
  147.                
  148.                 
  149.                ////获取子节点  
  150.              while(open.peek()!=null) { //循环  
  151.                   
  152.                  j++;  
  153.                  if(j>500)System.out.println("the program is not find the result!!");  
  154.                  System.out.println(j);  
  155.                  father_node_num=open.poll();  
  156.                  if(compare_function(target_element,node_name[father_node_num].node_element))   
  157.                  { System.out.println("success!");  
  158.                     while(node_name[father_node_num].node_name!=0)  
  159.                     {  
  160.                         for(int i=0;i<=2;i++)  
  161.                         {for(int j1=0;j1<=2;j1++)  
  162.                         System.out.print(node_name[father_node_num].node_element[i][j1]+" ");  
  163.                         System.out.println( );    
  164.                         }  
  165.                         System.out.println(node_name[father_node_num].node_name);  
  166.                         father_node_num=node_name[father_node_num].father_name;  
  167.                           
  168.                           
  169.                           
  170.                     }  
  171.                    
  172.                    
  173.                    
  174.                    
  175.                  break;}  
  176.                   //queue_node_num=father_node_num;  
  177.                  closed.offer( father_node_num);//压入closed队列  
  178.                  son_node=node_name[father_node_num].location;  
  179.                  switch(son_node){  
  180.                   case 0:   
  181.                     /* node_name[queue_node_num+1].node_element[0][1]=node_name[father_node_num].node_element[0][0];  
  182.                      node_name[queue_node_num+1].node_element[0][0]= node_name[father_node_num].node_element[0][1];  
  183.                      node_name[queue_node_num+1].node_name=queue_node_num+1;  
  184.                      node_name[queue_node_num+1].father_name=father_node_num;  
  185.                     for(int j=0;j<=2;j++)  
  186.                      for(int i=0;i<=2;i++)  
  187.                     {    if(j==0&&(i==0||i==1))   
  188.                                 {}  
  189.                             else     
  190.                                  node_name[queue_node_num+1].node_element[j][i]=node_name[queue_node_num].node_element[j][i];  
  191.                     }*/ 
  192.                       queue_node_num= search(node_name,queue_node_num,father_node_num,0,0,0,1,true);  
  193.                       queue_node_num= search(node_name,queue_node_num,father_node_num,0,0,1,0,false);  
  194.                      break;  
  195.                  case 1:  
  196.                      queue_node_num= search(node_name,queue_node_num,father_node_num,0,1,0,0,true);  
  197.                      queue_node_num=search(node_name,queue_node_num,father_node_num,0,1,0,2,true);  
  198.                      queue_node_num=search(node_name,queue_node_num,father_node_num,0,1,1,1,false);  
  199.                         break;  
  200.                  case 2:  
  201.                      queue_node_num=search(node_name,queue_node_num,father_node_num,0,2,0,1,true);  
  202.                      queue_node_num=search(node_name,queue_node_num,father_node_num,0,2,1,2,false);  
  203.                         break;  
  204.                  case 3:  
  205.                      queue_node_num=search(node_name,queue_node_num,father_node_num,1,0,0,0,false);  
  206.                      queue_node_num=search(node_name,queue_node_num,father_node_num,1,0,1,1,true);  
  207.                      queue_node_num=search(node_name,queue_node_num,father_node_num,1,0,2,0,false);  
  208.                      break;  
  209.                  case 4:   
  210.                      queue_node_num= search(node_name,queue_node_num,father_node_num,1,1,0,1,false);  
  211.                      queue_node_num=search(node_name,queue_node_num,father_node_num,1,1,1,0,true);  
  212.                      queue_node_num=search(node_name,queue_node_num,father_node_num,1,1,1,2,true);  
  213.                      queue_node_num=search(node_name,queue_node_num,father_node_num,1,1,2,1,false);  
  214.                      break;  
  215.                  case 5:   
  216.                      queue_node_num=search(node_name,queue_node_num,father_node_num,1,2,0,2,false);  
  217.                      queue_node_num=    search(node_name,queue_node_num,father_node_num,1,2,2,2,false);  
  218.                      queue_node_num=search(node_name,queue_node_num,father_node_num,1,2,1,1,true);  
  219.                      break;  
  220.                  case 6:  
  221.                      queue_node_num=search(node_name,queue_node_num,father_node_num,2,0,1,0,false);  
  222.                      queue_node_num=search(node_name,queue_node_num,father_node_num,2,0,2,1,true);  
  223.                         break;  
  224.                  case 7:  
  225.                      queue_node_num= search(node_name,queue_node_num,father_node_num,2,1,2,0,true);  
  226.                      queue_node_num=search(node_name,queue_node_num,father_node_num,2,1,2,2,true);  
  227.                      queue_node_num=search(node_name,queue_node_num,father_node_num,2,1,1,1,false);  
  228.                      break;  
  229.                  case 8:  
  230.                      queue_node_num=search(node_name,queue_node_num,father_node_num,2,2,2,2,true);  
  231.                      queue_node_num=search(node_name,queue_node_num,father_node_num,2,2,1,2,false);  
  232.                      break;  
  233.                  default:   System.out.println("the location is error!");  
  234.                  } }  
  235.            
  236.     }}  

我的节点类:

  1.  
  2. public class node {  
  3.  
  4.         int[][]  node_element= new  int[3][3];  
  5.         int node_name;  
  6.         int father_name;  
  7.         int location;  
  8.     node(int[][] node_element,int node_name,int father,int location){  
  9.         this.father_name=father;  
  10.         this.node_name=node_name;  
  11.         for(int i=0;i<=2;i++)  
  12.             for(int j=0;j<=2;j++)  
  13.                 this.node_element[i][j]=node_element[i][j];  
  14.         this.location=location;  
  15.           
  16.     }  
  17.  
  18. }  

我这个写的比较水,我对复杂点数据结构的操作就搞不好了。最近好好总结一下!