HDU-1428(记忆化搜索)
#include <iostream>
#include <cstring>
#include <queue>
using namespace std; struct node {
int x,y;
int d;
};
int n;
int map[][];
int dis[][];
__int64 dp[][];
int vis[][];
int dir[][] = {{,},{-,},{,},{,-}}; bool operator < (node a,node b) {
return a.d>b.d;
} void dfs()
{
priority_queue<node> que;
memset(vis,,sizeof(vis));
node tmp,next;
tmp.x = n;
tmp.y = n;
tmp.d = map[n][n];
que.push(tmp);
while(!que.empty())
{
tmp = que.top();
que.pop();
if(vis[tmp.x][tmp.y])
continue;
vis[tmp.x][tmp.y] = ;
dis[tmp.x][tmp.y] = tmp.d;
for(int i = ;i < ;i++) {
next.x = tmp.x+dir[i][];
next.y = tmp.y+dir[i][];
if(next.x<||next.x>n||next.y<||next.y>n||vis[next.x][next.y])
continue;
next.d = tmp.d + map[next.x][next.y];
que.push(next);
}
}
} __int64 solve(int x,int y)
{
if(dp[x][y] != -) return dp[x][y];
else {
node next;
dp[x][y] = ;
for(int i = ;i < ;i++) {
next.x = x+dir[i][];
next.y = y+dir[i][];
if(next.x<||next.x>n||next.y<||next.y>n)
continue;
next.d = dis[next.x][next.y];
if(next.d < dis[x][y])
dp[x][y]+=solve(next.x,next.y);
}
return dp[x][y];
}
} int main()
{
while(cin>>n)
{
memset(dp,-,sizeof(dp));
for(int i = ;i <= n;i++)
for(int j = ;j <= n;j++)
cin>>map[i][j];
dfs();
/*测试最小距离
cout<<endl<<"~~~"<<endl;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++){
cout<<dis[i][j]<<' ';
if(j == n)
cout<<endl;
}
*/
dp[n][n] = ;
cout<<solve(,)<<endl;
}
return ;
}
HDU-1428(记忆化搜索)的更多相关文章
- hdu 4722(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 思路:简单的记忆化搜索,留意一下A==0时的情况就可以了. #include<iostre ...
- HDU 4597 记忆化搜索
² 博弈取牌—记忆化搜索 题目描述: 有两副带有数字的牌,(数字>0)两人轮流取,取中了某张牌,自己的分数就加上牌上的数字,但只能从两端取,每人都会用最优的策略使得自己的分数最高.问A先取,他能 ...
- hdu 1514 记忆化搜索
题意是给4堆(堆的高度小于等于40)有颜色(颜色的种类小于等于20)的物品,你有一个篮子最多能装5件物品,每次从这4堆物品里面任取一件物品放进篮子里,但是取每堆物品时,必须先取上面的物品,才能取下面的 ...
- hdu 1208 记忆化搜索
题目大意:只能按照格子上的数字*方向走,从左上走到右下Sample Input42331121312313110Sample Output3 直接记忆化搜索,注意是0的情况 #include<c ...
- hdu 4960 记忆化搜索 DP
Another OCD Patient Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Ot ...
- hdu 2089 记忆化搜索写法(数位dp)
/* 记忆化搜索,第二维判断是否是6 */ #include<stdio.h> #include<string.h> #define N 9 int dp[N][2],digi ...
- Hdu 4597记忆化搜索
好久没有做题了,水平已经完全在学弟之下了. 一个吉林邀请赛最水的题目.:( 其实这题一看到数据范围,只可以想到思路,直接爆搜,加个记忆化. 这题虽然A了,但是我还是没太想清楚一些边界情况,心虚着A了. ...
- HDU 1978 记忆化搜索(dfs+dp)
Y - How many ways Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- hdu 1078(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 //dp[i][j]表示从点i,j处开始能获得的最多cheese #include <io ...
- hdu 1978 记忆化搜索
注意: dp[i][j] 表示(i,j)这个点有多少种方式 mark[i][j]表示这个点是否走过 假设有直接返回dp[i][j] dp的求法为全部梦走到点的dp的和 注意mark ...
随机推荐
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- Cross-origin resource sharing--reference
Cross-origin resource sharing (CORS) is a mechanism that allows many resources (e.g., fonts, JavaScr ...
- 在top命令下kill和renice进程
For common process management tasks, top is so great because it gives an overview of the most active ...
- .bash_profile和.bashrc的区别
参考资料: http://blog.163.com/wang_hai_fei/blog/static/309020312008728333912/
- NYOJ 214 最长上升子序列nlogn
普通的思路是O(n2)的复杂度,这个题的数据量太大,超时,这时候就得用nlogn的复杂度的算法来做,这个算法的主要思想是只保存有效的序列,即最大递增子序列,然后最后得到数组的长度就是最大子序列.比如序 ...
- Html.RenderPartial和Html.RenderAction的区别
添加一个PartialController控制器 using System; using System.Collections.Generic; using System.Linq; using Sy ...
- javascript 中寻找性能瓶颈
1.如果一个段代码很耗时的话你可以注释掉一部分你认为是很耗时的,或者干脆全部注释掉,然后再一点一点的解开. 2.js优化中最主要的还是对dom操作的优化,单纯的js执行时间是很短的,而js和dom之间 ...
- Js实现简单的联动,无数据库版本
<html> <head> <title></title> <script language="javascript" typ ...
- CSS3 用户界面
CSS3用户界面 在CSS3中,新的用户界面特性包括重设元素尺寸,盒尺寸以及轮廓等. 用户界面属性: resize box-sizing outline-offset 浏览器支持 属性 浏览器支持 r ...
- Carthage&&cocopads 摘抄笔记
Carthage 是 iOS/Mac 开发生态圈的一个包管理工具,与现在流行的 CocoaPods 不同,它是一个去中心化的解决方案.知道它已经有一段时间了,但是一直没有好好玩过,今天整合 Carth ...