• <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>
            付翔的專欄
            在鄙視中成長 記錄成長的點滴
            posts - 106,  comments - 32,  trackbacks - 0
            /*
             *騎士巡游 能否不重復(fù)走慢整個棋盤 數(shù)據(jù)結(jié)構(gòu)P63
             *algorithm :騎士總是移向出口最少且沒有到達的方塊
             * 試做用類來做
             * 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;//標記為已經(jīng)走過
                        
            //kcanmove[curx][cury] = -1;//為不可達
                        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;
            }

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

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

            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            CSDN - 我的blog地址

            博客

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            国产福利电影一区二区三区久久老子无码午夜伦不 | 香蕉久久夜色精品国产2020| 国产福利电影一区二区三区久久久久成人精品综合 | 久久久久亚洲精品无码网址| 久久久亚洲AV波多野结衣| 996久久国产精品线观看| 国内精品伊人久久久久妇| 久久99国产综合精品免费| 久久午夜免费视频| 97久久久精品综合88久久| 久久无码精品一区二区三区| 久久人人爽人人爽人人片AV不| 一级做a爰片久久毛片看看| 久久久久久一区国产精品| 成人精品一区二区久久久| 久久精品中文字幕一区| 国产国产成人久久精品| 性做久久久久久久| 久久精品中文騷妇女内射| 国内精品久久久久久久久电影网| 欧美久久一级内射wwwwww.| 狠狠人妻久久久久久综合| 99久久精品无码一区二区毛片| 久久久无码精品亚洲日韩按摩 | 国产精品丝袜久久久久久不卡 | 大蕉久久伊人中文字幕| 久久亚洲精精品中文字幕| 久久天天躁狠狠躁夜夜2020| 久久99国产精品一区二区| 久久青草国产精品一区| 国产激情久久久久影院| 97久久超碰国产精品2021| 日韩人妻无码一区二区三区久久| 久久久久久av无码免费看大片| 中文精品久久久久国产网址| 久久精品免费观看| 久久精品国产只有精品2020| 国产 亚洲 欧美 另类 久久| 欧美精品一区二区精品久久| 嫩草影院久久国产精品| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 |