eight digits

eight digits

In a 3×3 grid, the 8 numbers from 1 to 8 and an x happen to be distributed in this 3×3 grid without duplication or omission.

For example:
1 2 3
x 4 6
7 5 8
During the game, you can swap x with its number in one of the four directions up, down, left, or right (if it exists).

Our purpose is to make the grid into the following arrangement (called the correct arrangement) through the exchange:
1 2 3
4 5 6
7 8 x
For example, the graphics in the example can be arranged correctly by exchanging x with the numbers in the three directions of right, down and right.

The exchange process is as follows:

1 2 3 1 2 3 1 2 3 1 2 3
x 4 6 4 x 6 4 5 6 4 5 6
7 5 8 7 5 8 7 x 8 7 8 x
Now, given you an initial grid, please find out how many exchanges are needed to get the correct arrangement.

input format
The input occupies one line, and the 3×3
  The initial mesh of is drawn.

For example, if the initial grid looks like this:

1 2 3
x 4 6
7 5 8
Then the input is: 1 2 3 x 4 6 7 5 8

output format
The output occupies one line and contains an integer indicating the minimum number of exchanges

If no solution exists, output −1

Input sample:
2 3 4 1 5 x 7 6 8
output sample
19

Solution

The code above is a C++ program that solves the eight digits puzzle using breadth-first search (BFS). The eight digits puzzle is a sliding puzzle that consists of a 3x3 grid of numbered tiles and an empty space. The objective of the puzzle is to rearrange the tiles so that they are in numerical order, with the empty space in the bottom-right corner.

The program starts by defining two integer arrays dx and dy of size 4, which represent the four possible directions that can be taken to move the empty space. The dx array stores the horizontal offsets for each direction, and the dy array stores the vertical offsets.

Next, the program defines a function named bfs that implements the BFS algorithm to solve the eight digits puzzle. The function initializes a queue of strings q with the initial state of the puzzle, and a map of strings to integers d to store the shortest distance from the initial state to each state in the puzzle.

The bfs function then enters a loop that continues until the queue is empty. In each iteration of the loop, the function dequeues the front element of the queue and iterates over its neighbors. For each neighbor that has not been visited yet, the function computes the new state of the puzzle by swapping the empty space with the neighboring tile, and checks if the new state has been visited before. If the new state has not been visited before, the function sets its distance to the initial state to the distance of the current state plus one, and enqueues it to the back of the queue.

After the BFS algorithm has finished, the bfs function returns the shortest distance from the initial state to the solved state of the puzzle, which is stored in the d map. The program then prints the shortest distance to the standard output.

In the main function, the program reads in the initial state of the puzzle from the standard input as a string of 9 characters. It then calls the bfs function to solve the eight digits puzzle using BFS, and prints the shortest distance to the standard output.

Overall, the program solves the eight digits puzzle using BFS by computing the shortest distance from the initial state to the solved state of the puzzle, and prints the shortest distance to the standard output.

#include <bits/stdc++.h>

using namespace std;

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};


int bfs(string state) {
    queue<string> q;
    unordered_map<string, int> d;
    d[state] = 0;
    q.push(state);
    string end = "12345678x";
    while (q.size()) {
        auto t = q.front();
        q.pop();
        if (t == end) return d[t];
        int distance = d[t];
        int k = t.find("x");
        int row = k / 3, col = k % 3;
        for (int i = 0; i < 4; i ++ ) {
            int x = row + dx[i], y = col + dy[i];
            if (x >= 0 && x < 3 && y >= 0 && y < 3) {
                swap(t[x * 3 + y], t[k]);
                if (!d.count(t)) {
                    d[t] = distance + 1;
                    q.push(t);
                }
                swap(t[x * 3 + y], t[k]);
            }
        }
    }
    return -1;
}

int main() {
    string state;
    for (int i = 0; i < 9; i ++ ) {
        char c;
        cin >> c;
        state += c;
    }
    cout << bfs(state) << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值