[隐式图搜索]The most distant state UVA10085

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


题意:给出一个九宫格状态,移动其中的0这一格,求距离这个九宫格步数最远的一个状态。

思路:完全没有想到比较好的算法,只能用bfs把所有的状态图算出来,并且求出他们距离开始的状态有多远,题目中限时是13.333秒,限时有点宽,所以是可行的,我写的代码运行时间是4.2秒左右,实在是太长了,应该有很多可以优化的地方。在记录从初始状态到目标状态的步骤时,我没想到比较好的方法,开了一个极其大的string数组来保存每个状态的步骤,这里应该可以有很大的优化。

#include<iostream>
#include<cstring>
#include<set>
#include<string>

using namespace std;

typedef int state[9];
state st[362882];
int dist[362882];
string str[362882];//记录初始状态到达当前状态的步骤
const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};
const char ch[]={'U','D','L','R'};
set<int> vis;
int t,k,front,rear,maxlen;
int goal;

int try_to_insert(int pos)//判断重复
{
    int tmp=0;
    for(int i=0;i<9;i++) tmp=tmp*10+st[pos][i];//把状态图计算为一个9位的独一无二的数字存在set中
    if(vis.count(tmp)) return 0;
    vis.insert(tmp);
    return 1;
}

void bfs()
{
    vis.clear();
    front=1,rear=2;
    while(front<rear)
    {
        state &p=st[front];
        int i,j,x,y;
        for(i=0;i<9;i++) if(p[i]==0) break;
        x=i/3,y=i%3;
        int tag=0;
        for(j=0;j<4;j++)
        {
            int nx=x+dx[j];
            int ny=y+dy[j];
            if(nx>=0&&nx<3&&ny>=0&&ny<3)
            {
                state &v=st[rear];
                memcpy(&v,&p,sizeof(p));
                v[i]=v[3*nx+ny];
                v[3*nx+ny]=0;
                dist[rear]=dist[front]+1;
                str[rear]=str[front]+ch[j];
                if(try_to_insert(rear))
                {
                    if(dist[rear]>maxlen)
                    {
                        maxlen=dist[rear];
                        goal=rear;
                    }
                    rear++;
                }
            }
        }
        front++;
    }
}


int main()
{
    cin>>t;
    for(k=1;k<=t;k++)
    {
        int i,j;
        memset(dist,0,sizeof(dist));
        for(i=0;i<362882;i++)
            str[i]="";
        cout<<"Puzzle #"<<k<<endl;
        for(i=0;i<9;i++) cin>>st[1][i];
        maxlen=0;
        bfs();
        //cout<<maxlen<<endl;
        cout<<st[goal][0]<<" "<<st[goal][1]<<" "<<st[goal][2]<<endl;
        cout<<st[goal][3]<<" "<<st[goal][4]<<" "<<st[goal][5]<<endl;
        cout<<st[goal][6]<<" "<<st[goal][7]<<" "<<st[goal][8]<<endl;
        cout<<str[goal]<<endl;
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值