• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            心如止水
            Je n'ai pas le temps
            posts - 400,comments - 130,trackbacks - 0
            Bfs + Hash is ok.
            I found that the functions named mem... are veru useful.
            Here is my code:
            #include<iostream>
            #include
            <string.h>
            #define maxn 1000000
            using namespace std;
            const int xd[]={-1,1,0,0},yd[]={0,0,-1,1};
            int st[maxn][3][3],front=1,rear=1,aim[3][3]={{1,2,3},{8,0,4},{7,6,5}},dist[maxn];
            int head[maxn],next[maxn];

            long hash(long x)
            {
                
            long re=0;
                
            for(long i=0;i<3;i++)
                    
            for(long j=0;j<3;j++)
                        re
            =(re*10+st[x][i][j])%maxn;
                
            return re;
            }
            void insert(long x)
            {
                
            long h=hash(x),p=head[h];
                
            while(p)
                {
                    
            if(memcmp(st[p],st[x],sizeof(st[p]))==0return;
                    p
            =next[p];
                }
                next[x]
            =head[h];head[h]=x;
                rear
            ++;dist[rear]=dist[front]+1;
            }
            long bfs()
            {
                
            while(front<=rear)
                {
                    
            if(memcmp(st[front],aim,sizeof(aim))==0return front;
                    
            //  Found answer , return the position of the final state
                    long x,y;
                    
            bool find_zero=false;
                    
            for(x=0;x<3&&!find_zero;x++)
                        
            for(y=0;y<3&&!find_zero;y++)
                            
            if(st[front][x][y]==0)
                                find_zero
            =true;
                    x
            --;y--;
                    
            //  Find the position of figure zero
                    for(long k=0;k<4;k++)
                    {
                        
            long newx=x+xd[k],newy=y+yd[k];
                        
            if(newx>=0&&newx<3&&newy>=0&&newy<3)
                        {
                            memcpy(st[rear
            +1],st[front],sizeof(st[front]));
                            st[rear
            +1][x][y]=st[rear+1][newx][newy];
                            st[rear
            +1][newx][newy]=0;
                            
            //  Produce new states
                            insert(rear+1);
                            
            //  Try to insert
                        }
                    }
                    front
            ++;
                }
                
            return -1;
            }
            int main()
            {
                
            string s;
                cin
            >>s;
                
            for(long i=0;i<3;i++)
                    
            for(long j=0;j<3;j++)
                        st[
            1][i][j]=s.at(3*i+j)-'0';
                dist[
            1]=0;
                
            //  Input
                long ans=bfs();
                
            //  Bfs
                cout<<dist[ans]<<endl;
                
            //  Output
            return 0;
            }


            posted on 2010-09-22 10:26 lee1r 閱讀(406) 評論(0)  編輯 收藏 引用 所屬分類: 題目分類:搜索
            亚洲午夜精品久久久久久app| 久久亚洲私人国产精品vA| 国产精品久久久久一区二区三区| 中文字幕一区二区三区久久网站 | 无码人妻久久一区二区三区蜜桃| 久久人人爽人人爽人人av东京热 | 久久九九久精品国产免费直播| 久久影视国产亚洲| 国产精品久久久久9999高清| 国产2021久久精品| 欧美精品久久久久久久自慰| 久久久精品久久久久久| 97久久超碰成人精品网站| 久久久久九国产精品| 久久人爽人人爽人人片AV| 色播久久人人爽人人爽人人片aV| 久久天天躁狠狠躁夜夜网站| 久久久久国产精品麻豆AR影院| 乱亲女H秽乱长久久久| 伊人伊成久久人综合网777| 久久青青草原综合伊人| 无码专区久久综合久中文字幕| 国产精品免费看久久久香蕉| 国内精品久久久久影院一蜜桃| 久久九九久精品国产免费直播| 久久久久久无码国产精品中文字幕| 成人久久综合网| 国产成年无码久久久久毛片| 久久综合亚洲色一区二区三区| 久久久久女教师免费一区| 国产99久久久久久免费看| 大伊人青草狠狠久久| 高清免费久久午夜精品| 99久久99这里只有免费费精品 | 国产成人久久精品麻豆一区 | 中文字幕精品久久| 久久亚洲AV永久无码精品| 欧美国产精品久久高清| 一本大道久久东京热无码AV| 91麻豆国产精品91久久久| 久久无码AV中文出轨人妻|