• <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>

            winlinglin

            螺旋數(shù)組

            今天想練練手,所以寫了個(gè)螺旋數(shù)組:
              1 #include <iostream>
              2 
              3 using namespace std;
              4 
              5 #define MAXSIZE 8
              6 
              7 void left( int& x, int& y )
              8 {
              9     --y;
             10 }
             11 
             12 void right( int& x, int& y )
             13 {
             14     ++y;
             15 }
             16 
             17 void up( int& x, int& y )
             18 {
             19     --x;
             20 }
             21 
             22 void down( int& x, int& y )
             23 {
             24     ++x;
             25 }
             26 
             27 int main()
             28 {
             29     int numbers[MAXSIZE][MAXSIZE];
             30     // 初始化數(shù)組,若數(shù)值為0,則代表還沒有被賦值
             31     forint i = 0; i<MAXSIZE; ++i )
             32         forint j = 0; j<MAXSIZE; ++j )
             33             numbers[i][j] = 0;
             34 
             35     enum Direction{RIGHT,DOWN,LEFT,UP};    // 移動(dòng)方向
             36     int x=0,y=0;
             37     int Next = -1;    // 下一個(gè)位置的值
             38     Direction direct = RIGHT;
             39     int count = MAXSIZE * MAXSIZE;    // 還沒被賦值的數(shù)目
             40     int value = 1;    //將要被賦值的值
             41 
             42     while( count > 0 )
             43     {
             44         Next = numbers[x][y];
             45         if( Next == 0 && x<MAXSIZE && y<MAXSIZE )    // 無障礙,可以賦值
             46         {
             47             numbers[x][y] = value;
             48             // 賦值成功,count減一, value加一
             49             --count;
             50             ++value;
             51 
             52             // 設(shè)置Next
             53             if( direct == RIGHT )
             54                 right( x, y );
             55             else if( direct == DOWN )
             56                 down( x, y );
             57             else if( direct == LEFT )
             58                 left( x, y );
             59             else if( direct == UP )
             60                 up( x, y );
             61         }
             62         else        // 有障礙,要轉(zhuǎn)彎
             63         {
             64             if( direct == RIGHT )    // 若原來方向是右的話,就轉(zhuǎn)彎向下
             65             {
             66                 x = x + 1;
             67                 y = y - 1;
             68                 direct = DOWN;
             69             }
             70             else if( direct == DOWN )    //若原來方向是下的話,就轉(zhuǎn)彎向左
             71             {
             72                 x = x - 1;
             73                 y = y - 1;
             74                 direct = LEFT;
             75             }
             76             else if( direct == LEFT )    //若原來方向是左的話,就轉(zhuǎn)彎向上
             77             {                
             78                 x = x - 1;
             79                 y = y + 1;
             80                 direct = UP;
             81             }
             82             else if( direct == UP)    //若原來方向是上的話,就轉(zhuǎn)彎向右
             83             {                
             84                 x = x + 1;
             85                 y = y + 1;
             86                 direct = RIGHT;
             87             }
             88         }
             89     }
             90 
             91     forint i = 0; i<MAXSIZE; ++i )
             92     {
             93         forint j = 0; j<MAXSIZE; ++j )
             94         {
             95             cout<<numbers[i][j]<<"   ";
             96         }
             97         cout<<endl;
             98     }
             99 
            100     return 0;
            101 }

            posted on 2009-03-05 18:33 wil 閱讀(1133) 評(píng)論(2)  編輯 收藏 引用 所屬分類: 算法

            評(píng)論

            # re: 螺旋數(shù)組 2009-03-06 09:02 成大才子

            呵呵,有意思  回復(fù)  更多評(píng)論   

            # re: 螺旋數(shù)組 2010-03-18 16:16 后現(xiàn)代

            @成大才子
            思路清晰,可改性強(qiáng)。 強(qiáng)  回復(fù)  更多評(píng)論   


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2009年3月>
            22232425262728
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            文章分類

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            狠狠色丁香久久综合五月| 思思久久99热免费精品6| MM131亚洲国产美女久久| 97久久香蕉国产线看观看| 久久99精品国产麻豆宅宅| 久久男人中文字幕资源站| 久久精品国产亚洲AV麻豆网站| 99久久精品国产毛片| 无码国内精品久久人妻蜜桃| 国产精品热久久无码av| 欧美熟妇另类久久久久久不卡| 久久国产午夜精品一区二区三区| 久久精品久久久久观看99水蜜桃| 99久久精品免费看国产一区二区三区| 久久伊人五月丁香狠狠色| 激情综合色综合久久综合| 9久久9久久精品| 久久精品久久久久观看99水蜜桃| 国产免费福利体检区久久| 97热久久免费频精品99| 久久午夜夜伦鲁鲁片免费无码影视| 久久99国产一区二区三区| 99999久久久久久亚洲| 日产精品99久久久久久| 无码任你躁久久久久久老妇App| 精品国产青草久久久久福利| 久久夜色精品国产亚洲| 久久精品无码专区免费青青| 国内精品伊人久久久久777| 综合久久精品色| 亚洲va久久久久| 中文字幕无码久久人妻| 中文字幕无码久久精品青草| 日日狠狠久久偷偷色综合免费| 久久精品99无色码中文字幕| 伊人久久综在合线亚洲2019| 国产精品久久精品| 国内精品久久久久久野外| 东京热TOKYO综合久久精品| 精品久久久久久无码专区不卡| 国内精品人妻无码久久久影院|