码迷,mamicode.com
首页 > 其他好文 > 详细

浅学八数码 poj1077

时间:2015-05-09 08:47:18      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

Eight

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26261   Accepted: 11490   Special Judge

Description

The 15-puzzle has been around for over 100 years; even if you don‘t know it by that name, you‘ve seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let‘s call the missing tile ‘x‘; the object of the puzzle is to arrange the tiles so that they are ordered as: 
 1  2  3  4 

5 6 7 8
9 10 11 12
13 14 15 x

where the only legal operation is to exchange ‘x‘ with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle: 
 1  2  3  4    1  2  3  4    1  2  3  4    1  2  3  4 

5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8
9 x 10 12 9 10 x 12 9 10 11 12 9 10 11 12
13 14 11 15 13 14 11 15 13 14 x 15 13 14 15 x
r-> d-> r->

The letters in the previous row indicate which neighbor of the ‘x‘ tile is swapped with the ‘x‘ tile at each step; legal values are ‘r‘,‘l‘,‘u‘ and ‘d‘, for right, left, up, and down, respectively. 

Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and 
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing ‘x‘ tile, of course). 

In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three 
arrangement. 

Input

You will receive a description of a configuration of the 8 puzzle. The description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus ‘x‘. For example, this puzzle 
 1  2  3 

x 4 6
7 5 8

is described by this list: 

1 2 3 x 4 6 7 5 8

Output

You will print to standard output either the word ``unsolvable‘‘, if the puzzle has no solution, or a string consisting entirely of the letters ‘r‘, ‘l‘, ‘u‘ and ‘d‘ that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning of the line.

Sample Input

 2  3  4  1  5  x  7  6  8 

Sample Output

ullddrurdllurdruldr

Source

第一次的代码:
bfs+康拓展开;
略有点粗糙,晚上交一发,直接过了,但是时间不是很理想,明天再改进一下。
Accepted 24600K 63MS C++ 3370B 2015-05-09 00:58:11
技术分享
  1 #include<iostream>
  2 #include<algorithm>
  3 #include<cstdio>
  4 #include<cstring>
  5 using namespace std;
  6 
  7 int hehe[3][3]={1,2,6,24,120,720,5040,40320,362880};
  8 int hehe2[9]={1,2,6,24,120,720,5040,40320,362880};
  9 char w[4]={r,l,u,d};
 10 int dx[]={0,0,-1,1};
 11 int dy[]={1,-1,0,0};
 12 class Matrix
 13 {
 14 private:
 15     int t[3][3];
 16     int x0,y0;
 17     int d;
 18     int last;
 19 public:
 20     void Set_value(int x,int y,char *s);
 21     void Set_x(int tx){x0=tx;}
 22     void Set_y(int ty){y0=ty;}
 23     void Set_Deal(int a) { d=a; }
 24     void Set_Last(int w){last=w;}
 25     int Get_Deal() { return d;  }
 26     int Get_Last(){return last; }
 27     int Get_x(){return x0;}
 28     int Get_y(){return y0;}
 29     void Swap(int x1,int y1,int x2,int y2)
 30     {
 31         int e=t[x1][y1];
 32         t[x1][y1]=t[x2][y2];
 33         t[x2][y2]=e;
 34     }
 35     int Get_value(int x,int y) { return t[x][y];  }
 36     int Get_hash();
 37     void operator = (Matrix & w)
 38     {
 39         for(int i=0;i<3;i++)
 40             for(int j=0;j<3;j++)
 41                 t[i][j]=w.t[i][j];
 42         x0=w.x0;
 43         y0=w.y0;
 44     }
 45     void Show()
 46     {
 47         for(int i=0;i<3;i++,cout << endl)
 48             for(int j=0;j<3;cout << t[i][j] << " ",j++);
 49     }
 50 };
 51 int Matrix::Get_hash()
 52 {
 53     int rev=0;
 54     for(int i=0;i<3;i++)
 55         for(int j=0;j<3;j++)
 56             rev+=t[i][j]*hehe[i][j];
 57     return rev;
 58 }
 59 void Matrix::Set_value(int x,int y,char *s)
 60 {
 61     if(!isdigit(s[0]))
 62     {
 63         t[x][y]=0;
 64         x0=x,y0=y;
 65     }
 66     else
 67     {
 68         int len=strlen(s),c=0;
 69         for(int i=0;i<len;i++)
 70             c=c*10+s[i]-0;
 71         t[x][y]=c;
 72     }
 73 }
 74 int vis[4000000];
 75 Matrix Mx[10000000];
 76 int Ans=0;
 77 void output(int now)
 78 {
 79     if(Mx[now].Get_Last()==-1) return;
 80     else
 81     {
 82         output(Mx[now].Get_Last());
 83         printf("%c",w[Mx[now].Get_Deal()]);
 84     }
 85 }
 86 bool check(int x,int y)
 87 {
 88     if(x>=0&&x<=2&&y>=0&&y<=2) return true;
 89     return false;
 90 }
 91 void bfs()
 92 {
 93     memset(vis,0,sizeof(vis));
 94     vis[Mx[0].Get_hash()]=1;
 95     Mx[0].Set_Deal(-1);
 96     Mx[0].Set_Last(-1);
 97     int Start=1;
 98     int End=0;
 99     while(End<Start)
100     {
101         Matrix mx;
102         mx=Mx[End++];
103         int nx=mx.Get_x();
104         int ny=mx.Get_y();
105         for(int i=0;i<4;i++)
106         {
107             int tx=nx+dx[i];
108             int ty=ny+dy[i];
109             if(check(tx,ty))
110             {
111                 mx.Swap(nx,ny,tx,ty);
112                 mx.Set_x(tx),mx.Set_y(ty);
113                 int temp=mx.Get_hash();
114                 if(vis[temp]==0)
115                 {
116                     vis[temp]=1;
117                     Mx[Start]=mx;
118                     Mx[Start].Set_Deal(i);
119                     Mx[Start].Set_Last(End-1);
120                     if(temp==Ans)
121                     {
122                         output(Start);
123                         printf("\n");
124                         return;
125                     }
126                     Start++;
127                 }
128                 mx.Swap(nx,ny,tx,ty);
129                 mx.Set_x(nx),mx.Set_y(ny);
130             }
131         }
132     }
133     printf("unsolvable\n");
134 }
135 int main()
136 {
137     for(int i=0;i<8;i++)  Ans+=(i+1)*hehe2[i];
138     char temp[10];
139     for(int i=0;i<3;i++)
140         for(int j=0;j<3;j++)
141         {
142             scanf("%s",temp);
143             Mx[0].Set_value(i,j,temp);
144         }
145     bfs();
146     return 0;
147 }
148 //注释下次一起写
View Code

 

浅学八数码 poj1077

标签:

原文地址:http://www.cnblogs.com/I-love-HLD/p/4489251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!