• <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 閱讀(397) 評論(0)  編輯 收藏 引用 所屬分類: 題目分類:搜索
            精品久久久久久久久午夜福利 | 色综合合久久天天综合绕视看| 国产激情久久久久久熟女老人| 亚洲精品无码久久久久久| 久久久久久综合网天天| 久久婷婷五月综合97色| 老司机国内精品久久久久| 久久久久亚洲精品中文字幕| 免费久久人人爽人人爽av| 久久99精品国产麻豆 | 亚洲日本久久久午夜精品| 伊人久久精品无码二区麻豆| 亚洲乱亚洲乱淫久久| 人妻无码精品久久亚瑟影视| 亚洲嫩草影院久久精品| 亚洲香蕉网久久综合影视| 精品国产青草久久久久福利| 久久影院综合精品| 亚洲性久久久影院| 免费国产99久久久香蕉| 久久SE精品一区二区| 欧美精品一区二区久久| 久久91综合国产91久久精品| 久久成人国产精品免费软件| 久久久精品久久久久久| 51久久夜色精品国产| 欧美伊香蕉久久综合类网站| 久久综合九色综合网站| 亚洲精品高清国产一线久久 | 国产91久久精品一区二区| 一本色道久久88精品综合| 国产精品久久久久免费a∨| 久久本道久久综合伊人| 国产激情久久久久影院| 久久精品国产一区| 久久久久亚洲av无码专区喷水| 97精品国产97久久久久久免费 | 久久久久久久久波多野高潮| 欧美一级久久久久久久大| 久久九九免费高清视频| 精品久久久无码中文字幕天天 |