UVA 10085 the most distant state

Source: Internet
Author: User

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


However problem is a bit different. In this problem, given an initial state of the the puzzle your be asked to discover a goal state which was the most distant (i n Terms of number of moves) of the states reachable from the given state.
Input
The first line of the input file contains a integer representing the number of the test cases to follow. A Blank line follows the 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 all test case.
Output
For each test case first output the puzzle number. The next 3 lines would contain 3 integers each representing one of the most distant states reachable from the given state. The next line would contain the shortest sequence of moves that would transform the given state to this 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 the test case output a 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



English:
Give you a eight digital starting state, and then ask you the distance from this eight digital the furthest state is what.


#include <bits / stdc ++. h>
using namespace std;
struct node
{
    string st;
    vector <char> dir;
};
unordered_map <string, node> ums;
int t;
node Down (node n, int pos)
{
    if (pos> = 6)
    {
        n.st = "#";
        return n;
    }
    swap (n.st [pos], n.st [pos + 3]);
    n.dir.push_back ('D');
    return n;
}
node Up (node n, int pos)
{
    if (pos <3) // 0 on the first line
    {
        n.st = "#";
        return n;
    }
    swap (n.st [pos], n.st [pos-3]);
    n.dir.push_back ('U');
    return n;
}
node Left (node n, int pos) // cannot be on the far right
{
    if (pos% 3 == 2)
    {
        n.st = "#";
        return n;
    }
    swap (n.st [pos], n.st [pos + 1]);
    n.dir.push_back ('R');
    return n;
}
node Right (node n, int pos)
{
    if (pos% 3 == 0)
    {
        n.st = "#";
        return n;
    }
    swap (n.st [pos], n.st [pos-1]);
    n.dir.push_back ('L');
    return n;
}
node bfs (node start, string state)
{
    queue <node> Q;
    Q.push (start);
    ums [state] = start;
    node ans;
    int _max = 0;
    while (! Q.empty ())
    {
        node head = Q.front ();
// cout << head.st << endl;
        if (head.dir.size ()> _ max)
// if (head.dir.size () == 31) // If the number of steps equals 31, break returns
        {
            ans = head;
// break;
            _max = head.dir.size ();
        }
        Q.pop ();
        int zero = head.st.find ('0');
        node u = Up (head, zero);
        node d = Down (head, zero);
        node l = Left (head, zero);
        node r = Right (head, zero);
        if (u.st! = "#")
        {
            if (ums.find (u.st) == ums.end ())
            {
                ums.insert (make_pair (u.st, u));
                Q.push (u);
            }
        }
        if (d.st! = "#")
        {
            if (ums.find (d.st) == ums.end ())
            {
                ums.insert (make_pair (d.st, d));
                Q.push (d);
            }
        }
        if (l.st! = "#")
        {
            if (ums.find (l.st) == ums.end ())
            {
                ums.insert (make_pair (l.st, l));
                Q.push (l);
            }
        }
        if (r.st! = "#")
        {
            if (ums.find (r.st) == ums.end ())
            {
                ums.insert (make_pair (r.st, r));
                Q.push (r);
            }
        }
    }
    return ans;
}
int main ()
{
    ios :: sync_with_stdio (false);
    cin >> t;
    int k = 1;
    while (t--)
    {
        string st;
        node start;
        ums.clear ();
        for (int i = 1; i <= 9; i ++)
        {
            string s;
            cin >> s;
            st + = s;
        }
        start.st = st;
        node ans = bfs (start, st);
        cout << "Puzzle #" << k ++ << endl;
        for (int i = 0; i <ans.st.size (); i ++)
        {
            cout << ans.st [i];
            if ((i + 1)% 3 == 0)
                cout << endl;
            else
                cout << "";
        }
// cout << ans.dir.size () << endl;
        for (auto c: ans.dir)
            cout << c;
        cout << endl;
        cout << endl;
    }
    return 0;
}


answer:

To find the farthest state, use Guangsou to search for the last state in the queue, which is the farthest state. It can be implemented by using the Kang expansion or hash function.

I thought it would time out after writing the code. As a result, the time requirement on the title is 13s, halo ~

However, by writing two sets of data myself, I found a rule for this problem. That is, the farthest path length must be equal to 31 steps, but there is no proof. I guessed it myself. After using this conclusion, it is still ac. Because the eight-digit problem can be solved using heuristic search, I feel that this question is absolutely There can be a lot of room for optimization. After reading the list, there are a lot of 0s, but I haven't figured it out yet -_-


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.