hdu 1428(很好的一道题,最短路+记忆化搜索)
漫步校园
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3802 Accepted Submission(s): 1162
最近沉迷于AC不能自拔,每天寝室、机房两点一线。由于长时间坐在电脑边,缺乏运动。他决定充分利用每次从寝室到机房的时间,在校园里散散步。整个HDU
校园呈方形布局,可划分为n*n个小方格,代表各个区域。例如LL居住的18号宿舍位于校园的西北角,即方格(1,1)代表的地方,而机房所在的第三实验
楼处于东南端的(n,n)。因有多条路线可以选择,LL希望每次的散步路线都不一样。另外,他考虑从A区域到B区域仅当存在一条从B到机房的路线比任何一
条从A到机房的路线更近(否则可能永远都到不了机房了…)。现在他想知道的是,所有满足要求的路线一共有多少条。你能告诉他吗?
1 2 3
1 2 3
1 2 3
3
1 1 1
1 1 1
1 1 1
6
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(很好的一道题,最短路+记忆化搜索)的更多相关文章
- 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 ...
- HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)
题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...
- hdu 1142 最短路+记忆化
最短路+记忆化搜索HDU 1142 A Walk Through the Forest链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 > 题意 ...
- HDU 1428 漫步校园(记忆化搜索,BFS, DFS)
漫步校园 http://acm.hdu.edu.cn/showproblem.php?pid=1428 Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于 ...
- [HDU 1428]--漫步校园(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1428 漫步校园 Time Limit: 2000/1000 MS (Java/Others) M ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- HDU 1176 免费馅饼(记忆化搜索)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 4444 Walk (离散化建图+BFS+记忆化搜索) 绝对经典
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给你一些n个矩形,给你一个起点,一个终点,要你求从起点到终点最少需要转多少个弯 题解:因为 ...
- HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加 ...
随机推荐
- nodejs创建多层目录
1. fs.mkdir不能一次创建多层目录,必须先创建上层目录,再创建下层目录 //同步 fs.mkdirSync("./tmp/"); fs.mkdirSync("./ ...
- JS获取移动端系统信息(操作系统、操作系统版本、横竖屏状态、设备类型、网络状态、生成浏览器指纹)
function getOS() { // 获取当前操作系统 var os; if (navigator.userAgent.indexOf('Android') > -1 || navigat ...
- Javascript计算世界完全对称日
今天是 2011-11-02 日,微博啊.G+啊什么的都传是世界完全对称日,还说是多少年一遇的.下面写个 JavaScript 小程序,看看是否真的N年一遇.计算范围在公元2000年到3000年. 名 ...
- Codeforces Round #307 (Div. 2) D 矩阵快速幂+快速幂
D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...
- HDU 5640
King's Cake Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- MFC中用正则表达式进行有效性验证
转载自:http://blog.csdn.net/jinhill/article/details/5928993 正则表达式最实用的一个地方是验证用户输入.它可以轻松验证邮编.电话号码.信用卡号码-- ...
- poi-对于word的操作(二)
poi对于word文本的底纹和下划线的样式的展现 package poi.test; import java.io.FileOutputStream; import java.math.BigInte ...
- 洛谷2944 [USACO09MAR]地震损失2Earthquake Damage 2
https://www.luogu.org/problem/show?pid=2944 题目描述 Wisconsin has had an earthquake that has struck Far ...
- vijos 1037 背包+标记
描述 2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了这次灾难.为了纪念“9?11”事件,Mr. F决定自己用水晶来搭建一座双塔. Mr. F有N块水晶,每块 ...
- Item 3 ------单例模式的几种实现方式,及优缺点
单例模式,是指一个类只有一个唯一的实例,一个类只会被实例化一次.实现这种效果,最佳的方式,编写包含单个元素的枚举类型. 单例模式的最佳实现方式-----创建一个包含单个元素的枚举类 public en ...