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

Description

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;
}

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

时间: 2024-08-05 14:30:07

UVA 10085(bfs+康拓展开)八数码问题的相关文章

UVALive 6665 Dragon&#226;??s Cruller --BFS,类八数码问题

题意大概就是八数码问题,只不过把空格的移动方式改变了:空格能够向前或向后移动一格或三格(循环的). 分析:其实跟八数码问题差不多,用康托展开记录状态,bfs即可. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <queue&g

九宫重拍(bfs + 康拓展开)

问题描述 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干次移动,可以形成第二个图所示的局面. 我们把第一个图的局面记为:12345678. 把第二个图的局面记为:123.46758 显然是按从上到下,从左到右的顺序记录数字,空格记为句点. 本题目的任务是已知九宫的初态和终态,求最少经过多少步的移动可以到达.如果无论多少步都无法到达,则输出-1. 输入格式 输入第一行包含九宫的初态,第二行包含九宫的终态. 输出格式 输出最

hdu 1034 Eight 传说中的八数码问题。真是一道神题,A*算法+康托展开

Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13506    Accepted Submission(s): 3855 Special Judge Problem Description The 15-puzzle has been around for over 100 years; even if you don'

浅学八数码 poj1077

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

hdu 1043 Eight (八数码问题)【BFS】+【康拓展开】

<题目链接> 题目大意:给出一个3×3的矩阵(包含1-8数字和一个字母x),经过一些移动格子上的数后得到连续的1-8,最后一格是x,要求最小移动步数. 解题分析:本题用BFS来寻找路径,为了降低复杂度,用BFS从最终的目标状态开始处理,将所有搜索到状态以及对应的路径打表记录,然后对于输入的矩阵,直接查表输出答案 即可,本题还有一个难点,就是如何判断记录已经搜索过的状态,如果使用map+string(矩阵看成一维)会超时,所以我们这里用康拓展开式来记录状态. #include<cstdio

BFS+康托展开(洛谷1379 八数码难题)

在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了使题目简单,设目标状态为123804765),找到一种最少步骤的移动方法,实现从初始布局到目标布局的转变. 输入格式: 输入初试状态,一行九个数字,空格用0表示 输出格式: 只有一行,该行只有一个数字,表示从初始状态到目标状态需要的最少移动次数(测试数据中无特殊无法到达目标状态数据) 输入样例#1: 2831

poj 1077 八数码(BFS+康托展开)

1 /* 2 题意:八数码问题,给出3*3的矩阵含1~8以及x,给出一个符合的解使得移动后的矩阵的顺序为1~8,最后为x 3 4 题解:BFS 5 需要用到康托展开来表示状态,不然数组无法完全表示所有状态,这样BFS就无法判断找不到解的情况(status 6 的0ms,0KB究竟是怎么做到的,简直不能想象=.=) 7 */ 8 #include <cstdio> 9 #include <cstring> 10 #include <queue> 11 #include &

HDU 1043 Eight (BFS&#183;八数码&#183;康托展开)

题意  输出八数码问题从给定状态到12345678x的路径 用康托展开将排列对应为整数  即这个排列在所有排列中的字典序  然后就是基础的BFS了 #include <bits/stdc++.h> using namespace std; const int N = 5e5, M = 9; int x[4] = { -1, 1, 0, 0}; int y[4] = {0, 0, -1, 1}; int fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320

【HDOJ3567】【预处理bfs+映射+康拓展开hash】

http://acm.hdu.edu.cn/showproblem.php?pid=3567 Eight II Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 130000/65536 K (Java/Others)Total Submission(s): 4541    Accepted Submission(s): 990 Problem Description Eight-puzzle, which is also calle