uva10085 The most distant state(BFS)

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."

十进制记录状态,bfs以便即可

#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <queue>
#define ll long long
using namespace std;
int T;
struct node{
    int x,y,st;
    string ans;
}f,Ans;
ll ST;
map<ll,int> vis;
char s[4]={'U','L','D','R'};
int d[4][2]={-1,0,0,-1,1,0,0,1};
ll g[10];
int a[4][4];
void init(){
    g[0]=1;
    for(int i=1;i<10;i++) g[i]=g[i-1]*10;
}
void read(){
    vis.clear();
    ST=0;
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            scanf("%d",&a[i][j]);
            if(a[i][j]==0) f.x=i,f.y=j;
            ST=ST*10+a[i][j];
        }
    }
}
int calc(int x,int y){
    return (2-x)*3+(2-y);
}
void solve(int ca){
    queue<node> q;
    f.st=ST,f.ans="";
    q.push(f);
    vis[ST]=1;
    while(!q.empty()){
        node z=q.front();
        q.pop();
        for(int k=0;k<4;k++){
            f.x=z.x+d[k][0];
            f.y=z.y+d[k][1];
            if(!(f.x>=0&&f.x<3&&f.y>=0&&f.y<3)) continue;
            int ff=calc(f.x,f.y),zz=calc(z.x,z.y);
            int fx=z.st/g[ff]%10;
            f.st=z.st;
            f.st-=fx*g[ff];
            f.st+=fx*g[zz];
            if(!vis[f.st]){
                vis[f.st]=1;
                f.ans=z.ans+s[k];
                q.push(f);
            }
        }
        if(q.empty()) Ans=z;
    }
    printf("Puzzle #%d\n",ca);
    for(int i=8;i>=0;i--){
        if(i%3!=2) printf(" ");
        printf("%d",Ans.st/g[i]%10);
        if(i%3==0) printf("\n");
    }
    printf("%s\n",Ans.ans.c_str());
}
int main(){
    init();
    scanf("%d",&T);
    for(int ca=1;ca<=T;ca++){
        read();
        solve(ca);
    }
    return 0;
}
时间: 2024-10-06 23:15:13

uva10085 The most distant state(BFS)的相关文章

[Uva 10085] The most distant state (BFS)

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1026 题目大意:就是说给你一个8数码,问你能够达到的距离最远的状态是什么. 刚开始以为只要顺着一边走就行了,后来发现这样走可能最远到达的状态也能通过其他方式到达. 在这里WA了好多次. 应该把所有可能出现的情况全都枚举出来,然后判重.. UVA也真是慢..4s的代码跑了快4分钟..

UVA The most distant state

题目如下: Problem A The Most Distant State Input: standard input Output: standard output The 8-puzzle is a square tray inwhich eight square tiles are placed. The remaining ninth square is uncovered.Each tile has a number on it. A tile that is adjacent to

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 adj

HDU 1026 bfs与dfs

一般来说,广搜常用于找单一的最短路线,或者是规模小的路径搜索,它的特点是"搜到就是最优解", 而深搜用于找多个解或者是"步数已知(比如3步就必须达到条件)"的问题,它的空间效率高,但是找到的不一定是最优解,必须记录并完成整个搜索,故一般情况下,深搜需要非常高效的剪枝(优化). 像搜索最短路径这些的很明显要是用广搜,因为广搜的特性就是一层一层往下搜的,保证当前搜到的都是最优解,当然,最短路径只是一方面的应用,像什么最少状态转换也是可以应用的.深搜就是优先搜索一棵子树,

BFS DFS 模板

/* 该 DFS 框架以 2D 坐标范围为例,来体现 DFS 算法的实现思想. */ #include<cstdio> #include<cstring> #include<cstdlib> using namespace std; const int maxn=100; bool vst[maxn][maxn]; //访问标记 int map[maxn][maxn]; //坐标范围 int dir[4][2]= {0,1,0,-1,1,0,-1,0}; // 方向向量

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

HDU 1043 Eight八数码解题思路(bfs+hash 打表 IDA* 等)

题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原为初始状态 思路: 参加网站比赛时拿到此题目,因为之前写过八数码问题,心中暗喜,于是写出一套暴力bfs+hash,结果TLE呵呵 思路一:bfs+hash(TLE) 1 #include <cstdio> 2 #include <cstring> 3 #include <queu

poj1753 bfs+奇偶性减枝//状压搜索

http://poj.org/problem?id=1753 题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右)棋子的颜色都会被改变(白变成黑,黑变成白),问你将所有棋子变成白色或者黑色最少的步数. 思路: 1.如果用一个4*4的数组存储每一种状态,不但存储空间很大,而且在穷举状态时也不方便记录.因为每一颗棋子都只有两种状态,所以可以用二进制0和1表示每一个棋子的状态,则棋盘的状态就可以用一个16位的整数唯一

hdu 1429(BFS+状态压缩)

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7895    Accepted Submission(s): 2795 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这 次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了