HDU (3567): Eight Digital issues (upgrade version)--double BFS

Source: Internet
Author: User
Tags cmath

Topic Link: http://acm.hdu.edu.cn/showproblem.php?pid=3567

With the foundation of HDU 1043, this problem becomes easier, if you use a * algorithm, feeling the path is not too good to print, so give up, see a kind of like and 10,431 of the table method, but not how to understand, also gave up, although to print the minimum dictionary order, one-way BFS will obviously tle, So I can only use two-way BFS.

Fortunately, the input of this problem to ensure that there is a solution, otherwise you have to use the reverse number of eight digital theorem to determine whether there is a solution, that is: the same number of reverse order has a solution, otherwise no solution.

The overall code and 1043 of the almost, just the path of the print processing. Although seemingly simple, but still, WA a lot of times.

The first time WA, found that the operation of 0 is the output error.

WA, the second time, found that to print the minimum dictionary order, only the forward BFS output, even if the reverse traversal is the path

WA, the third time, found that in the direction of traversal, a point if it has been backward traversal, must operate on it, but also to do a comparison operation to see if it is the smallest dictionary order

Mainly this three times, later on AC, Time 1.3s, and great God's gap is not small.

Bidirectional BFS Code:

(See some people use a queue, the code looks simpler, but the principle is the same, and the accompanying data generation code)

#include <iostream> #include <sstream> #include <cstdio> #include <cstring> #include <cmath

> #include <algorithm> #include <queue> using namespace std;
int fac[]={1,1,2,6,24,120,720,5040,40320,362880};
int vis1[363000],vis2[363000];
int ed[]={1,2,3,4,5,6,7,8,0};
    string path[363000];//log path int cantor (int s[]) {//Cantor expanded, state compressed int sum=0;
        for (int i=0;i<9;i++) {int cnt=0;
        for (int j=i+1;j<9;j++) if (s[j]<s[i]) cnt++;
    SUM+=CNT*FAC[9-I-1];
return sum;
    } struct node{int s[9];

int cur,n;//same * algorithm int deep;//record level}t[2];
int d[][2]={{1,0},{0,-1},{0,1},{-1,0}}; Char dr1[]= "DLRU", dr2[]= "Urld";//here the opposite direction is traversed, the path is also opposite void BFs (node &t1,node &t2) {//You can see that the double BFS code is almost symmetrical, vis two
    , queue two memset (vis1,0,sizeof (VIS1));
    memset (vis2,0,sizeof (VIS2));
    if (T1.N==T2.N) {Cout<<0<<endl<<endl;return;}
    Queue<node> q,qq;
    Q.push (t1);
    Vis1[t1.n]=1; path[t1.n]="";
    Qq.push (T2);
    Vis2[t2.n]=1;
    Path[t2.n]= "";
            int l=0;//record level, since it is two-way, it must be your first layer, I order the first layer of the while (!q.empty () | |!qq.empty ()) {while (Q.front (). deep==l) {
            Node Tmp=q.front ();
            Q.pop ();
            int x=tmp.cur/3,y=tmp.cur%3;
                for (int p=0;p<4;p++) {int tx=x+d[p][0],ty=y+d[p][1];
                if (tx<0 | | ty<0 | | tx>2 | | ty>2) continue;
                Node tmp2=tmp;
                Tmp2.cur=tx*3+ty;
                Swap (tmp2.s[tmp.cur],tmp2.s[tmp2.cur]);
                Tmp2.n=cantor (TMP2.S);
                tmp2.deep=tmp.deep+1; if (VIS2[TMP2.N]) {//If solution is found, print path cout<< (int) path[tmp2.n].size () + (int) path[tmp.n].size () +1<<en
                    dl;//Print Length reverse (path[tmp2.n].begin (), Path[tmp2.n].end ());
                    cout<<path[tmp.n]<<dr1[p]<<path[tmp2.n]<<endl;
                Return } if (vis1[TMP2.N]) continue;
                Vis1[tmp2.n]=1;
                PATH[TMP2.N]=PATH[TMP.N];
                PATH[TMP2.N]+=DR1[P];

            Q.push (TMP2);
            } while (Qq.front (). deep==l) {node Tmp=qq.front ();
            Qq.pop ();
            int x=tmp.cur/3,y=tmp.cur%3;
                for (int p=0;p<4;p++) {int tx=x+d[p][0],ty=y+d[p][1];
                if (tx<0 | | ty<0 | | tx>2 | | ty>2) continue;
                Node tmp2=tmp;
                Tmp2.cur=tx*3+ty;
                Swap (tmp2.s[tmp.cur],tmp2.s[tmp2.cur]);
                Tmp2.n=cantor (TMP2.S);
                tmp2.deep=tmp.deep+1;
                The following if notice, we can not print like hang electricity 1043, because we are not sure that the path of the dictionary order is the smallest,//to find the smallest dictionary order, but also by the forward BFs OK, we here skip to its operation, by the next forward BFS print
                if (VIS1[TMP2.N]) continue; Here's the if note, because the comparison must be in the reverse direction traversal,&&, otherwise WA if (VIS2[TMP2.N] && path[tmp2.n][(int) path[tmp2.n].s Ize () -1]<dr2[p]) continue;
                Vis2[tmp2.n]=1;
                PATH[TMP2.N]=PATH[TMP.N];
                PATH[TMP2.N]+=DR2[P];
            Qq.push (TMP2); } l++;//level +1} int main () {//freopen ("C:\\Documents and Settings\\All Users\\ desktop \\in.txt", "R",
    STDIN);
    Freopen ("C:\\Documents and Settings\\All Users\\ desktop \\out2.txt", "w", stdout);
    int t;cin>>t;
        for (int p=1;p<=t;p++) {char ch;
            for (int j=0;j<2;j++) for (int i=0;i<9;i++) {cin>>ch;
                if (ch== ' X ') {ch= ' 0 ';
            T[j].cur=i;
        } t[j].s[i]=ch-' 0 ';
        T[0].n=cantor (T[0].S); t[0].deep=0;

        T[1].n=cantor (T[1].S); t[1].deep=0;
        printf ("Case%d:", p);
    BFS (t[0],t[1]);
return 0; }/* Data generation: #include <iostream> #include <queue> #include <cstdio> #include <cstring> #include &l T;algorithm> #include <ctime>

#include <vector> #include <cmath> #include <cstdlib> using namespace std;
const int MAXN=1E3+10;
int F[MAXN],G[MAXN];
    void random (int t) {int i,j,k,m,num=0;
    for (i=0;i<t;i++) f[i]=i;
    m=t;
        for (i=0;i<t;i++) {Num=rand ()%m;
        G[i]=f[num];
        F[NUM]=F[M-1];
    m--;
    an int judge () {int i,j,k=0;
        for (i=0;i<9;i++) {//printf ("%c\n", E.c[i]);
        if (g[i]==0) continue;
            for (j=0;j<i;j++) {if (g[j]==0) continue;
        if (G[j]>g[i]) k++;
} return k%2;
    int main () {freopen ("C:\\Documents and Settings\\All Users\\ desktop \\in.txt", "w", stdout);
    Srand (Time (NULL));
    int i,j,k,n;
    printf ("200\n");
        for (i=0;i<200;i++) {random (9);
        K=judge ();
            for (j=0;j<9;j++) {if (g[j]==0) printf ("X");
        else printf ("%d", g[j]);
 printf ("\ n");       do {random (9);
        }while (K!=judge ());
            for (j=0;j<9;j++) {if (g[j]==0) printf ("X");
        else printf ("%d", g[j]);
    printf ("\ n");
 }
}

*/


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.