八数码问题

单向和双向bfs

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define ls rt<<1
13 #define rs rt<<1|1
14 #define lson ls,nl,mid,l,r
15 #define rson rs,mid+1,nr,l,r
16 #define N 100010
17 #define For(i,a,b) for(register int i=a;i<=b;i++)
18 #define p(a) putchar(a)
19 #define g() getchar()
20 
21 using namespace std;
22 int n,temp;
23 int dx[]={-1,1,0,0};
24 int dy[]={0,0,-1,1};
25 int a[10][10];
26 map<int,int>m;
27 queue<int>q;
28 int t,x,y,nx,ny;
29 void in(int &x){
30     int y=1;
31     char c=g();x=0;
32     while(c<'0'||c>'9'){
33         if(c=='-')y=-1;
34         c=g();
35     }
36     while(c<='9'&&c>='0'){
37         x=(x<<1)+(x<<3)+c-'0';c=g();
38     }
39     x*=y;
40 }
41 void o(int x){
42     if(x<0){
43         p('-');
44         x=-x;
45     }
46     if(x>9)o(x/10);
47     p(x%10+'0');
48 }
49 
50 inline void Turn(int n){
51     for(register int i=3;i;i--)
52         for(register int j=3;j;j--){
53             a[i][j]=n%10;
54             n/=10;
55             if(!a[i][j]){
56                 x=i;
57                 y=j;
58             }
59         }
60 }
61 
62 int main(){
63     in(n);
64     q.push(n);
65     while(!q.empty()){
66         t=q.front();q.pop();
67         if(t==123804765)
68             break;
69         Turn(t);
70         For(I,0,3){
71             nx=x+dx[I];
72             ny=y+dy[I];
73             if(nx>3||ny>3||nx<1||ny<1) continue;
74             swap(a[nx][ny],a[x][y]);
75             temp=0;
76             For(i,1,3)
77                 For(j,1,3){
78                     temp*=10;
79                     temp+=a[i][j];
80                 }
81             if(!m.count(temp)){
82                 m[temp]=m[t]+1;
83                 q.push(temp);
84             }
85             swap(a[nx][ny],a[x][y]);
86         }
87     }
88     o(m[123804765]);
89     return 0;
90 }
View Code
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<ctime>
  7 #include<set>
  8 #include<map>
  9 #include<stack>
 10 #include<cstring>
 11 #define inf 2147483647
 12 #define ls rt<<1
 13 #define rs rt<<1|1
 14 #define lson ls,nl,mid,l,r
 15 #define rson rs,mid+1,nr,l,r
 16 #define N 123804765
 17 #define For(i,a,b) for(int i=a;i<=b;i++)
 18 #define p(a) putchar(a)
 19 #define g() getchar()
 20 
 21 using namespace std;
 22 int n,temp;
 23 int dx[]={-1,1,0,0};
 24 int dy[]={0,0,-1,1};
 25 int a[10][10];
 26 map<int,int>m,d;
 27 
 28 queue<int>q;
 29 int t,x,y,nx,ny;
 30 void in(int &x){
 31     int y=1;
 32     char c=g();x=0;
 33     while(c<'0'||c>'9'){
 34         if(c=='-')y=-1;
 35         c=g();
 36     }
 37     while(c<='9'&&c>='0'){
 38         x=(x<<1)+(x<<3)+c-'0';c=g();
 39     }
 40     x*=y;
 41 }
 42 void o(int x){
 43     if(x<0){
 44         p('-');
 45         x=-x;
 46     }
 47     if(x>9)o(x/10);
 48     p(x%10+'0');
 49 }
 50 
 51 inline void Turn(int n){
 52     for(int i=3;i;i--)
 53         for(int j=3;j;j--){
 54             a[i][j]=n%10;
 55             n/=10;
 56             if(!a[i][j]){
 57                 x=i;
 58                 y=j;
 59             }
 60         }
 61 }
 62 
 63 int main(){
 64     in(n);
 65     if(n==N){
 66         o(0);
 67         return 0;
 68     }
 69     q.push(n);
 70     q.push(N);
 71     m[N]=1;
 72     d[n]=1;
 73     d[N]=2;
 74     while(!q.empty()){
 75         t=q.front();q.pop();
 76         Turn(t);
 77         For(I,0,3){
 78             nx=x+dx[I];
 79             ny=y+dy[I];
 80             if(nx>3||ny>3||nx<1||ny<1) continue;
 81             swap(a[nx][ny],a[x][y]);
 82             temp=0;
 83             For(i,1,3)
 84                 For(j,1,3){
 85                     temp*=10;
 86                     temp+=a[i][j];
 87                 }
 88             if(d[temp]==d[t]){
 89                 swap(a[nx][ny],a[x][y]);
 90                 continue;
 91             }
 92             if(d[temp]+d[t]==3){
 93                 o(m[temp]+m[t]);
 94                 return 0;
 95             }
 96             d[temp]=d[t];
 97             m[temp]=m[t]+1;
 98             q.push(temp);
 99             swap(a[nx][ny],a[x][y]);
100         }
101     }
102     return 0;
103 }
View Code

 

posted @ 2019-03-11 22:04  WeiAR  阅读(119)  评论(0编辑  收藏  举报