[POJ 2329] Nearest number-2
Link:
Solution:
比较明显的$dp$,但爆搜好像也能过
用多个方向$dp$来解决此题,最后汇总答案即可
一开始我写了4个,但后来发现只要相反的2个方向即可,同时不用分别记录答案,直接不断更新答案即可
要特别注意对特例的判断:
不能只判断其最近距离相同且最近点相同!
仅当$(a1,b1)$和$(a2,b2)$当前都仅有一个最近点且其相同时才不增加权值
否则可能$(a2,b2)$有多个最近点但正好记录了与$(a1,b1)$最近点相同的点
Code:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib> using namespace std; const int MAXN=+,INF=<<;
struct number{int cnt,d,x,y;}dp[MAXN][MAXN];
int n,dat[MAXN][MAXN]; void check(int a,int b,int l,int r)
{
if(dp[a][b].d+<dp[l][r].d)
dp[l][r]=dp[a][b],dp[l][r].d++;
else if(dp[a][b].d+==dp[l][r].d) //注意这里的判断细节
{
if(dp[l][r].cnt== && dp[a][b].cnt== && dp[l][r].x==dp[a][b].x && dp[l][r].y==dp[a][b].y) return;
dp[l][r].cnt+=dp[a][b].cnt; //cnt都为1时才返回
}
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) for(int j=;j<=n;j++)
scanf("%d",&dat[i][j]),dp[i][j].d=INF;
for(int i=;i<=n;i++) for(int j=;j<=n;j++)
{
if(dat[i][j]) dp[i][j].d=,dp[i][j].x=i,dp[i][j].y=j,dp[i][j].cnt=;
check(i,j,i+,j);check(i,j,i,j+);
}
for(int i=n;i>=;i--) for(int j=n;j>=;j--)
{
if(dat[i][j]) dp[i][j].d=,dp[i][j].x=i,dp[i][j].y=j,dp[i][j].cnt=;
check(i,j,i-,j);check(i,j,i,j-);
} for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(dat[i][j]){printf("%d ",dat[i][j]);continue;}
if(dp[i][j].cnt==) printf("%d ",dat[dp[i][j].x][dp[i][j].y]);
else printf("0 ");
}
puts("");
}
return ;
}
Review:
Hack能力不足啊,很多细节还是要多想想
如果多次判断内容相同,就放到函数里去吧
[POJ 2329] Nearest number-2的更多相关文章
- [NewTrain 10][poj 2329]Nearest Number - 2
题面: http://poj.org/problem?id=2329 题解: 这题有很多做法 1. 搜索 复杂度$O(n^4)$ 但是实际上远远达不到这个复杂度 所以可以通过 2. 对于每一个格子,我 ...
- 【POJ】2329 Nearest number - 2(搜索)
题目 传送门:QWQ 分析 在dp分类里做的,然而并不会$ O(n^3) $ 的$ dp $,怒写一发搜索. 看起来是$ O(n^4) $,但仔细分析了一下好像还挺靠谱的? poj挂了,没在poj交, ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- LCA POJ 1330 Nearest Common Ancestors
POJ 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24209 ...
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
- POJ 2329 (暴力+搜索bfs)
Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3943 Accepted: 1210 De ...
- POJ-2329 Nearest number - 2(BFS)
Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4100 Accepted: 1275 De ...
- POJ.1330 Nearest Common Ancestors (LCA 倍增)
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...
随机推荐
- 【转载】Linux下安装LoadRunner LoadGenerator
原文地址:[转载]Linux下安装LoadRunner LoadGenerator作者:邱建忠tester LR的负载机安装在linux的理由: 1.windows xp,双核+4G内存,基本上每个v ...
- Java基础-7数组
一).什么是数组: 数组是一组具有相同类型和名称的变量集合,把一系列相同类型的数据保存在一起,这些变量称为数组的元素:每个元素都有一个编号,这个编号叫做下标,下标从 0 开始:元素的个数被称为数组的长 ...
- Microsxxxxxxx-面试总结
策略题 There are four kinds of cards, respectively, 1,2, 3,4 numbers. There are seven cards for each ty ...
- 测试基础面试题 + SQL 面试题(选择题有部分答案,难度:低)
测试基础面试题 + SQL 面试题(选择题有部分答案,难度:低) 答案: .A .C .C .A .A .D
- NBA投篮
D 辅助插件:原生 游戏制作难度系数:初级 游戏教程网址:http://www.raywenderlich.com/20333/beginning-unity-3d-for-ios-part-1 1. ...
- NGUI-为Popuplist的下拉选项添加删除功能
NGUI例子里的popuplist是这样的:,但有时我们希望下拉选项都有删除功能,也就是这样:,一种方法是改popuplist的源码,我想这个实现起来不难,但现在我想说的是用反射来实现此功能,以及其他 ...
- 【bzoj4898】[Apio2017]商旅 Floyd+分数规划+Spfa
题目描述 有n个点.m条边.和k种商品.第$i$个点可以以$B_{ij}$的价格买入商品$j$,并以$S_{ij}$的价格卖出.任何时候只能持有一个商品.求一个环,使得初始不携带商品时以某种交易方式走 ...
- ubuntu系统更换源
一:问题概述 ubuntu,我们在使用apt新装软件的时候,会使用官方的网站去下载软件,但是会因为国内的转接点太多,而导致下载的速度非常慢 ,我们可以通过换成一些中间的节点来进行下载,比如阿里源,中科 ...
- 2017 多校2 hdu 6053 TrickGCD
2017 多校2 hdu 6053 TrickGCD 题目: You are given an array \(A\) , and Zhu wants to know there are how ma ...
- div样式
DIV样式汇总 一.常用属性: 1.Height:设置DIV的高度. 2.Width:设置DIV的宽度. 例: <div style="width:200px;height:200px ...