漫步校园

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;
}
}
 

hdu 1428(很好的一道题,最短路+记忆化搜索)的更多相关文章

  1. HDU 1142 A Walk Through the Forest(最短路+记忆化搜索)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  2. HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)

    题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...

  3. hdu 1142 最短路+记忆化

    最短路+记忆化搜索HDU 1142 A Walk Through the Forest链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 > 题意 ...

  4. HDU 1428 漫步校园(记忆化搜索,BFS, DFS)

    漫步校园 http://acm.hdu.edu.cn/showproblem.php?pid=1428 Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于 ...

  5. [HDU 1428]--漫步校园(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1428 漫步校园 Time Limit: 2000/1000 MS (Java/Others)    M ...

  6. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  7. HDU 1176 免费馅饼(记忆化搜索)

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  8. HDU 4444 Walk (离散化建图+BFS+记忆化搜索) 绝对经典

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给你一些n个矩形,给你一个起点,一个终点,要你求从起点到终点最少需要转多少个弯 题解:因为 ...

  9. HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加 ...

随机推荐

  1. httpclient post请求带参数返回数据乱码问题解决

    客户端代码: //带参数的post请求 @Test public void doPostWithParam() throws Exception { CloseableHttpClient httpC ...

  2. HDU 5645

    DZY Loves Balls Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others ...

  3. bzoj1178 [Apio2009]CONVENTION会议中心 区间dp+贪心

    [Apio2009]CONVENTION会议中心 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 1130  Solved: 444[Submit][S ...

  4. C#学习之泛型

    //主函数//主函数里面调用的类都在后面有具体描述 using System; using System.Collections.Generic; using System.Linq; using S ...

  5. ACE服务端编程1:使用VS2010编译ACE6.0及从ACE5.6升级的注意事项

    ACE是一个跨平台的用于并发通信的C++框架,项目开始时使用的是ACE 5.6发布版,目前最新的ACE版本是6.3.0. 网上一直有一种黑ACE的氛围,主要黑点在于ACE的复杂和作者的背景,结合实际应 ...

  6. Robot Framework Change chrome language

    由于open browser的参数只有一个ff_profile_dir,所以不能指定chrome profile. 只能通过python 传递lang这个参数去改变语言. python: from s ...

  7. SpringBoot Caused by: java.lang.NoClassDefFoundError: org/apache/tomcat/util/descriptor/tld/TldParser

    最近尝试着用spring boot ,页面模版使用的jsp,在pom里配置了对jsp的支持: <dependency> <groupId>org.apache.tomcat.e ...

  8. 【BZOJ4516】【SDOI2016】生成魔咒 [SAM]

    生成魔咒 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 魔咒串由许多魔咒字符组成,魔咒字符 ...

  9. 【51NOD】独木舟

    [算法]贪心 [题解]比较经典,用l,r两个定位指针分别从左右向中间推进. #include<cstdio> #include<algorithm> #include<c ...

  10. OpenCVSSDpython目标探测对象检测

    1.请参考大牛博客链接 https://www.aiuai.cn/aifarm822.html