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

            This blog has been shut down permanently.

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              13 隨筆 :: 0 文章 :: 25 評(píng)論 :: 0 Trackbacks

            1   2   3     4
            12 13  14   5
            11 16  15   6
            10  9    8    7  

            這種情況用模擬比較好推出,以1為坐標(biāo)原點(diǎn),一次遞增,遇到邊界則轉(zhuǎn)向

            #include<stdio.h>
            #include
            <string.h>
            #define MAX_SIZE 100
            const int intx[]= {0,1,0,-1};
            const int inty[]= {1,0,-1,0};
            int main() 

               
            int dir,i,j,n,data,x,y,nextx,nexty;
               
            int arr[MAX_SIZE][MAX_SIZE];
               
            /*Read size*/
               printf(
            "please input the size\n");
               scanf(
            "%d",&n);
               
            /*Init*/
               x
            = y= 0;
               dir
            = 0;
               memset(arr,
            0,sizeof(arr));
               
            /*fill*/
               
            for(data=1; data<=n*n; data++)
               
            {
                   arr[x][y]
            = data;
                   nextx
            = x+intx[dir];
                   nexty
            = y+inty[dir];
                   
            if(arr[nextx][nexty] || nextx>=|| nexty>=|| nextx<0 || nexty<0)
                   
            {
                       dir
            ++;
                       
            if(dir==4)dir=0;
                   }

                   x
            += intx[dir];
                   y
            += inty[dir];
               }

               
            for(i=0; i<n; i++)
               
            {
                   
            for(j=0; j<n; j++)
                       printf(
            "%d\t",arr[i][j]);
                   printf(
            "\n");
               }

            }

             

            如果碰到另外一種情況就麻煩多了

                      21  22................
                       20  7  8  9  10
                       19  6  1  2  11
                       18  5  4  3  12
                       17  16 15 14 13

            如果從中間開始模擬

            從原點(diǎn)1開始,看方向的變化:右下左上;行走的步數(shù):11223344……

             公式:n^2= 1+1+2+2+...+n-1+n-1+n(第一種情況也有用到)

            有了公式,那就可以先算出最大的data是多少,這樣又轉(zhuǎn)換為第一種方法了。

            如果沒有公式,單純對(duì)行為進(jìn)行模擬難度很大,不過還是可以實(shí)現(xiàn)的。

            現(xiàn)在暫時(shí)還沒有想到實(shí)現(xiàn)的方法,想到以后再補(bǔ)充吧。
             

            posted on 2009-11-15 16:02 iZ 閱讀(3365) 評(píng)論(4)  編輯 收藏 引用 所屬分類: 『Algorithm,Data Structure in C++』

            評(píng)論

            # re: 螺旋矩陣的算法 2009-11-17 16:39 qinqing1984
            這個(gè)模型比較簡單,從中間向外轉(zhuǎn),就有點(diǎn)復(fù)雜了  回復(fù)  更多評(píng)論
              

            # re: 螺旋矩陣的算法 2009-11-18 17:02 xuxiandi
            從中間向外轉(zhuǎn)....不就是從外向中間轉(zhuǎn)..然后遞減嗎?  回復(fù)  更多評(píng)論
              

            # re: 螺旋矩陣的算法 2009-11-19 13:44 iSsay
            @xuxiandi
            我在想如果沒有數(shù)學(xué)公式該怎么模擬呢

            現(xiàn)在還想不出來,因?yàn)檠h(huán)控制不好弄,要用到很多變量標(biāo)記  回復(fù)  更多評(píng)論
              

            # re: 螺旋矩陣的算法 2012-10-22 00:07 光亮
            if(arr[nextx][nexty] || nextx>=n || nexty>=n || nextx<0 || nexty<0)

            作者這句話有點(diǎn)小錯(cuò)誤。。第一個(gè)判斷放到最后更好  回復(fù)  更多評(píng)論
              

            国内精品伊人久久久久网站| 久久中文娱乐网| 亚洲精品乱码久久久久久不卡| 久久婷婷五月综合97色直播| 久久嫩草影院免费看夜色| 亚洲精品美女久久久久99| 久久成人国产精品二三区| 久久精品国产乱子伦| 久久精品无码一区二区三区| 2021国内久久精品| 99热热久久这里只有精品68| 久久亚洲AV无码精品色午夜 | 欧美色综合久久久久久| 亚洲国产精品无码成人片久久| 99久久精品免费观看国产| 人妻无码αv中文字幕久久 | 2021国产精品午夜久久| 久久99精品九九九久久婷婷| 青草国产精品久久久久久| 伊人情人综合成人久久网小说| 久久91精品久久91综合| 久久AV高清无码| 久久人爽人人爽人人片AV| 中文字幕无码av激情不卡久久| 亚洲午夜久久久精品影院| 久久九九亚洲精品| AV狠狠色丁香婷婷综合久久| 亚洲中文字幕久久精品无码APP| 国产精品熟女福利久久AV| 99久久精品免费看国产一区二区三区| 国产成人久久精品激情| 久久精品国产精品亚洲毛片| 亚洲中文字幕无码久久精品1| 久久人做人爽一区二区三区 | 成人久久精品一区二区三区| 久久久精品国产免大香伊 | 9久久9久久精品| 精品久久久久久无码中文野结衣| 久久精品国产99国产电影网| 欧美久久精品一级c片片| 久久综合九色综合97_久久久|