• <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>
            隨筆 - 89  文章 - 118  trackbacks - 0
            <2008年3月>
            2425262728291
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            留言簿(16)

            隨筆分類(56)

            隨筆檔案(89)

            文章分類

            推薦博客

            搜索

            •  

            最新隨筆

            最新評論

            閱讀排行榜

            曾經遇到一個為二維數組循環賦值的問題,即賦值后的二維數組為如下情形:



            當時在網上找了一下答案,基本上都是1層大循環套4層小循環還實現的,感覺不夠優雅。最近翻了一下數據結構的書,看到迷宮問題受到了一點啟發,感覺同樣是實現這個功能,如下代碼要優雅一些:


            const 
            int ROW__ = 10;
            const 
            int COL__ = 10;
            int mat[ROW__][COL__];

            struct Position
            {
                
            int    nRow;
                
            int nCol;
            };

            void printMat(int mat[ROW__][COL__]);

            int main(int argc, char* argv[])
            {
                Position offset[
            4];
                offset[
            0].nRow = 0;        offset[0].nCol = 1;
                offset[
            1].nRow = 1;        offset[1].nCol = 0;
                offset[
            2].nRow = 0;        offset[2].nCol = -1;
                offset[
            3].nRow = -1;    offset[3].nCol = 0;

                Position curPos;
                curPos.nRow 
            = 0;
                curPos.nCol 
            = 0;
                mat[
            0][0= 1;

                
            int nOffset = 0;

                Position tempPos;
                
            for (int i = 1; i < ROW__*COL__; i++)
                {
                    
            // nOffset % 4 ------> 右->下->左->上 循環
                    tempPos.nRow = curPos.nRow + offset[nOffset % 4].nRow;
                    tempPos.nCol 
            = curPos.nCol + offset[nOffset % 4].nCol;

                    
            if (   tempPos.nRow >= ROW__ || tempPos.nRow < 0
                        
            || tempPos.nCol >= COL__ || tempPos.nCol < 0    // 不超過邊界
                        || mat[tempPos.nRow][tempPos.nCol] > 0)         // 已經有值
                    {
                        i
            --;        
                        nOffset
            ++;
                        
            continue;
                    }

                    curPos 
            = tempPos;
                    mat[curPos.nRow][curPos.nCol] 
            = i+1;
                }

                printMat(mat);

                
            return 0;
            }


            printMat函數這些就不提供了,它的功能是打印出這個數組。我上傳了一下工程,有興趣的朋友點此下載。

            posted on 2008-03-04 10:30 胡滿超 閱讀(4834) 評論(2)  編輯 收藏 引用

            FeedBack:
            # re: 為二維數組循環賦值 2008-03-04 14:39 Enoch
            沙發!
            不錯,這個與循環相比,時空性怎樣?。?
            分析時空性,我想這篇大作應該不只是
            “如下代碼要優雅一些”

            頂!  回復  更多評論
              
            # re: 為二維數組循環賦值 2008-03-04 16:29 raof01
            //時空性能還行,比樓主的代碼難看多啦~~
            template <size_t N1, size_t N2>
            void Convert(int (&array)[N1][N2])
            {
            int all = N1 * N2;
            int i = 0;
            int j = -1;
            int direction = 0;
            int val = 0;
            while(val < all)
            {
            if (0 == direction)
            {
            ++j;
            if (j < N2 && -1 == array[i][j])
            {
            array[i][j] = val;
            }
            else
            {
            direction = 1;
            --j;
            }
            }
            if (1 == direction)
            {
            ++i;
            if (i < N1 && -1 == array[i][j])
            {
            array[i][j] = val;
            }
            else
            {
            direction = 2;
            --i;
            }
            }
            if (2 == direction)
            {
            --j;
            if (j >= 0 && -1 == array[i][j])
            {
            array[i][j] = val;
            }
            else
            {
            direction = 3;
            ++j;
            }
            }
            if (3 == direction)
            {
            --i;
            if (i >= 0 && -1 == array[i][j])
            {
            array[i][j] = val;
            }
            else
            {
            direction = 0;
            ++i;
            --val;
            }
            }
            ++val;
            }
            }  回復  更多評論
              
            亚洲中文久久精品无码| 三级三级久久三级久久| 国产色综合久久无码有码| 亚洲婷婷国产精品电影人久久| 中文字幕精品久久久久人妻| 2020国产成人久久精品| 99久久99久久| 久久精品成人影院| 久久久久久久久无码精品亚洲日韩| 91精品国产高清91久久久久久| 亚洲一本综合久久| 亚洲va中文字幕无码久久| 女人高潮久久久叫人喷水| 热久久这里只有精品| 久久w5ww成w人免费| 日本加勒比久久精品| 国产成人久久777777| 国内精品人妻无码久久久影院| 狠狠色婷婷久久综合频道日韩| 久久99精品国产| 99精品国产在热久久无毒不卡| 久久久久亚洲AV无码专区桃色| 久久亚洲欧美日本精品| 囯产精品久久久久久久久蜜桃| 久久久久亚洲精品天堂久久久久久| 久久婷婷国产综合精品| 国产精品禁18久久久夂久| 99久久国产亚洲综合精品| 久久午夜综合久久| 久久久WWW成人免费精品| 99re久久精品国产首页2020| 久久不见久久见免费视频7| 亚洲AV无码久久寂寞少妇| 伊人久久大香线蕉亚洲| 欧美日韩精品久久久久| 欧美精品九九99久久在观看| 亚洲日韩欧美一区久久久久我 | 久久只有这里有精品4| 久久久久噜噜噜亚洲熟女综合 | 久久久久久精品免费看SSS| 色综合久久无码中文字幕|