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

POJ1077(八数码)

时间:2016-08-25 23:28:57      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

Eight

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30127   Accepted: 13108   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

POJ上的数据比较水,杭电服务器维护,明天到杭电上提交试试
  1 //2016.8.25
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<algorithm>
  5 #include<map>
  6 
  7 using namespace std;
  8 
  9 int a[10][10], b[10], d[1000], sx, sy, deep;
 10 bool ok;
 11 map<int, bool> vis;
 12 char dir[4] = {u, d, l, r};
 13 int dx[4] = {-1, 1, 0, 0};
 14 int dy[4] = {0, 0, -1, 1};
 15 
 16 bool solve()//求逆序对判断是否有解
 17 {
 18     int cnt = 0;
 19     for(int i = 1; i <= 9; i++)
 20       for(int j = 0; j < i; j++)
 21         if(b[i] && b[j]>b[i])
 22           cnt++;
 23     return !(cnt%2);
 24 }
 25 
 26 int Astar()
 27 {
 28     int h = 0;
 29     for(int i = 1; i <= 3; i++)
 30         for(int j = 1; j <= 3; j++)
 31             if(a[i][j]!=0)
 32             {
 33                 int nx = (a[i][j]-1)/3;
 34                 int ny = (a[i][j]-1)%3;
 35                 h += (abs(i-nx-1)+abs(j-ny-1));
 36             }
 37     return h;
 38 }
 39 
 40 int toInt()//把矩阵转换为int型数字
 41 {
 42     int res = 0;
 43     for(int i = 1; i <= 3; i++)
 44       for(int j = 1; j <= 3; j++)
 45         res = res*10+a[i][j];
 46     return res;
 47 }
 48 
 49 void IDAstar(int x, int y, int step)
 50 {
 51     if(ok)return ;
 52     int h = Astar();
 53     if(!h && toInt()==123456780)//找到答案
 54     {
 55         for(int i = 0; i < step; i++)
 56             cout<<dir[d[i]];
 57         cout<<endl;
 58         ok = 1;
 59         return ;
 60     }
 61     if(step+h>deep)return ;
 62     int now = toInt();
 63     if(vis[now])return ;
 64     vis[now] = true;
 65     for(int i = 0; i < 4; i++)
 66     {
 67         int nx = x+dx[i];
 68         int ny = y+dy[i];
 69         if(nx>=1&&nx<=3&&ny>=1&&ny<=3)
 70         {
 71             d[step] = i;
 72             swap(a[x][y], a[nx][ny]);
 73             IDAstar(nx, ny, step+1);
 74             swap(a[x][y], a[nx][ny]);
 75             d[step] = 0;
 76         }
 77     }
 78     return;
 79 }
 80 
 81 int main()
 82 {
 83     char ch;
 84     while(cin >> ch)
 85     {
 86         ok = false;
 87         deep = 0;
 88         int cnt = 0;
 89         for(int i = 1; i <= 3; i++)
 90         {
 91             for(int j = 1; j <= 3; j++)
 92             {
 93                 if(i==1&&j==1);
 94                 else cin >> ch;
 95                 if(ch == x)
 96                 {
 97                     a[i][j] = 0;
 98                     sx = i;
 99                     sy = j;
100                 }else
101                   a[i][j] = ch - 0;
102                 b[cnt++] = a[i][j];
103             }
104         }
105         if(!solve())
106         {
107             cout<<"unsolvable"<<endl;
108             continue;
109         }
110         while(!ok)
111         {
112             vis.clear();
113             IDAstar(sx, sy, 0);
114             deep++;
115         }
116     }
117 
118     return 0;
119 }

 

POJ1077(八数码)

标签:

原文地址:http://www.cnblogs.com/Penn000/p/5808582.html

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