UVA 10085(bfs+康拓展开)八数码问题

本文探讨了8宫格谜题的一种变体,即从初始状态出发,寻找能够达到距离最远目标状态的策略。通过算法实现路径搜索,确保找到最远的目标配置,并提供从初始状态到目标状态的最小移动路径。此过程涉及状态转换、哈希表应用及深度优先搜索算法的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Download as PDF

Problem A

The Most Distant State

Input: standard input

Output: standard output

 

The 8-puzzle is a square tray in which eight square tiles are placed. The remaining ninth square is uncovered. Each tile has a number on it. A tile that is adjacent to the blank space can be slid into that space. A game consists of a starting state and a specified goal state. The starting state can be transformed into the goal state by sliding (moving) the tiles around. The 8-puzzle problem asks you to do the transformation in minimum number of moves.

 

2

8

3

 

 

 

1

2

3

1

6

4

=>

8

 

4

7

 

5

 

 

 

7

6

5

Start

 

 

 

Goal

 

However, our current problem is a bit different. In this problem, given an initial state of the puzzle your are asked to discover a goal state which is the most distant (in terms of number of moves) of all the states reachable from the given state.


Input

The first line of the input file contains an integer representing the number of test cases to follow. A blank line follows this line.


Each test case consists of 3 lines of 3 integers each representing the initial state of the puzzle. The blank space is represented by a 0 (zero). A blank line follows each test case.

 

Output

For each test case first output the puzzle number. The next 3 lines will contain 3 integers each representing one of the most distant states reachable from the given state. The next line will contain the shortest sequence of moves that will transform the given state to that state. The move is actually the movement of the blank space represented by four directions : U (Up), L (Left), D (Down) and R (Right). After each test case output an empty line.

 

Sample Input

1

2 6 4
1 3 7
0 5 8

Sample Output

Puzzle #1
8 1 5
7 3 6
4 0 2
UURDDRULLURRDLLDRRULULDDRUULDDR

__________________________________________________________________________________________ 
Rezaul Alam Chowdhury 

"A fool looks for happiness in the distance, those who are intelligent grow it under their own feet."


总算敲出来了,痛苦哎。。。

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<vector>
#include<string.h>
#include<stack>
#include<map>
using namespace std;
vector<int>mat;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int A[]={1,1,2,6,24,120,720,5040,40320};
int hash_k(vector<int>T)
{
    int ans=0;
    for(int i=0;i<9;i++){
        int t=T[i];
        for(int j=0;j<i;j++){
            if(T[j]<T[i])t--;
        }
        ans+=t*A[8-i];
    }
    return ans;
}
int vis[362890];
queue<vector<int> >q;
map<vector<int>,vector<int> >p;
stack<char>FF;
char get_(vector<int>p,vector<int>q){
    int z1,z2;
    for(z1=0;z1<9;z1++)if(!p[z1])break;
    int x1=z1/3,y1=z1%3;
    for(z2=0;z2<9;z2++)if(!q[z2])break;
    int x2=z2/3,y2=z2%3;
    if(x1!=x2){
        if(x1-x2==1)return 'U';
        else return 'D';
    }
    if(y1-y2==1)return 'L';
    else return 'R';
}
int main()
{
    //freopen("in.txt","r",stdin);
    int T;
    cin>>T;
    int cas=1;
    while(T--){
    memset(vis,0,sizeof(vis));
     mat.clear();
     while(!q.empty())q.pop();
     while(!FF.empty())FF.pop();
     p.clear();
     vector<int>ans;
     int num;
     for(int i=0;i<9;i++){
            scanf("%d",&num);
            mat.push_back(num);
     }
     q.push(mat);
     vis[hash_k(mat)]=1;
     vector<int>u;
     vector<int>t;
     while(!q.empty())
     {
        u=q.front();
        q.pop();
        int z=0;
        for(z=0;z<9;z++)if(!u[z])break;
        int x=z/3,y=z%3;
        for(int i=0;i<4;i++)
        {
            int nx=x+dx[i],ny=y+dy[i];
            if(nx>=0&&nx<3&&ny>=0&&ny<3)
            {
                int nz=nx*3+ny;
                t=u;
                //cout<<z<<' '<<nz<<endl;
                t[nz]=u[z];
                t[z]=u[nz];
                int ok=hash_k(t);
                if(!vis[ok]){
                    vis[ok]=1;
                    q.push(t);
                    p[t]=u;
                    ans=t;

                }
            }
        }
     }
     cout<<"Puzzle #"<<cas++<<endl;
     for(int i=0;i<9;i++)
        printf("%d%c",ans[i],i%3==2?'\n':' ');
    FF.push('\n');
    while(1){
            if(ans==mat)break;
            FF.push(get_(p[ans],ans));
            ans=p[ans];
    }
    while(!FF.empty()){
        putchar(FF.top());
        FF.pop();
    }
    putchar('\n');
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值