• <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>
            隨筆 - 97, 文章 - 22, 評論 - 81, 引用 - 0
            數據加載中……

            Pku 2662 A Walk Through the Forest && HDU 1428 漫步校園 (bfs + dfs)

            首先從終點bfs,保存每一個節點到終點的最短路徑,然后從起點dfs,記憶化,如果滿足題目給定的拓撲序就加上從那個點到終點的路徑數。所謂滿足拓撲序就是題目所給定的taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A,這句話仔細讀上幾十遍就能明白題目意圖了。

            漫步校園
            代碼:

            #include <iostream>
            #include 
            <queue>
            using namespace std;

            int dir[4][2= {{01}{0-1}{10}{-10}};
            struct point {
                
            int x;
                
            int y;
                
            int time;

                
            bool friend operator < (point a, point b) {
                    
            return a.time > b.time;
                }


            }
            temp, tt;

            priority_queue 
            < point > q;
            int hash[51][51], map[51][51];
            __int64 dp[
            51][51];
            int n;

            __int64 dfs(
            int x, int y) {
                
                
            int i;
                
            int tx, ty;
                __int64 sum 
            = 0;

                
            if(x == n && y == n)
                    
            return 1;

                
            for(i = 0; i < 4; i++{
                    tx 
            = x + dir[i][0];
                    ty 
            = y + dir[i][1];
                    
            if(tx < 1 || ty < 1 || tx > n || ty > n)
                        
            continue;
                    
            if(hash[x][y] > hash[tx][ty]) {
                        
            if(dp[tx][ty] == -1{
                            dp[tx][ty] 
            = dfs(tx, ty);
                        }

                        sum 
            += dp[tx][ty];
                    }

                }


                
            return sum;
            }

            int main() {
                
            int i, j;
                
            while(scanf("%d"&n) != EOF) {

                    
            for(i = 1; i <= n; i++{
                        
            for(j = 1; j <= n; j++{
                            scanf(
            "%d"&map[i][j]);
                        }

                    }

                    temp.x 
            = n;
                    temp.y 
            = n;
                    temp.time 
            = map[n][n];
                    memset(hash, 
            -1sizeof(hash));
                    hash[n][n] 
            = map[n][n];
                    
            while(!q.empty())
                        q.pop();

                    q.push( temp );

                    
            while(!q.empty()) {
                        temp 
            = q.top();
                        q.pop();
                        
            for(i = 0; i < 4; i++{
                            tt.x 
            = temp.x + dir[i][0];
                            tt.y 
            = temp.y + dir[i][1];
                            
            if(tt.x < 1 || tt.y < 1 || tt.x > n || tt.y > n)
                                
            continue;
                            tt.time 
            = temp.time + map[tt.x][tt.y];
                            
                            
            if(hash[ tt.x ][ tt.y ] == -1 || tt.time < hash[ tt.x ][ tt.y ]) {
                                hash[ tt.x ][ tt.y ] 
            = tt.time;
                                q.push( tt );
                            }

                        }

                    }


                    memset(dp, 
            -1sizeof(dp));

                    dp[
            1][1= dfs(11);
                    printf(
            "%I64d\n", dp[1][1]);
                }

                
            return 0;
            }

            posted on 2009-03-09 19:12 英雄哪里出來 閱讀(710) 評論(2)  編輯 收藏 引用 所屬分類: ACM

            評論

            # re: Pku 2662 A Walk Through the Forest && HDU 1428 漫步校園 (bfs + dfs)  回復  更多評論   

            taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A

            這句話就是說有拓撲序了,啊…… 太強了,Orz!!
            我都不知道是什么意思!
            2009-12-03 10:28 | huangzhifei

            # re: Pku 2662 A Walk Through the Forest && HDU 1428 漫步校園 (bfs + dfs)  回復  更多評論   

            我就是想過不用DFS,用BFS去遍歷所獲得的耗時表,為什么不行呢?
            詳細的情況:http://acm.hdu.edu.cn/forum/read.php?tid=17192
            希望你能幫我看看,感激不盡~~
            2010-07-23 11:05 | Geek.tan
            亚洲AV无码久久| 91超碰碰碰碰久久久久久综合| 久久精品成人欧美大片| 国产激情久久久久影院老熟女| 日韩一区二区三区视频久久| 欧美日韩久久中文字幕| 粉嫩小泬无遮挡久久久久久| 国产精品成人99久久久久 | 久久久老熟女一区二区三区| 精品久久久久久久| 亚洲国产成人久久综合区| 亚洲AV无码一区东京热久久| 国产免费久久精品99久久| 亚洲精品国产第一综合99久久 | 99久久人妻无码精品系列蜜桃| 精品国产热久久久福利| 久久成人国产精品免费软件| 久久综合丁香激情久久| 7777精品久久久大香线蕉| 久久国产免费直播| 久久99热狠狠色精品一区| 久久精品国产免费观看三人同眠| 国产精品成人精品久久久| 人妻少妇久久中文字幕| 午夜精品久久久久久影视riav| 日韩精品国产自在久久现线拍| 日韩精品无码久久久久久| 国产精品美女久久福利网站| 久久AⅤ人妻少妇嫩草影院| 国产一久久香蕉国产线看观看| 久久久久久久久波多野高潮| 中文字幕亚洲综合久久菠萝蜜| 精品久久综合1区2区3区激情| 久久99久久99小草精品免视看| 欧洲精品久久久av无码电影| 久久久久久久久久久精品尤物| 久久亚洲天堂| 麻豆久久久9性大片| 久久久久久综合网天天| 欧美丰满熟妇BBB久久久| 精品久久久久久国产潘金莲|