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变成一个回文串最少需要添加 ...
随机推荐
- Javascript计算世界完全对称日
今天是 2011-11-02 日,微博啊.G+啊什么的都传是世界完全对称日,还说是多少年一遇的.下面写个 JavaScript 小程序,看看是否真的N年一遇.计算范围在公元2000年到3000年. 名 ...
- Java编程MapReduce实现WordCount
Java编程MapReduce实现WordCount 1.编写Mapper package net.toocruel.yarn.mapreduce.wordcount; import org.apac ...
- 目标世界上最小的Linux系统—ttylinux体验
ttylinux的官方网址:http://ttylinux.net/ 简单翻译一下: 你当前访问的是ttylinux的主页,一个针对多种CPU架构的极小的GNU/Linux系统.最小的ttylinux ...
- LoaderManager与CursorLoader用法
一.基本概念 1.LoaderManager LoaderManager用来负责管理与Activity或者Fragment联系起来的一个或多个Loaders对象. 每个Activity或者Fragme ...
- gitlab 的使用策略和简单介绍
gitlab 作为版本控制器,基本使用和github 相同,以下是一些策略和介绍: Git 分支管理策略可以参考下面三个链接: http://www.ruanyifeng.com/blog/2012/ ...
- [技巧篇]05.关于eclipse模版
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templa ...
- Chromedriver 的安装与配置
首先是下载网址:https://sites.google.com/a/chromium.org/chromedriver/downloads(需要FQ,用Browser浏览器即可翻进,版本要和Chro ...
- Codeforces Round #380 (Div. 2)/729E Subordinates 贪心
There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a ...
- 【设计模式】 模式PK:命令模式VS策略模式
1.概述 命令模式和策略模式的类图确实很相似,只是命令模式多了一个接收者(Receiver)角色.它们虽然同为行为类模式,但是两者的区别还是很明显的.策略模式的意图是封装算法,它认为“算法”已经是一个 ...
- pythonweb框架
https://www.cnblogs.com/sss4/p/8097653.html