HDU 1043 Eight ((八数码问题)逆向BFS + 康托定理判重)

23 篇文章 0 订阅
12 篇文章 0 订阅

题意不说了,就是一个八数码

思路:

很暴力,直接把空格看成0,然后把九个数字连接起来,用这个数来代表这个状态。

然后正常来想这个题目时,肯定是 从给定状态到目标态来bfs搜索,但是这样做死活爆内存 (好难受= =)

我们需要转个弯,从目标态向给定态搜索,这样可以直接预处理打表了。

正好这是一个9个不同数字的排列,用康托定理完美去重。

注: 用c++交的,G++还是爆内存= =

#include <cstdio> 
#include <string>
#include <cstring>  
#include <algorithm>  
#include <queue>  
#include <vector>  
using namespace std;  
const int md = 370000 + 7;  
char cmd[7];  
queue<int>q;  
char tab[5][5];  
string state[md];  
const int dx[] = {0,0,-1,1}; /// 0 - l, 1 - r, 2 - u, 3 - d;  
const int dy[] = {-1,1,0,0};  
const char* flag = "rldu";  
int jie[10];  
void init(){  
    jie[0] = jie[1] = 1;  
    for (int i = 2; i < 9; ++i){  
        jie[i] = jie[i-1] * i;  
    }  
}  
char hashs[10];  
int Hash(int v){  
    for (int i = 0; i < 9; ++i){  
        hashs[9-i-1] = v % 10 + 48;  
        v /= 10;  
    }  
    int ans = 0;  
    for (int i = 0; i < 9; ++i){  
        int sum = 0;  
        for (int j = i+1; j < 9; ++j){  
            if (hashs[j] < hashs[i])++sum;  
  
        }  
        ans += sum * jie[9-i-1];  
    }  
    return ans;  
}  
bool vis[md];  
void  bfs(int vvvv){  
    while(!q.empty()) q.pop();  
    q.push(vvvv);  
    int v = 0;  
    string tu = "";  
    vis[Hash(vvvv)] = 1;  
    while(!q.empty()){  
        int u = q.front(); q.pop();  
        int idu = Hash(u);  
        tu = "";  
        for (int i = 0; i < 9; ++i){  
            tu += u % 10 + 48;  
            u/=10;  
        }  
        reverse(tu.begin(),tu.end());  
        int pos;  
        for (pos=0;pos<9; ++pos) if (tu[pos] =='0')break;  
  
        for (int i = 0; i < 9; ++i){  
            tab[i/3][i%3] = tu[i];  
        }  
        int x = pos/3; int y = pos % 3;  
        for (int i = 0 ; i < 4; ++i){  
            int xx = dx[i] + x;  
            int yy = dy[i] + y;  
            if (xx >= 0 && xx <= 2 && yy >=0 && yy <= 2){  
                swap(tab[x][y],tab[xx][yy]);  
                v = 0;  
                for (int j = 0; j < 3; ++j){  
                    for (int k = 0; k < 3; ++k){  
                        v = v * 10 + tab[j][k] - 48;  
                    }  
                }  
                int idv = Hash(v);  
                if (!vis[idv]){  
                    vis[idv] = 1;  
                    state[idv] = state[idu] + flag[i];  
                    q.push(v);  
                }  
                swap(tab[x][y],tab[xx][yy]);  
            }  
        }  
    }  
}  
  
int main(){  
    init();  
//    printf("%d\n",Hash(876543210));  
    memset(vis,0,sizeof vis);  
    for (int i = 0; i < md; ++i){  
        state[i] = "";  
    }  
    bfs(123456780);  
    for (int i = 0; i < md; ++i){  
        reverse(state[i].begin(),state[i].end());  
    }  
    while(~scanf("%s",cmd)){  
        if (cmd[0] == 'x')cmd[0] = 48;  
        int v = cmd[0] - 48;  
        for (int i = 1; i < 9; ++i){  
            scanf("%s",cmd);  
            if (cmd[0] == 'x') cmd[0] = '0';  
            v = v * 10 + cmd[0] - 48;  
        }  
        if (!vis[Hash(v)]) puts("unsolvable");  
        else printf("%s\n",state[Hash(v)].c_str());  
    }  
    return 0;  
}



Eight

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22065    Accepted Submission(s): 5922
Special Judge


Problem 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, several descriptions of configuration of the 8 puzzle. One 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. Do not print a blank line between cases.
 

Sample Input
  
  
2 3 4 1 5 x 7 6 8
 

Sample Output
  
  
ullddrurdllurdruldr
 

Source
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1044  1026  1072  1180  1016 
 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值