Blue Bridge Cup previous test nine rearrangement classic eight digital problem * algorithm + Cantor expansion

Source: Internet
Author: User

Previous questions nine rearrangement time limit: 1.0s memory limit: 256.0MBThe problem is described in the nine of the first figure below, with a 1~8 digital card and a grid empty. The cards in the lattice adjacent to the empty lattice can be moved to a space. After several moves, the situation shown in the second diagram can be formed.

We recorded the first picture as: 12345678.
The second picture of the situation is recorded as: 123.46758
It is clear that the numbers are recorded in order from top to bottom, left to right, and spaces are marked as periods.
The task of this topic is to know the initial state and final state of the nine, and to find the minimum number of steps that can be reached. If no number of steps are reached, output-1. Input format input The first line contains the initial state of the nine, and the second line contains the final state of the nine. The output format outputs a minimum number of steps, or 1 if no scheme exists. Sample input 12345678.
123.46758 Example Output 3 sample input 13524678.
46758123. Sample Output 22
If you have no idea about this problem, I suggest you look at my blog: HDU Eight digital, which contains information on eight digital issues.judge There is no solution problem: According to the number of reverse order directly judge there is no solution, for a eight of digital, sequentially after each is the vacancy and adjacent position to exchange, the study will find that each exchange, reverse order number increases are even, that is, do not change the parity, Therefore, it is only necessary to determine the positive and negative numbers of the initial and target states.
Hash Question: Based on Cantor unfolding

See the code below for a comment:
#include <cstdio> #include <queue> #include <algorithm> #include <cstring> #define MAX 400000using namespace std; int visited[max];int hash[9]={1,1,2,6,24,120,720,5040,40320}; int dir[4][2] = {{ -1,0},{1,0}, {0,-1},{0,1}}; int des[3][3];struct node{int map[3][3]; int x, y; int hash; int g, H, F;bool operator< (const Node &A MP;N1) Const{//return H==n1.h? (G&GT;N1.G):(h>n1.h); return f!=n1.f?f>n1.f:h>n1.h;} BOOL Check () {if (x<0| | y<0 | | x>=3| | Y>=3) {return false;} return true;} Find the hash code for Cantor. Expand void Gethash () {int oth[9], k = 0; for (int i = 0; i < 3; ++i) {for (int j = 0; j < 3; ++j) {oth[k++] = Map[i][j];}} hash = 0; for (int i = 0; i < 9; ++i) {int k = 0, for (int j = 0; J < i; ++j) {if (Oth[i]<oth[j]) {k++;}} Hash + = K*hash[i];}} Ask two points between Manhattan distance void Geth () {int oth[9], k = 0; h = 0; for (int i = 0; i < 3; ++i) {for (int j = 0; j < 3; ++j) {int m = 0, n = 0;for (m = 0; m < 3; ++m) {for (n = 0; n < 3; ++n) {if (map[i][j] = = Des[m][n]) {goto Loop;}}} Loop:h + = ABS (m-i) +abs (N-J);}}} end;//according to the nature of the eight digital, determine whether there is no solution bool judge (node start, node end) {int s[9], st = 0, et = 0, k = 0;for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) {s[k++] =start.map[i][j];}} for (int i = 0, i < 9; ++i) {for (int j = 0; J < i; ++j) {if (S[i]&&s[j]&&s[i]<s[j]) {st++;}}} k = 0; for (int i = 0, i < 3; ++i) {for (int j = 0; j < 3; ++j) {s[k++] = End.map[i][j];}} for (int i = 0, i < 9; ++i) {for (int j = 0; J < i; ++j) {if (S[i]&&s[j]&&s[i]<s[j]) {et++;}}} Return! ((st&1) ^ (et&1));} int AStar (node start) {priority_queue<node> Que;que.push (start); while (!que.empty ()) {Node now = Que.top (); Que.pop (); if (Now.hash = = End.hash) {return visited[now.hash];} for (int i = 0; i < 4; ++i) {Node next=now; Next.x + = Dir[i][0];next.y + dir[i][1];if (!next.check ())//coordinates out of bounds {continue; } swap (Next.map[now.x][now.y],next.map[next.x][next.y]); Next.gethash (); if (visited[next.hash] = = 0)//This figure has not been accessed {next.geth (); next.g++; next.f = Next.g+next.h;visited[next.hash] = next.g; Que.push (next);} Else{if (Next.g+1<visited[next.hash])//If the graph has been accessed, but now the access cost is smaller, then update the access cost to the graph {next.geth (); next.g++; next.f = next.g+ Next.h;visited[next.hash] = NEXT.G; Que.push (next);}}} return-1;} int main () {char line[15];while (line) {Node start;memset (visited,0,sizeof (visited)), for (int i = 0; i < 9; ++i) {i F (line[i]! = '. ') START.MAP[I/3][I%3] = line[i]-' 0 '; else{start.map[i/3][i%3] = 0; start.x = i/3, Start.y = i%3;}} Gets (line), for (int i = 0; i < 9; ++i) {if (line[i]! = '. ') END.MAP[I/3][I%3] = line[i]-' 0 '; elseend.map[i/3][i%3] = 0; if (line[i]! = '. ') DES[I/3][I%3] = line[i]-' 0 '; elsedes[i/3][i%3] = 0;} End.gethash (); end.g = -1;end.geth (); end.f = End.g+end.h; Start.gethash (); Start.geth () start.g = 0; start.f = start.g+st art.h; Visited[start.hash] = = 1; if (!judge (start,end)) {printf (" -1\n"); continue;} if (Start.hash = = End.hash) {printf ("0\n"); continue;} int ans = AStar (start);p Rintf ("%d\n", ans); return 0;}



Blue Bridge Cup previous test nine rearrangement classic eight digital problem * algorithm + Cantor expansion

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.