hdu 1428(很好的一道题,最短路+记忆化搜索)

漫步校园

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3802    Accepted Submission(s): 1162


Problem Description
LL 最近沉迷于AC不能自拔,每天寝室、机房两点一线。由于长时间坐在电脑边,缺乏运动。他决定充分利用每次从寝室到机房的时间,在校园里散散步。整个HDU 校园呈方形布局,可划分为n*n个小方格,代表各个区域。例如LL居住的18号宿舍位于校园的西北角,即方格(1,1)代表的地方,而机房所在的第三实验 楼处于东南端的(n,n)。因有多条路线可以选择,LL希望每次的散步路线都不一样。另外,他考虑从A区域到B区域仅当存在一条从B到机房的路线比任何一 条从A到机房的路线更近(否则可能永远都到不了机房了…)。现在他想知道的是,所有满足要求的路线一共有多少条。你能告诉他吗?
 

 

Input
每组测试数据的第一行为n(2=<n<=50),接下来的n行每行有n个数,代表经过每个区域所花的时间t(0<t<=50)(由于寝室与机房均在三楼,故起点与终点也得费时)。
 

 

Output
针对每组测试数据,输出总的路线数(小于2^63)。
 

 

Sample Input
3 1 2 3 1 2 3 1 2 3 3 1 1 1 1 1 1 1 1 1
 

 

Sample Output
1 6
 

 

Author
LL
 

 

Source
 
这个题题意很难读懂啊,,理解了大概就是一个点到另一个点的条件是另一个点必须要比上一个点离终点(n-1,n-1)要近。
所以我们先用优先队列进行宽度优先搜索,把每个点到终点的距离算出来,一定要 从终点算到每个点的距离,因为这样一次bfs就行了,我华丽丽的TLE了
然后用记忆化搜索路径数。原问题的解=子问题所有的路径数相加(记得初始化dp[n-1][n-1]为1)
import java.util.PriorityQueue;
import java.util.Scanner;

 
public class Main {
    static class Node implements Comparable<Node>{
        int x,y,v;
        public Node(int x, int y, int v) {
            this.x = x;
            this.y = y;
            this.v = v;
        }
        @Override
        public int compareTo(Node o) {
            if(this.v>o.v) return 1;
            return -1;
        }
    }
    static Node [] node;
    static int [][] map;
    static int [][]dis; ///记录此点到 (n,n)的最短距离
    static int n;
    static long [][] dp;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            n = sc.nextInt();
            node = new Node[n*n];
            map = new int [n][n];
            dis = new int [n][n];
            dp = new long[n][n];
            int k = 0;
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    map[i][j] = sc.nextInt();
                    node[k++] = new Node(i, j, map[i][j]);
                }
            }
            /*for(int i=0;i<k;i++){  //bfs优先队列求出每点的最短距离 TLE了,应该从终点开始走
                dis[node[i].x][node[i].y] = bfs(node[i]); 
            }*/
            bfs(node[k-1]);
            /*for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    System.out.print(dis[i][j]+" ");
                }
                System.out.println();
            }*/
            dp[n-1][n-1]=1; //初始化终点到自己有一条路
            long ans = dfs(0,0); ///记忆化搜索
            System.out.println(ans);
        }
    }
    static int [][] dir = {{1,0},{-1,0},{0,1},{0,-1}};
    private static long dfs(int x, int y) {if(dp[x][y]>0) return dp[x][y];
     dp[x][y] = 0;
for(int i=0;i<4;i++){ int nextx = x+dir[i][0]; int nexty = y+dir[i][1]; if(nextx<0||nextx>n-1||nexty<0||nexty>n-1) continue; if(dis[nextx][nexty]<dis[x][y]){ dp[x][y] += dfs(nextx,nexty); } }return dp[x][y]; } private static int bfs(Node node) { PriorityQueue<Node> q = new PriorityQueue<Node>(); boolean [][] vis = new boolean[n][n]; dis[n-1][n-1] = node.v; q.add(node); vis[node.x][node.y]=true; while(!q.isEmpty()){ Node t = q.remove(); for(int i=0;i<4;i++){ int nextx = t.x+dir[i][0]; int nexty = t.y+dir[i][1]; if(nextx<0||nextx>n-1||nexty<0||nexty>n-1||vis[nextx][nexty]) continue; vis[nextx][nexty]=true; dis[nextx][nexty] = t.v+map[nextx][nexty]; q.add(new Node(nextx,nexty,dis[nextx][nexty])); } } return -1; } }

 

 

转载于:https://www.cnblogs.com/liyinggang/p/5372535.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值