"CodeVS1225" Eight digital puzzles

Source: Internet
Author: User

Description

Yours and Zero are studying a * heuristic algorithm. Get a classic A * question, but they won't do it, please help them.
Problem description

On the 3x3 chessboard, there are eight pieces, each of which is marked with a number 1 to 8. A space is left in the chessboard, and the space is indicated by the ". The pieces around the space can be moved to a space. The problem to solve is: give an initial layout (initial state) and a target layout (in order to make the topic simple, set the target State to 123804765), find a minimum step of the moving method, the transition from the initial layout to the target layout.

Input

Enter the initial status, a row of nine digits, the space is indicated by 0

Output

Only one row, the row has a number that represents the minimum number of moves required from the initial state to the target State (no special unreachable target State data in the test data)

Sample Input

283104765

Sample Output

4

HINT

See questions

Exercises

BFS Hash Weight

#include <iostream>#include<cstdio>#defineN 500000using namespacestd;structnode{intmp[4][4];} A[n];intg[4][4]={{0,0,0,0},{0,1,2,3},{0,8,0,4},{0,7,6,5}};intxx[4] = {0,0,1,-1};intyy[4] = {1,-1,0,0};inthash[3733800];intStep[n];inth,t=1, Flag;intCheck () { for(intI=1; i<=3; i++)         for(intj=1; j<=3; j + +)        if(A[t].mp[i][j]! =G[i][j])return 0; return 1;}intHash () {ints =0, k =1;  for(intI=1; i<=3; i++)         for(intj=1; j<=3; j + +) s+ = a[t].mp[i][j]*k,k*=7; S%=3733799; if(!hash[s]) {Hash[s] =1;return 1;} return 0;}intPdintXinty) {    if(x && x<=3&& y && y<=3)return 1; return 0;}voidMoveintXinty) {     for(intI=0;i<4; i++)    {        intp = x+xx[i], q = y+Yy[i]; if(PD (P,Q)) { for(intj=1; j<=3; j + +)                 for(intk=1; k<=3; k++) A[t].mp[j][k]=A[h].mp[j][k];            Swap (a[t].mp[x][y],a[t].mp[p][q]); Step[t]= Step[h] +1; if(check ()) {cout<<step[t]; flag =1;return;} if(Hash ()) t++; }    }}voidsearch () { while(h<t) { for(intI=1; i<=3; i++)             for(intj=1; j<=3; j + +)            {                if(A[h].mp[i][j] = =0) Move (I,J); if(flag)return; } h++; }}intMain () {stringstr; CIN>>str;  for(intI=1; i<=3; i++)         for(intj=1; j<=3; j + +) a[0].MP[I][J] = str[(i-1)*3+j-1]-'0'; Search ();}

"CodeVS1225" Eight digital puzzles

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.