• <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>
            付翔的專欄
            在鄙視中成長 記錄成長的點(diǎn)滴
            posts - 106,  comments - 32,  trackbacks - 0
            /*
             *騎士巡游 能否不重復(fù)走慢整個棋盤 數(shù)據(jù)結(jié)構(gòu)P63
             *algorithm :騎士總是移向出口最少且沒有到達(dá)的方塊
             * 試做用類來做
             * author:fuxiang
            */

            # include
            <stdio.h>
            # include
            <stdlib.h>
            # include
            <iostream>

            using namespace std;
            const int row = 8,col = 8;
            int kmovex[8= {-2,-1,1,2,2,1,-1,-2};
            int kmovey[8= {1,2,2,1,-1,-2,-2,-1};
            class Knight
            {
            public:
                
            //const int row = 8,col = 8;
                Knight(){startx=0,starty=0;}
                Knight(
            int x,int y){startx = x;starty=y;}
                
            void output()
                
            {
                    
            int i,j;
                    process();
                    
            for(i = 0; i < step;i++)
                        printf(
            "step %d : %d %d \n",i,path[i][0],path[i][1]);

                    
            for(i = 0;i<step;i++)
                        kmap[path[i][
            1]][path[i][0]] = i;
                    printf(
            "map of knight \n");
                    
            for(i=0;i<8;i++)
                    
            {
                        
            for(j=0;j<8;j++)
                            printf(
            "%02d ",kmap[i][j]);
                        printf(
            "\n");
                    }

                    printf(
            "kcanmove array is :\n");
                    
            for(i=0;i<8;i++)
                    
            {
                        
            for(j=0;j<8;j++)
                            printf(
            "%02d ",kcanmove[i][j]);
                        printf(
            "\n");
                    }

                }

            private:

                
            int startx,starty,step ;
                
            int visted[row][col];
                
            int kcanmove[row][col];//記錄當(dāng)前位置可以走的方向
                int kmap[row][col];
                
            int path[row*col][2];
                
            bool check(int x,int y)
                
            {
                    
            if(x>=0  && x < 8 && y >=0 && y < 8)
                        
            return true;
                    
            return false;
                }

                
            int CountKnightCanMove(int x,int y)
                
            {
                    
            int c = 0,i;
                    
            for(i =0;i < 8;i++)
                        
            if(check(x+ kmovex[i],y + kmovey[i]) && visted[x+ kmovex[i] ][ y + kmovey[i] ] == 0) c ++;
                    
            return c;
                }

                
            /*是用來找出周圍可走步數(shù)中 具有最小的步數(shù)的下一步 這個函數(shù)是寫的最爛的 當(dāng)然整個程序也不咋的
                
            */

                
            int FindMinOut(int x,int y)
                
            {
                    
            int i,min = -1,flag = 0;
                    
            for(i = 0;i < 8;i++)
                    
            {
                        
            if(visted[x+ kmovex[i]][y + kmovey[i]]!= 0 || kcanmove[x+ kmovex[i] ][ y + kmovey[i]] <= 0)
                            
            continue;
                        
            if(flag == 0
                        
            {
                            
            if(check(x+ kmovex[i],y + kmovey[i]) && visted[x+ kmovex[i]][y + kmovey[i]]==0)//找到第一個符合條件的
                                min = i,flag = 1;
                        }

                        
            else if(kcanmove[x+ kmovex[i] ][ y + kmovey[i] ] <= kcanmove[x+ kmovex[min]][ y + kmovey[min]]
                            
            && check(x+ kmovex[i],y + kmovey[i]) && visted[x+ kmovex[i]][y + kmovey[i]]==0)
                            min 
            = i;
                        
            //if(check(x+ kmovex[i+1],y + kmovey[i+1]) && visted[x+ kmovex[i+1]][y + kmovey[i+1]]==0)
                        
            //    min = i+1;
                    }

                    
            return min;
                }

                
            void init()
                
            {
                    step 
            = 0;
                    
            int i,j;
                    
            //visted[startx][starty] = 1; //放在這里 貌似沒有改變他的值

                    memset(kmap,
            0,sizeof(kmap));
                    memset(visted,
            0,sizeof(visted));
                    memset(path,
            0,sizeof(path));
                    
            for(i = 0; i < 8;i++)
                        
            for(j = 0 ; j < 8;j++)
                            kcanmove[i][j] 
            = CountKnightCanMove(i,j);
                
                }

                
            void process()
                
            {
                    
            int npos = 8;
                    
            int nextstep,curx,cury,tx,ty;
                    init();
                    visted[startx][starty] 
            = 1;//放在這里就好了
                    path[step][0= startx,path[step][1= starty;
                    
            for(step =1;step < 64;step ++)
                    
            {
                        curx 
            = path[step-1][0];
                        cury 
            = path[step-1][1];
                        nextstep 
            = FindMinOut(curx,cury);

            //             for(nextstep = 0; nextstep<8;nextstep++)
            //                 if(check(curx+kmovex[nextstep],cury + kmovey[nextstep]) &&
            //                     visted[curx+kmovex[nextstep]][cury + kmovey[nextstep]] == 0)
            //                     break;
                        if(nextstep == -1 || nextstep == 8 ) {printf("Knight is over\n");break;}
                         curx 
            += kmovex[nextstep];cury += kmovey[nextstep];
                        visted[curx][cury] 
            = 1;//標(biāo)記為已經(jīng)走過
                        
            //kcanmove[curx][cury] = -1;//為不可達(dá)
                        path[step][0= curx;
                        path[step][
            1= cury;

                        
            //更新
                        for(int i = 0; i < 8; i ++)
                        
            {
                            tx 
            = curx + kmovex[i];
                            ty 
            = cury + kmovey[i];
                            
            if(check(tx,ty) == falsecontinue;
                            kcanmove[tx][ty] 
            --;
                            
            if(kcanmove[tx][ty] <  0)
                                kcanmove[tx][ty] 
            = 0;
                            
            //CountKnightCanMove(curx + kmovex[nextstep]+kmovex[i],cury + kmovey[nextstep]+kmovey[i]);
                        }


                    }


                }




            }
            ;
            int main()
            {
                
            int x,y;
                scanf(
            "%d%d",&x,&y);
                Knight knight(x,y);
                knight.output();
                
            return 0;
            }

            /*
            這個程序 最開始是一口氣寫完 但是運(yùn)行就發(fā)現(xiàn)有很多錯誤 改的時間也很多 也許是很久沒有寫程序的緣故吧

            */
            posted on 2011-02-09 16:05 付翔 閱讀(275) 評論(0)  編輯 收藏 引用 所屬分類: ACM 數(shù)據(jù)結(jié)構(gòu)

            <2010年7月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            CSDN - 我的blog地址

            博客

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久国产精品无码一区二区三区| 91精品国产高清久久久久久io| 免费精品99久久国产综合精品 | 久久精品免费一区二区| 91秦先生久久久久久久| 久久超碰97人人做人人爱| 午夜精品久久久久久中宇| 亚洲综合久久久| 欧美亚洲国产精品久久久久| 久久e热在这里只有国产中文精品99| 久久久精品一区二区三区| AV色综合久久天堂AV色综合在 | 奇米综合四色77777久久| 久久经典免费视频| 亚洲伊人久久大香线蕉综合图片| 亚洲伊人久久综合影院| 久久精品桃花综合| 精品国产青草久久久久福利| 久久久久久伊人高潮影院| 超级97碰碰碰碰久久久久最新| 日本WV一本一道久久香蕉| 久久亚洲国产成人影院| 久久这里只有精品首页| 亚洲精品无码久久千人斩| 久久久久人妻精品一区| www久久久天天com| 精品久久久久久久久久中文字幕 | 久久综合伊人77777麻豆| 欧美一级久久久久久久大片| 性做久久久久久久久浪潮| 国产精品乱码久久久久久软件| 人妻精品久久无码区| 天天久久狠狠色综合| 色婷婷噜噜久久国产精品12p| 久久人人青草97香蕉| 久久久久久毛片免费播放| 曰曰摸天天摸人人看久久久| 亚洲精品无码久久久| 国产亚洲综合久久系列| 久久精品视屏| 亚洲AV无码一区东京热久久 |