随笔 - 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 英雄哪里出来 阅读(688) 评论(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

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理