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

            文章分類

            推薦博客

            搜索

            •  

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            曾經(jīng)遇到一個(gè)為二維數(shù)組循環(huán)賦值的問題,即賦值后的二維數(shù)組為如下情形:



            當(dāng)時(shí)在網(wǎng)上找了一下答案,基本上都是1層大循環(huán)套4層小循環(huán)還實(shí)現(xiàn)的,感覺不夠優(yōu)雅。最近翻了一下數(shù)據(jù)結(jié)構(gòu)的書,看到迷宮問題受到了一點(diǎn)啟發(fā),感覺同樣是實(shí)現(xiàn)這個(gè)功能,如下代碼要優(yōu)雅一些:


            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 ------> 右->下->左->上 循環(huán)
                    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)         // 已經(jīng)有值
                    {
                        i
            --;        
                        nOffset
            ++;
                        
            continue;
                    }

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

                printMat(mat);

                
            return 0;
            }


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

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

            FeedBack:
            # re: 為二維數(shù)組循環(huán)賦值 2008-03-04 14:39 Enoch
            沙發(fā)!
            不錯(cuò),這個(gè)與循環(huán)相比,時(shí)空性怎樣啊!
            分析時(shí)空性,我想這篇大作應(yīng)該不只是
            “如下代碼要優(yōu)雅一些”

            頂!  回復(fù)  更多評(píng)論
              
            # re: 為二維數(shù)組循環(huán)賦值 2008-03-04 16:29 raof01
            //時(shí)空性能還行,比樓主的代碼難看多啦~~
            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;
            }
            }  回復(fù)  更多評(píng)論
              

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


            亚洲综合熟女久久久30p| 国产成人精品久久亚洲高清不卡| 久久久国产精品| 日韩欧美亚洲综合久久 | 久久影院综合精品| 久久国产精品久久精品国产| 久久久久一本毛久久久| 亚洲va久久久噜噜噜久久男同| 国产精品久久国产精品99盘| 久久精品亚洲乱码伦伦中文| 东京热TOKYO综合久久精品 | 91精品国产综合久久精品| 久久久久无码专区亚洲av| 国产情侣久久久久aⅴ免费| 色天使久久综合网天天| 国产精品VIDEOSSEX久久发布| 国产A级毛片久久久精品毛片| 国产一区二区三精品久久久无广告| 色欲综合久久中文字幕网| 久久人人爽人人爽人人片AV麻豆| 99久久人妻无码精品系列蜜桃| 婷婷国产天堂久久综合五月| 91精品国产综合久久香蕉| 久久精品国产99久久久| 精品多毛少妇人妻AV免费久久| 亚洲欧洲久久久精品| 久久久久久亚洲精品不卡 | 亚洲精品成人久久久| 国产高清国内精品福利99久久| 国产亚洲精久久久久久无码| 国产亚洲精久久久久久无码77777| 亚洲国产精品一区二区三区久久| 久久av高潮av无码av喷吹| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 久久无码专区国产精品发布| 99热成人精品免费久久| 99久久国产热无码精品免费久久久久| 久久66热人妻偷产精品9| 国内精品久久国产大陆| 久久成人影院精品777| 香蕉久久夜色精品国产小说|