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变成一个回文串最少需要添加 ...
随机推荐
- 删边(cip)
删边(cip) 给出一个没有重边和自环的无向图,现在要求删除其中两条边,使得图仍然保持连通. 你的任务是计算有多少组不合法的选边方案.注意方案是无序二元组. Sol 神题,无从下手啊. 考虑点dfs建 ...
- C++分离字符串中的数字和字符 转
#include <iostream> #include <string> #include <vector> using namespace std; void ...
- bzoj 4069 [Apio2015]巴厘岛的雕塑 dp
[Apio2015]巴厘岛的雕塑 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 494 Solved: 238[Submit][Status][Dis ...
- OopenCV复习及函数深入理解(轮廓查询及绘图)
核心函数:(后面标明号的,下面有解析) int cvFindContours(Iplimage* img,//这是输入函数,必须是8bit,单通道的图像---1 CvMemStorage* stora ...
- uboot主Makefile分析(t配置和编译过程详解)
1.编译uboot前需要三次make make distcleanmake x210_sd_configmake -j4 make distclean为清楚dist文件. make x210_sd_c ...
- How to solve “Device eth0 does not seem to be present, delaying initialization” error
Today, I encountered with a strange error after I cloned CentOS 6 guest machine in Oracle VirtualBox ...
- ZooKeeper内部构件
引言 这个文档包含关于ZK内部工作的信息.目前为止,它讨论了这些主题: 原子广播 日志 原子传播 ZK的核心是一个原子的通信系统,它使所有的服务端保持同步. 保证.属性和定义 通过使用ZooKeepe ...
- poi-对于word的操作(二)
poi对于word文本的底纹和下划线的样式的展现 package poi.test; import java.io.FileOutputStream; import java.math.BigInte ...
- Eclipse中安装Tomcat
1. 下载Tomcat并安装: http://tomcat.apache.org/download-60.cgi 2. 下载最新Eclipse的Tomacat插件: http://www.eclips ...
- c# 自定义排序Compare
.net FrameWork 框架博大精深,用着忘着,计划对自己能够想到知识点梳理一下,此篇是对自定义排序的理解: class Program { static void Main(string[] ...