主页 > 程序人生 > Java写的八数码

Java写的八数码

先献上java版八数码的代码,未使用启发式搜索,只是将所有状态预处理,然后直接读出罢了。。。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
 
import java.util.*;
 
/**
 *
 * @author Zerob13
 */
class node{
    int[][] state;
    int pre,tem;
    char dir;
    node()
    {
        int i,j,k=1;
     state=new int[3][3];
        for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++)
            {
                state[i][j]=k++;
            }
        }
        pre=tem=dir=0;
    }
    int HASH()
     {
          int fac[]={1,1,2,6,24,120,720,5040,40320,362880};
        int i,j,ret=0,num=0;
        int[] b=new int[10];
        for(i=0;i<3;i++)
            for(j=0;j<3;j++)
            {
                b[ret++]=state[i][j];
            }
            ret=0;
        for(i=0;i<9;i++)
        {
                num=0;
            for(j=0;j<i;j++)
            {
                if(b[j]>b[i])  num++;
            }
            ret+=fac[i]*num;
        }
            tem=ret;
        return ret;
    }
}
 
public class Main {
 
 
  static  char output(int q)
{
        if(q==1)
            return 'u';
        if(q==2)
            return 'd';
        if(q==3)
            return 'r';
        if(q==0)
            return 'l';
        return 0;
}
 
    public static void main(String[] args) {
 
      node[] queue=new node[362882];
      byte[] hash=new byte[362882];
      int[] sa=new int[362882];
      int i,top,la,j,aaa,con;
      for(i=0;i<362882;i++)hash[i]=0;
      int[][] dir={{0,1},{1,0},{-1,0},{0,-1}};
      top=la=0;
      node head = new node(),p=new node();
      aaa=1;
       for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++)
            {
                head.state[i][j]=aaa;
                aaa++;
             }
        }
      head.pre=-1;
      head.HASH();
      head.dir=0;
      hash[head.tem]=1;
      top=0;
      la=1;
      con=0;
       queue[0]=new node();
         for(i=0;i<3;i++)
        {
            for (j = 0; j < 3; j++) {
                queue[0].state[i][j] = head.state[i][j];
            }
        }
        queue[0].pre = head.pre;
        queue[0].dir = head.dir;
        queue[0].tem = head.tem;
      while(top<la)
      {
           int ii,e,f,x,y,tem;
           for(ii=0;ii<3;ii++)
           {
               for(j=0;j<3;j++)
               {
                   head.state[ii][j]=queue[top].state[ii][j];
               }
           }
           head.pre=queue[top].pre;
           head.dir=queue[top].dir;
           head.tem=queue[top].tem;
          sa[head.tem]=top;
          e=f=0;
           for(ii=0;ii<3;ii++)
                for(j=0;j<3;j++)
                {
                    p.state[ii][j]=head.state[ii][j];
                    if(p.state[ii][j]==9)
                        {e=ii;f=j;}
                }
 
          for(i=0;i<4;i++)
                {
                    x=dir[i][0]+e;
                    y=dir[i][1]+f;
                    if(x<0||y<0||x>=3||y>=3)
                    continue;
                       p.state[x][y]^=p.state[e][f];
                       p.state[e][f]^=p.state[x][y];
                       p.state[x][y]^=p.state[e][f];
                      tem=p.HASH();
                       if(hash[tem]==0)
                       {
                          p.pre=top;
                            p.tem=tem;
                            p.dir=output(i);
                            hash[tem]=1;
                            queue[la]=new node();
                            for(ii=0;ii<3;ii++)
                            {
                                for(j=0;j<3;j++)
                                {
                                    queue[la].state[ii][j]=p.state[ii][j];
                                }
                            }
                            queue[la].pre=p.pre;
                            queue[la].dir=p.dir;
                            queue[la].tem=p.tem;
                            la++;
                       }
                       p.state[x][y]^=p.state[e][f];
                       p.state[e][f]^=p.state[x][y];
                       p.state[x][y]^=p.state[e][f];
                }
                 top++;
      }
 
 
      Scanner in=new Scanner(System.in);
      String kk;
      while(in.hasNextLine())
      {
          kk=in.nextLine();
          int[] s=new int[100];
          int bbb=0;
          aaa=0;
          for(i=0;i<kk.length();i++)
          {
              if(kk.charAt(i)!=' ')
              {
                  if(kk.charAt(i)=='x')
                  {
                      s[aaa++]=9;
                  }else
                      s[aaa++]=kk.charAt(i)-'0';
 
              }
          }
          for(i=0;i<3;i++)
          {
              for(j=0;j<3;j++)
              {
                  head.state[i][j]=s[bbb++];
              }
          }
          aaa=head.HASH();
          if(hash[aaa]!=0)
          {
              node star=queue[sa[aaa]];
              while(star.pre!=-1)
              {
                  System.out.print(star.dir);
                  star=queue[star.pre];
              }
              System.out.println("");
          }else
          {
              System.out.println("unsolvable");
          }
      }
 
    }
}

相关日志

, , , , ,

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

引用:0

下面所列的是引用到本博客的链接
Java写的八数码 来自 混沌的云
顶部