POJ:1077 Eight(双向BFS+判重)

来源:互联网 发布:九州风神知乎 编辑:程序博客网 时间:2024/05/13 13:24

HDU也有这道题但是数据比较强,我的代码过不了(内存超限了。。)。只能过POJ的。。

 

这是某大神的。。

http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html

 

 

我的代码。。

用了stl这是速度慢的一个重要原因。。

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<map>#include<queue>#include<algorithm>using namespace std;struct State{    int grid[5][5];    int x,y;    string path;};int Convers(State t){    int n=0;    for(int i=0; i<3; ++i)        for(int j=0; j<3; ++j)            n=n*10+t.grid[i][j];    return n;}int M[5][3]= {{-1,0},{1,0},{0,-1},{0,1}};map<int,bool> mp1,mp2;map<int,string> ph1,ph2;int main(){    char str[100];    while(gets(str))    {        State st;        for(int i=0,j=0; str[i]; ++i)        {            if(isdigit(str[i]))            {                st.grid[j/3][j%3]=str[i]-'0';                j++;            }            else if(str[i]=='x')            {                st.grid[j/3][j%3]=0;                st.x=j/3;                st.y=j%3;                j++;            }        }        mp1.clear();        mp2.clear();        ph1.clear();        ph2.clear();        mp1[Convers(st)]=true;        bool ok=false;        string ans;        if(Convers(st)!=123456780)        {            queue<State> q1,q2;            q1.push(st);            State ed;            for(int i=0; i<8; ++i)                ed.grid[i/3][i%3]=i+1;            ed.grid[2][2]=0;            ed.x=2;            ed.y=2;            q2.push(ed);            mp2[Convers(ed)]=true;            bool a1=false,a2=false;            while(!q1.empty()||!q2.empty())            {                if(ok) break;                State t1,t2;                if(!q1.empty())                {                    t1=q1.front();                    q1.pop();                }                else a1=true;                if(!q2.empty())                {                    t2=q2.front();                    q2.pop();                }                else a2=true;                for(int i=0; i<4&&!ok&&!a1; ++i)                {                    State t=t1;                    t.x+=M[i][0];                    t.y+=M[i][1];                    if(t.x<0||t.x>3||t.y<0||t.y>3) continue;                    swap(t.grid[t.x][t.y],t.grid[t1.x][t1.y]);                    int flag=Convers(t);                    if(mp1[flag]) continue;                    else                    {                        if(i==0) t.path+='u';                        else if(i==1) t.path+='d';                        else if(i==2) t.path+='l';                        else if(i==3) t.path+='r';                        if(mp2[flag])                        {                            ans=t.path+ph2[flag];                            ok=true;                        }                        else                        {                            mp1[flag]=true;                            ph1[flag]=t.path;                            q1.push(t);                        }                    }                }                for(int i=0; i<4&&!ok&&!a2; ++i)                {                    State t=t2;                    t.x+=M[i][0];                    t.y+=M[i][1];                    if(t.x<0||t.x>3||t.y<0||t.y>3) continue;                    swap(t.grid[t.x][t.y],t.grid[t2.x][t2.y]);                    int flag=Convers(t);                    if(mp2[flag]) continue;                    else                    {                        if(i==0) t.path='d'+t.path;                        else if(i==1) t.path='u'+t.path;                        else if(i==2) t.path='r'+t.path;                        else if(i==3) t.path='l'+t.path;                        if(mp1[flag])                        {                            ans=ph1[flag]+t.path;                            ok=true;                        }                        else                        {                            mp2[flag]=true;                            ph2[flag]=t.path;                            q2.push(t);                        }                    }                }            }        }        else ok=true;        if(ok) cout<<ans<<endl;        else cout<<"unsolvable"<<endl;    }    return 0;}

原创粉丝点击